I have this to check my phone number entries. Please advise if I can improve this. Right now it does check and verify phone numbers but does not check "-" and misses blank spaces (ie: 222-22 1343). If I enter a 222--292-334 for example it will not give me any alert message. Any help with the regular expression or a better way to do my phone validation??
if (/[^((\d\s?[\(\-\.\s]\s?)?\d{3}\s?[\)\-\.\s]?)?\s?\d{3}\s?[\.\-\s]?\s?\d{4}$]/
.test(document.myform.phone.value))
{
alert("Invalid Phone input. Valid phone format: 703-111-2222");
return false;
}
else if (document.myform.phone.value.length != 12)
{
alert("Invalid Phone number length.");
return false;
}
if (/[^((\d\s?[\(\-\.\s]\s?)?\d{3}\s?[\)\-\.\s]?)?\s?\d{3}\s?[\.\-\s]?\s?\d{4}$]/
.test(document.myform.phone.value))
{
alert("Invalid Phone input. Valid phone format: 703-111-2222");
return false;
}
else if (document.myform.phone.value.length != 12)
{
alert("Invalid Phone number length.");
return false;
}