81

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.

Community
  • 1
  • 1
Sean W.
  • 4,944
  • 8
  • 40
  • 66
  • I've noticed that people are most willing to help when you provide examples of what you are trying to match... At least explain what is different between the input that you know how to match and the input you don't. – agent-j Jun 25 '11 at 20:16
  • Sorry it took so ling for me to find an example. I couldn't find any clear documentation on E.164 format, so I've included the Twilio blurb on them. – Sean W. Jun 25 '11 at 23:39
  • These still look like USA phone numbers. The regular expression from Freeswich.org matches the one you +14155554345 (well, if you escape the first `+` with `\+`). `^(\+1|1)?([2-9]\d\d[2-9]\d{6})$` – agent-j Jun 25 '11 at 23:53
  • @agent-j I added a UK example. – Sean W. Jun 26 '11 at 00:00
  • For me the tricky part is extensions .. what if they put "+1 (999)-999-999 ext. 191" or "11 99 8888 8888 extension 9909" ? – Mike Graf Jul 30 '13 at 21:02

8 Answers8

142

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

muttonUp
  • 6,351
  • 2
  • 42
  • 54
Graham Davison
  • 1,579
  • 1
  • 10
  • 3
  • 29
    New Zealand service numbers can be as short as 5 digits, including the country code, eg.: +64010. Perhaps the answer should be updated to `^\+?\d{5,15}$`? Or even better: `^\+?[1-9]\d{4,14}$` to take into account the fact that there are no country codes starting with 0. Personally, I'd use `^\+?[1-9]\d{1,14}$` since there is no minimum length for subscriber number defined in E.164. – Sergei Feb 11 '15 at 08:55
  • Yes, that's even better. I didn't think of the special case on the first digit. – Graham Davison Feb 13 '15 at 00:33
  • 1
    Perhaps, update your answer then? Will be easier for others to get the regex they're looking for. – Sergei Feb 13 '15 at 07:27
  • Additional note: if your regular expressions do not support the "\d" shortcut, use "[0-9]" instead. – VasiliNovikov Mar 03 '15 at 15:02
  • for E.164 validation use /^\+[0-9]{1,3}\.[0-9]{1,14}$/ e.g. +27.235682811 – Louis Ferreira Apr 18 '17 at 10:53
  • @LouisFerreira . (periods) are not part of the E.164 spec. – dbinott Jan 02 '19 at 02:39
  • 28
    The + is not optional in E.164 for services like Twilio, so remove the ? after the + ^\+[1-9]\d{1,14}$ – dbinott Jan 02 '19 at 02:55
17

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

agent-j
  • 27,335
  • 5
  • 52
  • 79
  • Thanks! That's exactly what I needed for now. I will answer this question myself if I happen to learn more about number formatting along the way. – Sean W. Jun 26 '11 at 00:30
  • "As described in by the ITU, the E.164 general format must contain only digits split as follows: - Country code (1 to 3 digits) - Subscriber number (max 12 digits)" It should be `d{10,15}` – warvariuc Aug 19 '20 at 07:26
4

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/

Sampath
  • 63,341
  • 64
  • 307
  • 441
3

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!

swateek
  • 6,735
  • 8
  • 34
  • 48
1

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

maudulus
  • 10,627
  • 10
  • 78
  • 117
0

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.

Shayan Ahmad
  • 952
  • 8
  • 17
  • This doesn't match for numbers w/o a plus sign either. 16135551212 And why are you allowing unlimited +? Also the + is typically needed for services like Twilio. Country codes also do not start with a 0. – dbinott Jan 02 '19 at 02:56
0

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

4b0
  • 21,981
  • 30
  • 95
  • 142
Farhaan
  • 75
  • 1
  • 4
0

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
greg-449
  • 109,219
  • 232
  • 102
  • 145
Paul Alexander
  • 31,970
  • 14
  • 96
  • 151
  • Thanks for the information and the link to link to refiddle; what a useful site! I ended up needing regex that matches more than just US numbers. The confusion was caused when I initially only provided examples in the form of US numbers, as those were the only formatted examples that I could find at the time. The regex above is still useful for anyone wanting to match E.164 formatted US numbers. – Sean W. Jun 26 '11 at 00:27