Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App

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

Last Updated : 31 Dec, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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: html
    <!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <meta name="viewport"         content="width=device-width, initial-scale=1.0">     <meta http-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>GeeksforGeeks</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: html
    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta name="viewport"         content="width=device-width, initial-scale=1.0">     <meta http-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>GeeksforGeeks</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:

R

RitikGarg2
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Misc

Similar Reads

    JavaScript - Convert a Number into JS Array
    You have a number, like 12345, and you need to convert it into an array where each element represents a digit of the number. For example, 12345 should become [1, 2, 3, 4, 5]. How can you achieve this in JavaScript?In JavaScript, there are various ways to transform a number into an array of its digit
    3 min read
    How to generate a n-digit number using JavaScript?
    The task is to generate an n-Digit random number with the help of JavaScript. You can also generate random numbers in the given range using JavaScript. Below are the approaches to generate a n-digit number using JavaScript: Table of Content Using Math.random()Math.random() Method and .substring() Me
    2 min read
    How to get decimal portion of a number using JavaScript ?
    Given a float number, The task is to separate the number into integer and decimal parts using JavaScript. For example, a value of 15.6 would be split into two numbers, i.e. 15 and 0.6 Here are a few methods discussed. These are the following methods: Table of Content Javascript String split() Method
    3 min read
    Generate Random Number in Given Range Using JavaScript
    Here are the different ways to generate random numbers in a given range using JavaScript1. Using Math.random(): basic ApproachThis is the simplest way to generate a random number within a range using Math.random().JavaScriptlet min = 10; let max = 20; let random = Math.floor(Math.random() * (max - m
    3 min read
    How to convert long number into abbreviated string in JavaScript ?
    We are given a long number and the task is to convert it to the abbreviated string(eg.. 1234 to 1.2k). Here 2 approaches are discussed with the help of JavaScript.Approaches to Convert Long Number to Abbreviated String:Table of ContentUsing JavaScript methodsUsing Custom functionUsing logarithmsUsin
    6 min read
    How to check first number is divisible by second one in JavaScript ?
    Given two numbers and the task is to check the first number is divisible by the second number or not with the help of JavaScript. Before getting into the coding part, first let us know about the modulo operator and triple equals. To find a number is divisible by another or not, we simply use the rem
    2 min read
    JavaScript - How to Get a Number of Vowels in a String?
    Here are the various methods to get the number of vowels in a string using JavaScript.1. Using a for LoopThis is the most basic and beginner-friendly approach. It uses a loop to iterate over each character and checks if it is a vowel.JavaScriptconst cVowels = (s) => { const vowels = "aeiouAEIOU";
    3 min read
    How to limit a number between a min/max value in JavaScript ?
    We can limit a number between a min/max value using the is-else condition and using the Math.min and Math.max methods of JavaScript.Below are the approaches to limit a number between a min/max value in JavaScript:Table of ContentUsing the if-else conditionUsing the Math.min() and Math.max() methodsU
    2 min read
    How to convert a pixel value to a number value using JavaScript ?
    In this article, we will see how to convert the string value containing 'px' to the Integer Value with the help of JavaScript. There are two approaches to converting a pixel value to a number value, these are: Using parseInt() MethodUsing RegExpApproach 1: Using parseInt() Method This method takes a
    2 min read
    How to count number of data types in an array in JavaScript ?
    Given an array and the task is to count the number of data types used to create that array in JavaScript. Example: Input: [1, true, "hello", [], {}, undefined, function(){}] Output: { boolean: 1, function: 1, number: 1, object: 2, string: 1, undefined: 1 } Input: [function(){}, new Object(), [], {},
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences