I need to determine whether a phone number is valid before attempting to dial it. The phone call can go anywhere in the world.
What regular expression will match valid international phone numbers?
I need to determine whether a phone number is valid before attempting to dial it. The phone call can go anywhere in the world.
What regular expression will match valid international phone numbers?
All country codes are defined by the ITU. The following regex is based on ITU-T E.164 and Annex to ITU Operational Bulletin No. 930 – 15.IV.2009. It contains all current country codes and codes reserved for future use. While it could be shortened a bit, I decided to include each code independently.
This is for calls originating from the USA. For other countries, replace the international access code (the 011 at the beginning of the regex) with whatever is appropriate for that country's dialing plan.
Also, note that ITU E.164 defines the maximum length of a full international telephone number to 15 digits. This means a three digit country code results in up to 12 additional digits, and a 1 digit country code could contain up to 14 additional digits. Hence the
[0-9]{0,14}$
a the end of the regex.
Most importantly, this regex does not mean the number is valid - each country defines its own internal numbering plan. This only ensures that the country code is valid.
^011(999|998|997|996|995|994|993|992|991| 990|979|978|977|976|975|974|973|972|971|970| 969|968|967|966|965|964|963|962|961|960|899| 898|897|896|895|894|893|892|891|890|889|888| 887|886|885|884|883|882|881|880|879|878|877| 876|875|874|873|872|871|870|859|858|857|856| 855|854|853|852|851|850|839|838|837|836|835| 834|833|832|831|830|809|808|807|806|805|804| 803|802|801|800|699|698|697|696|695|694|693| 692|691|690|689|688|687|686|685|684|683|682| 681|680|679|678|677|676|675|674|673|672|671| 670|599|598|597|596|595|594|593|592|591|590| 509|508|507|506|505|504|503|502|501|500|429| 428|427|426|425|424|423|422|421|420|389|388| 387|386|385|384|383|382|381|380|379|378|377| 376|375|374|373|372|371|370|359|358|357|356| 355|354|353|352|351|350|299|298|297|296|295| 294|293|292|291|290|289|288|287|286|285|284| 283|282|281|280|269|268|267|266|265|264|263| 262|261|260|259|258|257|256|255|254|253|252| 251|250|249|248|247|246|245|244|243|242|241| 240|239|238|237|236|235|234|233|232|231|230| 229|228|227|226|225|224|223|222|221|220|219| 218|217|216|215|214|213|212|211|210|98|95|94| 93|92|91|90|86|84|82|81|66|65|64|63|62|61|60| 58|57|56|55|54|53|52|51|49|48|47|46|45|44|43| 41|40|39|36|34|33|32|31|30|27|20|7|1)[0-9]{0, 14}$
You can use the library libphonenumber from Google.
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
String decodedNumber = null;
PhoneNumber number;
try {
number = phoneNumberUtil.parse(encodedHeader, null);
decodedNumber = phoneNumberUtil.format(number, PhoneNumberFormat.E164);
} catch (NumberParseException e) {
e.printStackTrace();
}
No criticism regarding those great answers I just want to present the simple solution I use for our admin content creators:
^(\+|00)[1-9][0-9 \-\(\)\.]{7,32}$
Force start with a plus or two zeros and use at least a little bit of numbers. White space, brackets, minus and point are optional, no other characters allowed.
You can safely remove all non-numbers and use this in a tel: input. Numbers will have a common form of representation and I do not have to worry about being to restrictive.
Here's an "optimized" version of your regex:
^011(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|
2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|
4[987654310]|3[9643210]|2[70]|7|1)\d{0,14}$
You can replace the \ds with [0-9] if your regex syntax doesn't support \d.
I made the regexp for european phone numbers, and it is specific against dial prefix vs length of number.
const PhoneEuropeRegExp = () => {
// eu phones map https://en.wikipedia.org/wiki/Telephone_numbers_in_Europe
const phonesMap = {
"43": [4, 13],
"32": [8, 10],
"359": [7, 9],
"385": [8, 9],
"357": 8,
"420": 9,
"45": 8,
"372": 7,
"358": [5, 12],
"33": 9,
"350": 8,
"49": [3, 12],
"30": 10,
"36": [8, 9],
"354": [7, 9],
"353": [7, 9],
"39": [6, 12],
"371": 8,
"423": [7, 12],
"370": 8,
"352": 8,
"356": 8,
"31": 9,
"47": [4, 12],
"48": 9,
"351": 9,
"40": 9,
"421": 9,
"386": 8,
"34": 9,
"46": [6, 9],
};
const regExpBuilt = Object.keys(phonesMap)
.reduce(function(prev, key) {
const val = phonesMap[key];
if (Array.isArray(val)) {
prev.push("(\\+" + key + `[0-9]\{${val[0]},${val[1]}\})`);
} else {
prev.push("(\\+" + key + `[0-9]\{${val}\})`);
}
return prev;
}, [])
.join("|");
return new RegExp(`^(${regExpBuilt})$`);
};
alert(PhoneEuropeRegExp().test("+420123456789"))
It works pretty well with 00xx and +xx:
^(?:00|\+)(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$
The international numbering plan is based on the ITU E.164 numbering plan. I guess that's the starting point to your regular expression.
I'll update this if I get around to create a regular expression based on the ITU E.164 numbering.
I only check for valid characters and allow up to 30 characters. Numbers that include an extension are also possible.
^[\+\(\s.\-\/\d\)]{5,30}$
Matches the following:
(0123) 123 456 1
555-555-5555
0049 1555 532-3455
123 456 7890
0761 12 34 56
+49 123 1-234-567-8901
+61-234-567-89-01
+46-234 5678901
+1 (234) 56 89 901
+1 (234) 56-89 901
+46.234.567.8901
+1/234/567/8901