How to use Regular expression for validating Phone Numbers in .net

Regular expressions are a good way to validate text fields such as names, addresses, phone numbers, and other user information. You can use them to constrain input, apply formatting rules, and check lengths.
You can use the System.Text.RegularExpressions.Regex class for validate any input string for any specific format.
A regular expression can easily check whether a user entered something that looks like a valid phone number. By using capturing groups to remember each set of digits, the same regular expression can be used to replace the subject text with precisely the format you want.

The following example will allow you to Input Phone Number only.

In this example we can determine whether a user entered phone number in a common format that includes 0123456789, 012-345-6789, (012)-345-6789, (012)3456789 012 3456789, 012 345 6789, 012 345-6789, (012) 345-6789, 012.345.6789 and all related combinations


using System.Text.RegularExpressions;
  2. public static bool CheckNumber(string strPhoneNumber)
  3. {
  4.    string MatchPhoneNumberPattern = "^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$";
  5.    if (strPhoneNumber!= null) return Regex.IsMatch(strPhoneNumber, MatchPhoneNumberPattern );
  6.    else return false;
  7. }


Import System.Text.RegularExpressions
  2. Public Shared Function CheckPhoneNumber (ByVal strPhoneNumber As String) As Boolean
  3.   Dim MatchPhoneNumberPattern As String = "^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$"      
  4.   If strPhoneNumber IsNot Nothing Then
  5.      Return Regex.IsMatch(strPhoneNumber , MatchPhoneNumberPattern)
  6.   Else
  7.      Return False
  8.   End If
  9. End Function

Similarly if we want to validate phone number like xxx-xxx-xxxx format then you can use “\d{3}-\d{3}-\d{4}”.

Regular expression for phone numbers with country code:

If you want to use regular expression for validating phone or mobile numbers followed by country code. For example if you want to validating Indian mobile numbers followed by +91, then you can use the following regx:
(\+91[\-\s])\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$

Now i am simplifying the above regular expression as ^(\+91[\-\s]?)\d{10}$.

Edited by Hirendra Sisodiya on 2 June, 2012.
Edited by Hirendra Sisodiya on 15 March, 2012.
Edited by Hirendra Sisodiya on 11 Jan, 2012

More on Regular Expressions

  • Mounika

    Hi Ankur,

       Your post was very helpful..
    Can i get reg expression for following two formats plzz…
    1.  +91 then followed by 10 digit no
    2. 080-then followed by 8 digit no  

  • Mounika

    Hi Ankur,
       Your post was very helpful..Can i get reg expression for following two formats plzz…1.  +91 then followed by 10 digit no2. 080-then followed by 8 digit no  

  • hello Munika

    I have updated this article by adding your request, please see in the end of the article.

  • bhushan

    hey friends this is really helpful 😀 but do anyone guide me for email Id validation plz?

  • Bilal yasin

    what will be the method if i want to put (-) this symbol after first four character the next seven digits will be written in aspx file, similarly i want to put (-) in syntax formate for CNIC how i write the regular expression

  • Bilal yasin

    i want to set (xxxx-xxxxxxx) this formate for Cell number in .NET and (xxxxx-xxxxxxxx-x) this formate for CNIC how can i set this formate using aspx