Others



Validate Phone Number using JavaScript Regular Expression


We have to validate a phone number like

  • 9876543210
  • +9876543210
  • 987 654 3210
  • 987-654-3210
RegEx Fromat
var phoneNumRegex = /^\+?([0-9]{3})\)?[ -]?([0-9]{3})[ -]?([0-9]{4})$/;

The following example shows, how to validate phone number using JavaScript RegEx.

Example
<html>
  <body>
    <input type='text' onkeyup='validate(this)' placeholder='Enter Phone Number'>
    <p id='result'></p>
    <script>
      function validate(phoneNum) {
        var phoneNumRegex = /^\+?([0-9]{3})\)?[ -]?([0-9]{3})[ -]?([0-9]{4})$/;
        if(phoneNum.value.match(phoneNumRegex)) {
        document.querySelector("#result").innerHTML="<span style='color:green;'>Valid</span>";
        }  
        else {  
        document.querySelector("#result").innerHTML="<span style='color:red;'>Invalid</span>";
        }
      }
    </script>
  </body>
</html>
Try it Yourself

Demo