Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Regex to validate phone number

Tags:

c#

regex

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

like image 745
Derin Avatar asked Jan 18 '12 11:01

Derin


2 Answers

Try with:

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

However it does not handle number counting

like image 157
hsz Avatar answered Oct 14 '22 01:10

hsz


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.

like image 34
Dmitry Avatar answered Oct 14 '22 02:10

Dmitry