Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate Phone Number

Status
Not open for further replies.

CoffeeQuick

Programmer
Jul 6, 2004
23
0
0
GB
Hi,

I came across this script the other day for validating phone/fax numbers, it's about the best one i've seen, although it had a few bugs in it, which i've ironed out (exp originally came up with a syntax error, it had () where the shouldn't have been, and didn't like properly formated London numbers eg 020 8231 9331).

It allows for international numbers and for extension numbers added at the end.

So I thought I would share it with you.

Here's the code.

Code:
function validPhoneNumber(formField) 
{
  var phone = formField.value;
  var exp = /^(\+\d{1,3} ?)?(\(\d{1,5}\)|\d{1,5}) ?\d{3,4} ?\d{0,7} ?(x|xtn|ext|extn|extension)??\.? ?\d{1,5}?$/i;
  var isValid = exp.test(phone);

  if (!isValid) {
    alert("Please enter valid telephone number");
    formField.focus();
    }
  return isValid;
}

Hope this is of use to someone.

John[atom]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top