Forum Moderators: coopster

Message Too Old, No Replies

PHP to refuse 0 as a numeric value

         

dbarasuk

10:59 pm on May 20, 2008 (gmt 0)

10+ Year Member



Hi,
I have a small piece of code to test whether a field contains a value among other things less than 0 and then issue an error message that the value provided is invalid like this:

if(empty($math) ¦¦ !is_numeric($math) ¦¦ $math < 0 ¦¦ $math > 20)
{
$error[] = "Le math field is invalid";

}

If I type 0 inside, it's issuing the above error message. How can I do so that Zero get accepted as an integer?

Thanks,

LifeinAsia

11:09 pm on May 20, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



How are you defining or passing $math to your code?

eelixduppy

11:15 pm on May 20, 2008 (gmt 0)



zero is considered to be "empty" when using empty(). You should probably change your code to the following:

if(!isset($math) ¦¦ !is_numeric($math) ¦¦ $math < 0 ¦¦ $math > 20)

dbarasuk

11:16 pm on May 20, 2008 (gmt 0)

10+ Year Member



The definition is inside the following code:

if(array_key_exists('submit', $_POST))
{
foreach(array_keys($_POST) as $key)
{
$$key=intval(trim($_POST[$key]));
switch($key)
{
case 'math':
if(empty($math) ¦¦ !is_numeric($math) ¦¦ $math < 0 ¦¦ $math > 20)
{
$error[] = "The math field is invalid";

}

break;
The code continues here....

Dinkar

1:14 pm on May 21, 2008 (gmt 0)

10+ Year Member



Try :

if(empty($math) ¦¦ !is_numeric($math) ¦¦ $math < -1 ¦¦ $math > 20)

Dinkar

1:19 pm on May 21, 2008 (gmt 0)

10+ Year Member



Try 1 and not -1. Sorry for wrong code.

BTW, I can't edit my post, why?