
November 12th, 2012, 03:27 PM
|
 |
We're trapped inside a game!
|
|
Join Date: Jul 2008
Location: Maryland
|
|
|
Why does the order matter?
I'm not good with regex, much more than email validation loses me. But I'm working on an older code repo that has some filtering going on for its form validation functions.
This is how the function validates a person's first or last name:
PHP Code:
filter_var('3 32pm', FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/[a-zA-Z][a-zA-Z0-9-_\.]{2,32}/")));
And in testing our form out, my coworker put in "3 32 pm" in the last name field. The validation failed so I took the above snippet and tested it with the following values with the respective values returned by var_dump():
3 32 pm - false
pm3 32 - pm3 32
3 pm32 - 3 pm32
3pm pm32 - 3pm pm32
3 pm 32 - false
I rearranged the regex like so, with the number range first:
PHP Code:
filter_var('3 pm 32', FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/[0-9a-zA-Z][a-zA-Z-_\.]{2,32}/")));
And tested the above values that produced a false result before; this time they returned the filtered string, rather than false.
What I don't understand is why the order mattered here, shouldn't the PHP function evaluate the entire string before returning a value? If my input contained '$' or '@' I'd expect it to be removed, but this thing with the numbers seems odd. I can't think of why someone would put the time in for their name and I can fix this bug, but the issue is still really weird.
__________________
"Those who can make you believe absurdities can make you commit atrocities."
|