How to Validate International Phone Numbers

Published on Feb 19, 2022

In this short article, we'll learn how to validate international phone numbers. Let's get started:

Table of Contents

  1. Using Regex
  2. Using Package

Using Regex

We can easily validate international phone number using regular expression. It works for most of the programming langauges such as .NET, Java, JavaScript, PCRE, Perl, Python, Ruby etc. The numbers should start with a plus sign, followed by the country code and national number.

Have a look at an example:

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

    if (regex.test(phone)) {
        console.log("phone number is valid");
    } else {
        console.log("phone number is invalid");

Regex explanation:

^ # 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.

Validate international phone numbers in EPP format using Regex:


Using Package

There is an awesome package provided by Google to verify phone number. The package is libphonenumber. Take a look at their docs and third party packages.

Here are some third party packages made with libphonenumber:

That's all. Thanks for reading. ?

Comments (0)