16

It would be great if someone could help me with a Regex for phone numbers. Following are the conditions:

  • If + is present, then it should be the first character
  • Allowed characters are numbers ( ) space - and .
  • Minimum of 6 numbers and max 12
  • ( , ) and space can come anywhere in the string
  • - shouldn't be the first and last character and shouldn't appear immediately after +, if + is present.

Here are some valid numbers:

  • +93483227359
  • +1 703 335 65123
  • 34565464
  • 001 (703) 332-6261
  • +1703.338.6512
  • +934-83227359
  • (111)123-4567
  • 111-123-4567

Thanks in advance

Derin
  • 2,125
  • 7
  • 28
  • 31
  • 1
    Just to check, are you sure you want "+)-)-)333(((,111" to be a valid phone number? – Jon Skeet Jan 18 '12 at 11:04
  • @JonSkeet well, I'm pretty sure `tel:+)-)-)333(((,111` is a valid tel URI, so one could argue that if it's good enough for RFC 3966 then... :) I'd be more worried that the min and max number of digits is Internationally valid. – Jon Hanna Jan 18 '12 at 11:09
  • Indeed, my own is 12digits after the +, so if given in the form local to a given country it would be at least 13digits long. – Jon Hanna Jan 18 '12 at 11:21

2 Answers2

26

Try with:

^\+?(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d$

However it does not handle number counting

hsz
  • 148,279
  • 62
  • 259
  • 315
4

Not exactly the answer to your question, but for those who need to work with phone numbers, there is a .NET port of Google's libphonenumber: libphonenumber-csharp.

Dmitry
  • 17,078
  • 2
  • 44
  • 70