Jump to content

Checking Array Values Are Numbers


thara

Recommended Posts

can anybody tell me how can check values in an array have only 4 digits long numbers. if not, if there are strings or numbers long to more than 4 digits, I want to echo an error massege.

 

This is Correct one : $searchText = '3423, 2453, 3245 , 2425, 6765';

This is Wrong one : $searchText = '34d23, 244353, fsddf , 2d4425, 674365';

 

can any body tell me how can I do this?

 

thank you...

 

Link to comment
https://forums.phpfreaks.com/topic/270239-checking-array-values-are-numbers/
Share on other sites

You could always try your results against is_int.

 

Then if you want to know if it's 4 digits after validating that it is an integer value you could always try:

if ( $value >= 1000 && $value <= 9999 )
{
   //Code true
}
else
{
   //Code false / error message
}

here is a list of function you could use

  • is_bool() - Finds out whether a variable is a boolean
  • is_float() - Finds whether the type of a variable is float
  • is_numeric() - Finds whether a variable is a number or a numeric string
  • is_string() - Find whether the type of a variable is string
  • is_array() - Finds whether a variable is an array
  • is_object() - Finds whether a variable is an object

 

This is Correct one : $searchText = '3423, 2453, 3245 , 2425, 6765';

This is Wrong one : $searchText = '34d23, 244353, fsddf , 2d4425, 674365';

 

thank you...

 

Those are not arrays, they are strings.

 

Your question is far from clear.

 

Does a particular number have to be exactly 4 digits or can it be from 1-3 digits long?

What is the output you expect?

yes gizmola.. Its not an array. It is a sting that comes from a form. What I need to do is users can enter 4 digits long numbers separating by commas then I need to collect them. And want check these are numbers or its contain strings or long than 4 digits etc..

 

Thank you..

Some code like this is what you want:

 

$numbers = explode(',', $searchText);

foreach ($numbers as &$number) {
// make sure you don't have any spaces in the string
$number = trim($number);

if (is_int($number) && strlen($number) == 4) {
	echo "$number is ok";
} else {
	echo "$number is not a 4 digit number";
}

}

I got a solution to this using this function..

 

 $searchText = $_POST['searchText']; 

 function validate($input) {
  $searchArray = array_map('trim', explode(',', $input));
  foreach($searchArray as $item) {
   if(filter_var($item, FILTER_VALIDATE_INT) === false || strlen($item) != 4)
 return false;
  }
  return true;
 }
 if(!validate($searchText)) {
  $searchingError= 'Enter four digits length number!';
  echo '<script type="text/javascript">';
  echo "alert('" . $searchingError . "')";
  echo "</script>";
 } else {
  echo $searchText;
 }

 

can I know this a good way to do this?

Apart from never reaching the "is ok" code... not a lot. :shy:

 

Yep, the code could be something like that,

 

$numbers = array_map('intval',explode(',', trim($searchText)));
foreach ($numbers as &$number) {

    if (is_int($number) && strlen($number) == 4) {
		   echo "$number is ok";
    } else {
		    echo "$number is not a 4 digit number";
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.