1

I need regular expression for phone number.

(from 0 to 9) (total 10 digits)
Example: "0123456789"

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
TheChampp
  • 1,337
  • 5
  • 24
  • 41
  • View this Question. It has regex solutions for each and every type of phone number.. http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation?page=1&tab=votes#tab-top – Salman Zaidi Jan 30 '13 at 10:43

3 Answers3

2

its simple:

\d{10}

\d allows digits, and {10} indicates that the phone number must be exactly 10 digits long.

As you mentioned in the comment that you also want a regex for 012-345-6789, (\d{3}-){2}\d{4} would do the work.

(\d{3}-) would validate the first 3 digits and a -

(\d{3}-){2} would look for two occurrence of the above described group. So will validate: 012-345-

And at last \d{4} will look for the last 4 digits

Ali Shah Ahmed
  • 3,263
  • 3
  • 24
  • 22
  • Ok this work. One more example if I want 012-345-6789, what regex should I use ? – TheChampp Jan 30 '13 at 10:41
  • you could go with this regex: (\d{3}-){2}\d{4} (\d{3}-) would validate the first 3 digits and a "-" (\d{3}-){2} would look for two occurrence of the above described group. So will validate: 012-345- And atlast \d{4} will look for 4 digits – Ali Shah Ahmed Jan 30 '13 at 10:44
  • i guess its not visible here. let me make an edit to my answer. – Ali Shah Ahmed Jan 30 '13 at 10:45
2

Use this pattern and match it:

\d{10}

\d : digits only.

{n}: numbers of occurrences.

You can refer this post for more info. Regex for Phone number

Community
  • 1
  • 1
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
2

Normally a \d{10} would do the trick. But that is the lazy-man's way of validating as '0000000000' would pass a a valid entry.

But in case you know more about the domain, and the rules (I mean, if your phone number belongs to a specific country) and you want to make sure the number matches the local rules, then you can be a little bit more specif.

For example if all the numbers are starting with a leading zero, you can do this

0\d{9}

Or if the prefixes are well know... you can make an expression that allows phone numbers only starting with certain prefix(es).

(017|016|019|012)\d{7}

This will allow only those prefixes in the list, plus other 7 digits.

Adrian Salazar
  • 5,279
  • 34
  • 51