Crazy Horse
asked on
only allow numbers with preg match
I tried this:
if(!preg_match('/\d/', trim($_POST['number']))) {
// show error
which shows an error if I type in "ABC" for example, but if I type in "abc123" then it doesn't show the error message I created. I want the field to only allow numbers/digits so there must be something wrong with my regular expression?
if(!preg_match('/\d/', trim($_POST['number']))) {
// show error
which shows an error if I type in "ABC" for example, but if I type in "abc123" then it doesn't show the error message I created. I want the field to only allow numbers/digits so there must be something wrong with my regular expression?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry, Ray. I awarded points before you posted.
No problem. I have enough points to orbit Saturn.
I have found over time that regular expressions (like many things in programming) make more sense when they are broken out into component parts with explanatory comments. Also, this is useful:
https://www.cheatography.com/davechild/cheat-sheets/regular-expressions/pdf/
I have found over time that regular expressions (like many things in programming) make more sense when they are broken out into component parts with explanatory comments. Also, this is useful:
https://www.cheatography.com/davechild/cheat-sheets/regular-expressions/pdf/
https://www.experts-exchange.com/articles/7830/A-Quick-Tour-of-Test-Driven-Development.html
Here's a test script with the regex deconstructed into components and comments to show the parts.
https://iconoun.com/demo/temp_black_sulfur.php
Open in new window