Regular expression for US and Canada phone number

by

in ,
Regular Expressions
Regular Expressions (Photo credit: Jeff Kubina)

This a quick post regarding how to validate US and Canada phone number in very possible formats.

Requirements:

To determine whether a user entered a North American phone number in a common format, including the local area code. The supported formats are 1234567890, 123-456-7890, 123.456.7890,  123 456 7890, (123) 456 7890, and all related combinations. If the phone number is valid, we will convert that our standard format,  (123) 456-7890, so that the phone number records are consistently recorded.

Suggested expression: ^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$

Valid phone number series for the expression:

  • 123-456-7890
  • 123 456-7890
  • 123-456 7890
  • (123)-456-7890
  • 123 456 7890

 

.NET C# Code Snippet

string userInput = txtPhoneNumber.Text;

Regex regexPhoneNumber = new Regex(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$");

if (regexPhoneNumber.IsMatch(userInput))
{
	string formattedPhoneNumber = regexPhoneNumber.Replace(userInput, "($1) $2-$3");
}
else
{
	// Invalid phone number
}

The following layout breaks the regular expression into its individual parts, excluding the repeating groups of digits:

^ Assert position at the beginning of the string.
\( Match a literal “(“
  ? between zero and one time.
( Capture the enclosed match to back reference 1
  [0-9] Match a digit
{3} exactly three times.
) End capturing group 1.
\) Match a literal “)”
? between zero and one time.
[-. ] Match one character from the set “-. “
? between zero and one time.
? [Match the remaining digits and separator.]
$ Assert position at the end of the string.

Here, ‘^‘ and ‘$‘ are meta-characters named an anchor or assertion. ‘^ is used to mask the start of regular expression and $ is used to mark the end. ‘$’ is used to stop matching too much string into final result than required.

 

For people seeking more information regarding the expression are suggested to visit detailed post here: http://blog.stevenlevithan.com/archives/validate-phone-number


Comments

4 responses to “Regular expression for US and Canada phone number”

  1. shubham jain Avatar
    shubham jain

    Thanks for regular expression.

    I have modified your regular expression that works for following US phone number.

    regExp -> /^[+]?[0-9]{0,1}[-. ]?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/

    1444-555-1234
    1-444-555-1234
    14325678901
    1(123)456-7890
    +1 246.555-8888
    +1 (123)456-7890
    +1(123)456-7890

    Thanks,
    Shubham

  2. manmohan Avatar
    manmohan

    Thank you Subham it helped me a lot

  3. This works for the format, doesn’t actually ensure valid phone numbers. See http://www.regexplanet.com/cookbook/phone-number-nanp/index.html

  4. Tyler Breau Avatar
    Tyler Breau

    This regex is flawed. (123-456-7890 and 123)-456-7890 passes as a valid format.

Leave a Reply

Your email address will not be published. Required fields are marked *