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]
- +: Plus sign
- country code: International country code. It comes after the plus sign. For ex. It is +91 for India whereas it is +1 for USA.
- area code: It follows after the international country code. India area codes usually have 2, 3 or 4 digits. For ex. In India, Kolkata has an area code of 3211 and Mumbai has area code of 22.
- local phone number: 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.
- Program:
<!DOCTYPE html><htmllang="en">ÂÂ<head>   Â<metacharset="UTF-8">   Â<metaname="viewport"       Âcontent="width=device-width, initial-scale=1.0">   Â<metahttp-equiv="X-UA-Compatible"       Âcontent="ie=edge">   Â<title>       ÂCell phone number in an International way   Â</title>    Â   Â<style>       Âbody {           Âtext-align: center;       Â}        Â       Âh1 {           Âcolor: green;       Â}   Â</style></head>ÂÂ<body>   Â<h1>neveropen</h1>    Â   Â<h3>       ÂCell phone number in       Âan International way   Â</h3>    Â   Â<script>       Âvar localNumber = prompt("Please enter your number");       Â// Using regular expression to check whether       Â// string is valid or not       Âvar newArray = localNumber.match               Â(/^(91|)?(\d{3})(\d{3})(\d{4})$/);       Â// Checking the international code       Âvar intlCountryCode = (newArray[1] ? '+91' : '');       Â// Resolving the above array we get       Â// the international number       Âvar internationalNumber = intlCountryCode +               Â' (' + newArray[2] + ') ' + newArray[3]               Â+ '-' + newArray[4];       Âdocument.write("The number in international" +                   Â"form is: " + internationalNumber);   Â</script></body>ÂÂ</html> - Output:
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.
- Program:
<!DOCTYPE html><htmllang="en"><head>   Â<metacharset="UTF-8">   Â<metaname="viewport"       Âcontent="width=device-width, initial-scale=1.0">   Â<metahttp-equiv="X-UA-Compatible"       Âcontent="ie=edge">   Â<title>       ÂCell phone number in an International way   Â</title>    Â   Â<style>       Âbody {           Âtext-align: center;       Â}        Â       Âh1 {           Âcolor: green;       Â}   Â</style></head><body>   Â<h1>neveropen</h1>   Â<h3>       ÂCell phone number in       Âan International way   Â</h3>    Â   Â<script>    Â   Âvar localNumber= prompt("Please enter your number");    Â   Â// Using regular expression to check   Â// whether string is valid or not   Âvar newArray = localNumber.match               Â(/^(91|)?(\d{3})(\d{3})(\d{4})$/);    Â   Â// Checking the international code   Âvar intlCountryCode=(newArray[1]?'+91':'');    Â   Â// Resolving the above array we get   Â// the international number   Âvar internationalNumber = intlCountryCode + ' ('               Â+ newArray[2] + ') ' + newArray[3]               Â+ '-' + newArray[4];    Â   Âdocument.write("The international number is: "               Â+ internationalNumber);   Â</script></body>ÂÂ</html> - Output:

