PHP woes : checking if value is numeric

Status
Not open for further replies.

paul

Ninja
I'm using this function from PHP: is_numeric - Manual
PHP:
 function is_whole_number($var){
  return (is_numeric($var)&&(intval($var)==floatval($var)));
}

Now this works to see if the input is a numeral. But what should I do to check if the value is set in the first place ? I tried PHP: isset - Manual isset but that didn't swing it.

I'm sure it's something obvious I am missing. Any hints ? Thanks ?
 

louie

New Member
you do the isset() before calling the function
PHP:
 is_whole_number($var)
 

paul

Ninja
Thanks Louie for the help

Still having problems though
PHP:
function is_whole_number($var){
  return (is_numeric($var) && (intval($var)==floatval($var)) && ($var!=NULL) );
}

if ( isset($_POST[checkit]) && is_whole_number($_POST[checkit])){
// do
}else {
// no do
}
still bails out when I don't have a value in my form to check.

I have an input box that I want people to either leave blank, or enter a numeral. Otherwise if they enter 'six' or something like that I want it to report the error. :(
 

louie

New Member
don't get it. what format are you loonking to get from the visitor?

like 6.00 or 6

give an example
 

paul

Ninja
Hi Louie,

I want to make sure that a user gives a whole decimal. The field is four characters long, and I am afraid some bright spark will enter 'five' instead of '5'. So only a whole number will do. No "two" "2,3" or "2.3" values should be accepted only 2.
 

louie

New Member
try this:

PHP:
if (isset($_POST['checkit'])){
  $no_post = trim($_POST['checkit']);
  if(is_numeric($no_post)){
    // do something here cause is number
  }else {
    // no do only numbers
  }  
}else{
  //you haven't enter any numbers
}
 

paul

Ninja
sorry about this I forgot to mention that if he enters a null value then that is okay.
so logic
Code:
 is NULL == okay proceed
 is ! NULL 
      IS NUMERIC == okay proceed
      IS NOT NUMERIC == give out error.
 

louie

New Member
PHP:
if (isset($_POST['checkit'])){
$no_post = trim($_POST['checkit']);
if(is_numeric($no_post)){
// do something here cause is number
}else {
// no do only numbers
} 
}
 

paul

Ninja
Cheers for your help louie and pointing me in the right direction.

The problem was checking if something was NULL . I used
PHP:
function is_whole_number($var){
  return (is_numeric($var)&&(intval($var)==floatval($var)));
}


function postvars() {
   foreach(func_get_args() as $var) {
  // echo $var. " ". $_POST[$var]." <br/>" ;
       if( $_POST[$var] === '' || is_whole_number($_POST[$var])) return true;
   }
   return false;
}

$happy = (postvars('checkit'));
 
Status
Not open for further replies.
Top