Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- Preg Match Problems (I Think!)


TheMadScientist - 2:40 am on Mar 24, 2011 (gmt 0)


Uh, 0-9 is zero to nine, numerically ... You need to add the dash to the possibilities of matches:

preg_match("/^[0-9-]+/", $_POST['name']);

You also want it to be all numerical or dashes, so you should match start ^ to finish $

preg_match("/^[0-9-]+$/", $_POST['name']);

And you want it to match only if it starts with a number, so you need to eliminate the - match from the first character match

preg_match("/^[0-9][0-9-]+$/", $_POST['name']);

And keep in mind the preceding will match 9--------- which may not be desirable, so you might want to check for specific NNNN-NNNNNNNN patterns, like this, which would match 4 numbers followed by - followed by 8 numbers:

preg_match("/^[0-9]{4}-[0-9]{8}$/", $_POST['name']);

And, I'll let you have some fun with it from there...


Thread source:: http://www.webmasterworld.com/php/4286350.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com