How to format a phone number in Human-Readable using JavaScript ?

Give a phone number and the task is to format a phone number in such a way that it becomes easy to understand for humans. There are two approaches to format the numbers which are discussed below:

Approach 1:

  • A RegExp is used to replace the original phone number by human-readable phone number.
  • The RegExp is searching for the 3 digits and saving it in a variable($1 for first 3 digits, $2 for second 3 digits and so on.)
  • At the end, It is just concatenating them with ‘-‘ among them.

Example: This example implements the above approach.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>  
<html>  
  
<head>  
    <title>  
        How to format a phone number in
        Human-Readable using JavaScript?
    </title>
      
    <script src=
    </script>
</head>  
  
<body style = "text-align:center;">  
      
    <h1 style = "color:green;" >  
        GeeksForGeeks  
    </h1
      
    <p id = "GFG_UP" style
        "font-size: 15px; font-weight: bold;"
    </p>
      
    <button onclick = "GFG_Fun()">
        click here
    </button>
      
    <p id = "GFG_DOWN" style
        "font-size: 24px; font-weight: bold; color: green;"
    </p>
      
    <script>  
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
        var phoneNo = '4445556678';
          
        up.innerHTML = "Click on the button to format "
            + "the phone number.<br>Phone No: " + phoneNo; 
          
        function GFG_Fun() {
            down.innerHTML = 
                phoneNo.replace(/(\d{3})(\d{3})(\d{4})/,
                                '$1-$2-$3');
        
    </script>  
</body>  
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Approach 2:

  • This example is using the substr() method to format a phone number in Human-Readable format.
  • It is taking a substrings of length 3 and putting ‘-‘ among them. Remaining string elements using the same method.

Example 2: This example implements the above approach.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>  
<html>  
  
<head>  
    <title>  
        How to format a phone number in
        Human-Readable using JavaScript?
    </title>
      
    <script src=
    </script>
</head>  
  
<body style = "text-align:center;">  
      
    <h1 style = "color:green;" >  
        GeeksForGeeks  
    </h1
      
    <p id = "GFG_UP" style
        "font-size: 15px; font-weight: bold;"
    </p>
      
    <button onclick = "GFG_Fun()">
        click here
    </button>
      
    <p id = "GFG_DOWN" style
        "font-size: 24px; font-weight: bold; color: green;"
    </p>
      
    <script>  
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
        var phoneNo = '4445556678';
          
        up.innerHTML = "Click on the button to format "
            + "the phone number.<br>Phone No: " + phoneNo; 
          
        function GFG_Fun() {
            down.innerHTML = 
                down.innerHTML = phoneNo.substr(0, 3)
                         + '-' + phoneNo.substr(3, 3)
                         + '-' + phoneNo.substr(6, 4);
        
    </script>  
</body>  
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.