Open In App

How to write a cell phone number in an international way using JavaScript ?

E.164 format is used to convert a phone number into an international format. It is an internationally recognized standard that defines a general numbering plan.

The international number format according to E.164 is as follows:



[+][country code][area code][local phone number]

Prerequisite article: How to write Regular Expressions?

Concept of regular expressions is used for converting a cell phone number into international way. Regular expressions are a generalized way to match patterns with sequences of characters.
Some examples according to the E.164 format



Without international code 
localNumber: 9760064000
intlNumber: (976) 006-4000

With international code 
localNumber: 919760064000
intlNumber: +91 (976) 006-4000 

Example 1: This code uses the regular expression /^(\d{3})(\d{3})(\d{4})$/ for validating the phone numbers. If the number is found to be valid then an array will be returned and if not then null will be returned. The elements of the returned array is then joined according to the E.164 format for international number.

Example 2: This code uses the regular expression /^(91|)?(\d{3})(\d{3})(\d{4})$/ for validating the phone numbers with international code. The same approach as above is followed to get the phone number in international format.


Article Tags :