wayne Posted March 19, 2006 Posted March 19, 2006 Using php I want to find out if a value entered in a form is 4 to 6 digits only. I used the expression if (!ereg("[0-9]{4,6})) die ("<h3 align=\"center\">The value you entered must be 4-6 digits only.<br />Use the back button and try again.</h3>"); value - expected result - result 123 - fail - fail 1234 - pass - pass 123456 -pass -pass 1234567 - fail -PASS!!!!! Why does this not work??? Any help would be appreciated. Quote
TweezerMan Posted March 19, 2006 Posted March 19, 2006 The regex does not work as you as intend because as you have it, the regex will match on any substring of 4 to 6 digits. Besides allowing '1234567' to pass, values such as 'xyz1234' and '1234xyz' will also pass, because the test string contains a string of 4 to 6 digits. If you add "^" to the beginning of the regex (regex must match at beginning of string) and "$" to the end of the regex (regex must match at end of string), this will force the regex to match only on the entire string, rather than matching on a substring: >if (!ereg("^[0-9]{4,6}$", $testvalue)) die ("<h3 align=\"center\">The value you entered must be 4-6 digits only.<br />Use the back button and try again.</h3>"); Quote
wayne Posted March 20, 2006 Author Posted March 20, 2006 David You know I went to a site, read the rules about 20 times and could not see why this was not working. Read your post and you know...a light bulb went off in my head..of course. And of course it now works thanks to your reply, much appreciated!! Wayne Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.