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
Share on other sites

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

Link to comment
Share on other sites

 

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?

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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";
}

}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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";
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.