Forum Moderators: coopster

Message Too Old, No Replies

is numeric is causing an error in if statement

php is_numeric error

         

diana_burns

12:11 am on Oct 29, 2006 (gmt 0)

10+ Year Member



Here is my code that collects a zipcode from the html form. However, it doesn't work as it should. It always prints the else statement even though I've entered a number in the field. Can someone help me with this please. I am pretty new to PHP and trying very hard.

If I remove the is_numeric then it works fine, but I need it to check if the entry is a number.

if (is_numeric(!empty($_REQUEST['pf_zipcode']))){
$pf_zipcode = (trim(stripslashes($_REQUEST['pf_zipcode'])));
} else {
$pf_zipcode = NULL;
echo "<p>Required field: Enter your 5 digit zipcode</p>";
}

Thank you in advance.

Psychopsia

12:40 am on Oct 29, 2006 (gmt 0)

10+ Year Member



Hi diana_burns, welcome to WebmasterWorld!

Try with this:

$pf_zipcode = $_REQUEST['pf_zipcode'];
if (!empty($pf_zipcode) && is_numeric($pf_zipcode))
{
$pf_zipcode = trim(stripslashes($pf_zipcode));
}
...

Hope this helps!

[edited by: Psychopsia at 12:41 am (utc) on Oct. 29, 2006]

dreamcatcher

8:21 am on Oct 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Diana,

Another option would be to use the character class:

if (!empty($pf_zipcode) && ereg("^[[:digit:]]+$", $pf_zipcode))

dc

diana_burns

3:05 am on Oct 30, 2006 (gmt 0)

10+ Year Member



Thanks! I'll give your suggestions a try.