## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# 4.3. Validate International Phone Numbers

## Problem

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

## Solution

### Regular expression

^\+(?:[0-9]?){6,14}[0-9]\$
 Regex options: None Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

### JavaScript

function validate (phone) {
var regex = /^\+(?:[0-9] ?){6,14}[0-9]\$/;

if (regex.test(phone)) {
// Valid international phone number
} else {
// Invalid international phone number
}
}

### Other programming languages

See Recipe 3.5 for help implementing this regular expression with other programming languages.

## Discussion

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 preceding 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, 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.

## Variations

### Validate international phone numbers in EPP format

^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?\$
 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.

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