View Full Version : Simple Regex problem :(

07-14-2008, 12:54 PM
I'm trying to write a regular expression that validates a form entry. It's designed to only allow uppercase alphabetic characters and numbers, it must also be between 2 and 4 characters in length (inclusive).

I came up with the following.. but it's allowing pretty much everything through, can anyone see where I'm going wrong?

if(preg_match('/^([A-Z0-9]{2,4})$/', $_POST['prefix']) === FALSE){ fail }

Any help much appreciated.

07-14-2008, 01:01 PM
See http://in.php.net/preg_match

Return Values

preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject . preg_match() returns FALSE if an error occurred.

07-14-2008, 01:22 PM

if (!preg_match('/^[A-Z0-9]{2,4}$/', $_POST['prefix'))
echo "Invalid";



07-14-2008, 06:31 PM
excellent, thanks for the help guys. I used to use eregi and I guess I just didn't think to check how preg_match deals with not finding a match.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum