How to Validate International Phone Numbers
In this short article, we'll learn how to validate international phone numbers. Let's get started:
Table of Contents
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:
^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?$
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:
- C#: https://github.com/twcclegg/libphonenumber-csharp
- Go: https://github.com/nyaruka/phonenumbers
- Objective-c: https://github.com/iziz/libPhoneNumber-iOS
- PHP: https://github.com/giggsey/libphonenumber-for-php
- Laravel: https://github.com/Propaganistas/Laravel-Phone
- PostgreSQL in-database types: https://github.com/blm768/pg-libphonenumber
- Python: https://github.com/daviddrysdale/python-phonenumbers
- Ruby: https://github.com/mobi/telephone_number
- Rust: https://github.com/1aim/rust-phonenumber
- Erlang: https://github.com/marinakr/libphonenumber_erlang
- Clojure: https://github.com/randomseed-io/phone-number
- R: https://github.com/socialresearchcentre/dialr/
That's all. Thanks for reading. ?
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)