Javascript Regex - What to use to validate a phone number?

user2031340 picture user2031340 · Feb 1, 2013 · Viewed 40.5k times · Source

Could anyone tell me what RegEx would work to validate an international phone number including white space between the numbers and also allowing for these chars: - ( ).

The amount of numbers in the string is not too important, I would just like the user to be able to type something like either example 1 or 2 if they wish:

Example:

  1. +44 (0) 207 111 1111

  2. 442071111111

I have already read through and tested the posted answers to some similar questions to the best of my understanding but so far none of them are working for me the way I want them to.

Please can someone help me out with a clear explanation of how the above should be written for validation?

Many thanks to anyone who can help.

Answer

Sagar Hirapara picture Sagar Hirapara · Feb 1, 2013

Try this code

HTML Code

<input type="text" id="phone"/>

JS Code

$("#phone").blur(function() {
  var regexp = /^[\s()+-]*([0-9][\s()+-]*){6,20}$/
  var no = $("#phone").val();
  if (!regexp.test(no) && no.length < 0) {
    alert("Wrong phone no");
  }
});