Jump to content

Recommended Posts

Posted

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.

Posted

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>");

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...