web analytics

Validate International Phone Numbers

by | Apr 9, 2019 | Articles | 0 comments

You want to validate international phone numbers. The numbers should start with a plus sign, followed by the country code and national number.

Regular expression
Regex options: None
Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby
JavaScript example
function validate(phone) {
var regex = /^\+(?:[0-9] ?){6,14}[0-9]$/;

if (regex.test(phone)) {
// Valid international phone number
} else {
// Invalid international phone number
Follow Recipe 3.6 to implement this regular expression with other programming languages.

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. Although the tilde character (~) can appear within a phone number to indicate the existence of an additional dial tone, it has been excluded from this regular expression since it is merely a procedural element (in other words, it is not actually dialed) and is infrequently used. 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.

With all of this in mind, let’s look at the regular expression again after breaking it into its pieces. Because this version is written using free-spacing style, the literal space character has been replaced with ‹\x20›:

^ # Assert position at the beginning of the string.
\+ # Match a literal “+” character.
(?: # Group but don’t capture:
[0-9] # Match a digit.
\x20 # 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.
Regex options: Free-spacing
Regex flavors: .NET, Java, XRegExp, PCRE, Perl, Python, Ruby
The ‹^› and ‹$› anchors at the edges of the regular expression ensure that it matches the whole subject text. The noncapturing group, enclosed with ‹(?:⋯)›, matches a single digit followed by an optional space character. Repeating this grouping with the interval quantifier ‹{6,14}› enforces the rules for the minimum and maximum number of digits, while allowing space separators to appear anywhere within the number. The second instance of the character class ‹[0-9]› completes the rule for the number of digits (bumping it up from between 6 and 14 digits to between 7 and 15), and ensures that the phone number does not end with a space.

Validate international phone numbers in EPP format
Regex options: None
Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby
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.

See Also
Recipe 4.2 provides more options for validating North American phone numbers.

ITU-T Recommendation E.123 (“Notation for national and international telephone numbers, e-mail addresses and web addresses”) can be downloaded at http://www.itu.int/rec/T-REC-E.123. ITU-T Recommendation E.164 (“The international public telecommunication numbering plan”) can be downloaded at http://www.itu.int/rec/T-REC-E.164. National numbering plans can be downloaded at http://www.itu.int/ITU-T/inr/nnp/.

RFC 5733 defines the syntax and semantics of EPP contact identifiers, including international phone numbers. You can download RFC 5733 at http://tools.ietf.org/html/rfc5733.

Techniques used in the regular expressions in this recipe are discussed in Chapter 2. Recipe 2.1 explains which special characters need to be escaped. Recipe 2.3 explains character classes. Recipe 2.5 explains anchors. Recipe 2.9 explains grouping. Recipe 2.12 explains repetition.

Join Today

Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Donec rutrum congue.

Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus. Donec sollicitudin molestie malesuada. Pellentesque in ipsum id orci porta dapibus. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Quisque velit nisi.

By NettSite

Curabitur aliquet quam id dui posuere blandit. Proin eget tortor risus eget tortor.

Check Out These Related Posts

Are Your Children Safe Online?

Are Your Children Safe Online?

Password education should be age-appropriate: here's how Our set of best practice principles should help adults to teach the principles to children in an age-appropriate way. Shutterstock Karen Renaud, Rhodes University and Suzanne Prior, Abertay University Children...

read more



Submit a Comment

Your email address will not be published. Required fields are marked *

WordPress Anti-Spam by WP-SpamShield