Regular expression matching E.164 formatted phone numbers
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
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!
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/
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.)