In this regex tutorial, we will learn to validate international phone numbers based on industry-standard notation specified by ITU-T E.123.
The rules and conventions used to print international phone numbers vary significantly around the world, so it’s hard to provide meaningful validation for an international phone number unless you adopt a strict format. Fortunately, there is a simple, industry-standard notation specified by ITU-T E.123. This notation requires that international phone numbers include a leading plus sign (known as the international prefix symbol), and allows only spaces to separate groups of digits.
Also thanks to the international phone numbering plan (ITU-T E.164), phone numbers cannot contain more than 15 digits. The shortest international phone numbers in use contain seven digits.
1. Regex for Matching International Phone Numbers
Regex : ^\+(?:[0-9] ?){6,14}[0-9]$
^ # Assert position at the beginning of the string. \+ # Match a literal "+" character. (?: # Group but don't capture: [0-9] # Match a digit. \\s # Match a space character ? # between zero and one time. ) # End the noncapturing group. {6,14} # Repeat the group between 6 and 14 times. [0-9] # Match a digit. $ # Assert position at the end of the string.
The above regular expression can be used to validate international phone numbers based on ITU-T standards. Let’s look at one example.
List phoneNumbers = new ArrayList();
phoneNumbers.add("+1 1234567890123");
phoneNumbers.add("+12 123456789");
phoneNumbers.add("+123 123456");
String regex = "^\\+(?:[0-9] ?){6,14}[0-9]$";
Pattern pattern = Pattern.compile(regex);
for(String email : phoneNumbers)
{
Matcher matcher = pattern.matcher(email);
System.out.println(email +" : "+ matcher.matches());
}
The program output:
+1 1234567890123 : true
+12 123456789 : true
+123 123456 : true
2. Regex for International Phone Numbers in EPP format
This regular expression follows the international phone number notation specified by the Extensible Provisioning Protocol (EPP). EPP is a relatively recent protocol (finalized in 2004), designed for communication between domain name registries and registrars. It is used by a growing number of domain name registries, including .com, .info, .net, .org, and .us. The significance of this is that EPP-style international phone numbers are increasingly used and recognized, and therefore provide a good alternative format for storing (and validating) international phone numbers.
EPP-style phone numbers use the format +CCC.NNNNNNNNNNxEEEE, where C is the 1–3 digit country code, N is up to 14 digits, and E is the (optional) extension. The leading plus sign and the dot following the country code are required. The literal “x” character is required only if an extension is provided.
Regex : ^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?$
List phoneNumbers = new ArrayList();
phoneNumbers.add("+123.123456x4444");
phoneNumbers.add("+12.1234x11");
phoneNumbers.add("+1.123456789012x123456789");
String regex = "^\\+[0-9]{1,3}\\.[0-9]{4,14}(?:x.+)?$";
Pattern pattern = Pattern.compile(regex);
for(String email : phoneNumbers)
{
Matcher matcher = pattern.matcher(email);
System.out.println(email +" : "+ matcher.matches());
}
The program output:
+123.123456x4444 : true
+12.1234x11 : true
+1.123456789012x123456789 : true
Feel free to edit the above regex and play with it to match the more strict phone number formats, you have in your mind.
Happy Learning !!
Comments