Regular expression matching E.164 formatted phone numbers

Regex

Regex Problem Overview


I need to add a regular expression that matches all possible valid E.164 formatted phone numbers.

This regex works fine for for North American phone numbers, but I need something that will work for international numbers as well:

> ^(+1|1)?([2-9]\d\d[2-9]\d{6})$

> Example: +13172222222 matches > 13172222222 still matches because +1 or 1 are optional > 3172222222 still matches because +1 or 1 are optional > 3171222222 does not match and is not a valid NANPA number.

Source: Freeswitch.org

I also came across this related question, but I think that it is way too crazy for my needs. In my case, I am simply validating an entry for a blacklist, which I'm comparing to incoming Twilio data; so, I care much less about weather a country code is valid. I really only need to test if a number matches the general E.164 form, rather than assuming it's a NANPA.

To be better understand what I need to match, here is an example from the Twilio Documentation:

> All phone numbers in requests from > Twilio are in E.164 format if > possible. For example, (415) 555-4345 > would come through as '+14155554345'. > However, there are occasionally cases > where Twilio cannot normalize an > incoming caller ID to E.164. In these > situations Twilio will report the raw > caller ID string.

I want to match something like +14155554345, but not (415) 555-4345, 415555434, 555-4345 or 5554345. The regex should not restrict itself to only matching the US country code though. Basically, it should match the +xxxxxxxxxxx format. I also think the number could be longer, as there are multi-digit country codes, such as in the UK. T-Mobile's UK number is +447953966150 I'll update this if I can come up with a better example.

Regex Solutions


Solution 1 - Regex

The accepted answer is good, except an E.164 number can have up to 15 digits. The specification also doesn't indicate a minimum, so I wouldn't necessarily count on 10.

It should be ^\+?[1-9]\d{1,14}$

See http://en.wikipedia.org/wiki/E.164

Solution 2 - Regex

I think until you have a great set of examples, you are best served by a flexible regex. This one will match a + followed by 10-14 digits.

^\+?\d{10,14}$

Broken down, this expression means: ^ Match begining of string. \+? Optionally match a + symbol. \d{10,14} Match between 10 and 14 digits. $ Ensure we are at the end of the string.

If you learn that a digit at a particular index must not be 1 or 0, then you can use the [2-9] at that position, like this:

^\+?\d{6,7}[2-9]\d{3}$

[2-9] means match any digit from 2 through 9 (don't match 0 or 1.)

Solution 3 - Regex

Well, I used the accepted answer but it failed for many cases:

For inputs like:

  • Where numbers did not start with "+".
  • Where number count was less than 9.

the regex failed.

I finally used

^\+(?:[0-9]?){6,14}[0-9]$

This worked like a charm!

Solution 4 - Regex

Typescript/Javascript : E.164

This works for me:

   static PHONE_NUMBER = /^\+[1-9]\d{10,14}$/; // E.164

   PHONE_NUMBER.test('+' + countryCode + phoneNumber);

Reference: https://blog.kevinchisholm.com/javascript/javascript-e164-phone-number-validation/

Solution 5 - Regex

This matches only formats like +16174552211 and 16174552211

/\A\+?\d{11}\z/

It is especially useful if you are using Twilio and Ruby on Rails

Solution 6 - Regex

The regex you've provided should work except that the initial + needs to be escaped.

/^(\+1|1)?[2-9]\d\d[2-9]\d{6}$/g

See it working at http://refiddle.com/19x

Solution 7 - Regex

The accepted answer doesn't work with numbers without '+' sign. And I did a little math following the Wikipedia metrics, Country Code : 1 to 3 digits, maximum : 15, Actual Phone Number : 12 (upon 15-3) to 14 (15-1) digits

The minimum in this regard is 10. For instance, the dummy US number "+14155552671" is the bare minimum. Breaking it down, +1 is US Country Code and the rest is all 'Area Code' + 'Subscriber Number' which is going to be 10. I couldn't find, in my research, a number less than 7 digits (Sweden) and is valid.

So the regex I had to come up with that works along with '+' sign along with the digits which much reside between 10~15 is as follows:

> ^\++?[1-9][0-9]\d{6,14}$

And this works well. You can check it out on Regex101.

Solution 8 - Regex

This RegEx ^\\+?[0-9]{1,3}[ 1-9]\\d{1,14}$ also works without Invalid regular expression.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSean W.View Question on Stackoverflow
Solution 1 - RegexGraham DavisonView Answer on Stackoverflow
Solution 2 - Regexagent-jView Answer on Stackoverflow
Solution 3 - RegexswateekView Answer on Stackoverflow
Solution 4 - RegexSampathView Answer on Stackoverflow
Solution 5 - RegexmaudulusView Answer on Stackoverflow
Solution 6 - RegexPaul AlexanderView Answer on Stackoverflow
Solution 7 - RegexShayan AhmadView Answer on Stackoverflow
Solution 8 - RegexFarhaanView Answer on Stackoverflow