Forum Moderators: coopster

Message Too Old, No Replies

using eregi to validate integers

         

bysonary

4:44 pm on Feb 12, 2007 (gmt 0)

10+ Year Member



hello, I am looking into validating a form field to see if it is an integer value or not, I am sure I need to use eregi but am not sure if what I am thinking will work, can someone offer me some advice.. I have the following idea..

$nextq = (int)$_POST['nextquest'];

if (eregi([0-9], $nextq))
{
//INSERT $nextq INTO THE DATABASE
}
else
{
//DISPLAY ERROR IDENTIFYING $nextq AS A NON INT
}

can someone tell me if my if statement looks right and perhaps if i could do it like this..


if (!eregi([0-9], $nextq))

sort of like if nextq is not an int

any help here would be appreciated

Psychopsia

5:27 pm on Feb 12, 2007 (gmt 0)

10+ Year Member



Another way is:

if (preg_match('/^\d$/', $nextq))
{
// SQL
}

bysonary

5:32 pm on Feb 12, 2007 (gmt 0)

10+ Year Member



ah cool so my way looks ok then does it even the if (!eregi()){} way?

Psychopsia

5:41 pm on Feb 12, 2007 (gmt 0)

10+ Year Member



I think so, don't know exactly because I never use ereg o eregi.

if (!preg_match('/^\d$/', $nextq))
{
// ERROR
}

bysonary

5:42 pm on Feb 12, 2007 (gmt 0)

10+ Year Member



Can you explain this I dont understand how this is checking to see if the value of the $nextq variable is an integer or not?

Psychopsia

5:50 pm on Feb 12, 2007 (gmt 0)

10+ Year Member



Check this out: [webmasterworld.com...]

Specially on "Some special character sequences" block.

[edited by: Psychopsia at 6:04 pm (utc) on Feb. 12, 2007]

dreamcatcher

6:01 pm on Feb 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP has built in functions for checking for digits. Check out the ctype_digit [uk.php.net] function or you can use the character class [:digit:]

$string = '5';

if (ctype_digit($string))
{
//ok
}

if (ereg("^[[:digit:]]+$", $string))
{
//ok
}

dc

bysonary

9:29 pm on Feb 14, 2007 (gmt 0)

10+ Year Member



when using


if (preg_match('/^\d$/', $nextq) && preg_match('/^\d$/', $weight))
{
// SQL
}
else
{
//ERROR
}

it doesnt work if enter the number 10 in any of the fields e.g.

$nextq = 7
$weight = 10

i get an error with the above if statement and the values, I tried


if (ctype_digit($nextq) && ctype_digit($weight))
{
//SQL
}
else
{
//ERROR
}

that ctype_digit code failed too, can anyone tell me how i can this to work when an integer of say "10" is entered?

bysonary

11:32 pm on Feb 14, 2007 (gmt 0)

10+ Year Member



got it working finally with

if(is_numeric($nextq) && is_numeric($weight))

bysonary

1:57 am on Feb 16, 2007 (gmt 0)

10+ Year Member



is_numeric wasnt working properly it accepted a1 and b2 as valid when they contain alphabetic characters

I found out that ctype_digit works however the reason it wasnt working before was because i was testing $nextq

and $nextq = (int)$POST['next_qid'];

the (int) was preventing ctype_digit from working

jatar_k

11:47 am on Feb 16, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> (int)

you should never (I've only used it once in a production script) need to typecast vars in php, even though types are fluid in php they work well. If you are always sure what type of data you are passing around you'll be ine.

As you can see with your example, you created a situation where you changed data before you tested it which gave wrong results.

and my apologies, I saw that when I first read this thread and should have said something.

Psychopsia

2:39 pm on Feb 16, 2007 (gmt 0)

10+ Year Member



it doesnt work if enter the number 10 in any of the fields

Sorry, was a mistake in the pattern, should be:

if (preg_match('/^\d+$/', $nextq) && preg_match('/^\d+$/', $weight))

\d+$ --- not \d$