To Validate US Phone(North) in Java Script

This code shows how To Validate US Phone(North) in Java Script


function validatePhone(phoneField, format)
{
var num = phoneField.value.replace(/[^\d]/g,'');
if(num.length != 10) {

//Alert the user that the phone number entered was invalid.

alert('Please enter a valid phone number including area code');
document.getElementById("field2").value="(xxx)xxx-xxxx";

return false;
}

else {

switch(format)
{

case '0': //Format (xxx)-xxx-xxxx

phoneField.value = "(" + num.substring(0,3) + ")-" + num.substring(3, 6) +

"-" + num.substring(6);

break;

case '1': //Format xxx-xxx-xxxx

phoneField.value = num.substring(0,3) + "-" + num.substring(3, 6) + "-" +

num.substring(6);

break;
default: //Format xxxxxxxxxx
phoneField.value = num;
break;
}
return true;
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: