Snetty
11-16-2006, 12:24 PM
Lo guys, I'm stuck with my regular expressions again.
if (!eregi("^[[0-10]]*$", $_POST['rating'])){
//display error
}
The $_POST['rating'] needs to contain a value between 0 and 10, that's all. Can anyone tell me why the above code doesn't work if I put in 10, but works fine for all numbers below it.
Thanks in anticipation.
Snetty
11-16-2006, 12:26 PM
actually, it doesn't work at all. It works if I put 0-9 in place of 0-10
SeeIT Solutions
11-16-2006, 12:37 PM
It won't work because 0-9 means 1 character, any number. [0-10] means 1 character but 10 is actually 2 characters.
You would need to use this I believe:
if (!eregi("^[[0-9]{1,2}]*$", $_POST['rating'])){
//display error
}
Snetty
11-16-2006, 01:03 PM
It won't work because 0-9 means 1 character, any number. [0-10] means 1 character but 10 is actually 2 characters.
You would need to use this I believe:
if (!eregi("^[[0-9]{1,2}]*$", $_POST['rating'])){
//display error
}
Thanks for the help, but won't that allow any number up to 99?
SeeIT Solutions
11-16-2006, 01:13 PM
True, one way to do it would be change the if statement to be either 0-9 or 10.
marek_mar
11-16-2006, 01:15 PM
It shouldn't work at all.
$rating = abs((int) $_POST['rating']);
if($rating > 10)
{
// Display error;
}
Snetty
11-16-2006, 01:18 PM
thanks marek_mar.. i really wanted to do all my validation with regex, just for consistency more than anything else. But I've had to deviate in other areas, so I guess one more won't hurt.
Thanks for the help guys.
marek_mar
11-16-2006, 01:32 PM
You should use regex only if it is really needed... it's slower in most cases.
Snetty
11-16-2006, 02:03 PM
oh ok, thanks for the advice, i will take it on board..
chump2877
11-17-2006, 01:44 AM
if (preg_match('/^(0|1|2|3|4|5|6|7|8|9|10)$/', $_POST['rating']) == 0)
{
//display error
}
that seems like it should work....