Link to home
Start Free TrialLog in
Avatar of Crazy Horse
Crazy HorseFlag for South Africa

asked on

regular expression for 10 digit number

I am trying to simplify my telephone number validation and I just want a regular expression to check that the input is only numbers and must be 10 digits. I am not very clued up on regular expressions/preg_match and any assistance would be appreciated.

Also, if anyone can point me in the direction of a good (not difficult to understand) tutorial on basic regular expressions I would be grateful.
SOLUTION
Avatar of Frank Helk
Frank Helk
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A good place to start learning regular expressions:
http://www.regular-expressions.info/tutorial.html
http://www.regular-expressions.info/quickstart.html

This seem OK, but I haven't used it:
https://regexone.com/

A one-page reference (useful after you've mastered the basics and the jargon) is here:
http://www.cbs.dtu.dk/courses/27610/regular-expressions-cheat-sheet-v2.pdf
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Crazy Horse

ASKER

A lot of really helpful answers here, thank you!
Two of the most important answers were omitted.
https://xkcd.com/1171/
http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags?answertab=oldest#tab-top

Basically it comes down to this.  There is no formal way to test regular expressions.  Instead, you have to "roll your own" tests like those shown in this article.

TL;DR Regular Expressions (indeed, almost any kind of string processing algorithms) are very, very hard to get right.  If they can be avoided, it's probably wise to design an app that can avoid them!  If they can't be avoided, normalize and thereby minimize the string processing.