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

regular expression

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;
public static bool CheckNumber(string strPhoneNumber)
   string MatchPhoneNumberPattern = "^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$";
   if (strPhoneNumber!= null) return Regex.IsMatch(strPhoneNumber, MatchPhoneNumberPattern );
   else return false;


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

Author: Ankur

Have worked primarily in the domain of Calling, CRM and direct advertisers services. My technological forte is Microsoft Technologies especially Dot Net (Visual Studio 2003, 2005, 2008, 2010 and 2012) and Microsoft SQL Server 2000,2005 and 2008 R2. My Area of Expertise is in C#. Net, VB.Net, MS-SQL Server, ASP. Net, Silverlight, HTML, XML, Crystal Report, Active Reports, Infragistics, Component Art, ComponeOne, Lead Tools etc.

19 thoughts on “How to use Regular expression for validating Phone Numbers in .net”

  1. 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  

  2. 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  

  3. 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

  4. 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

Comments are closed.