Click to See Complete Forum and Search --> : Phone validation almost working


florida
04-18-2003, 12:20 PM
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;
}

jeffmott
04-18-2003, 12:35 PM
Instead of trying to figure out what your current regex is doing (which you also tell us isn't what you want to happen), it would be easier if you just told us what you want to match and what not to. For instance, if you want the format 555-5555 then it would be.../^\d{3}-\d{4}$/But if that's not what you want then you need to tell us what is. Is area code optional/required? Can they leave out the dash so long as they enter 7 digits (or 10 if they enter area code)?

florida
04-18-2003, 12:38 PM
I would like the following formats:

111-111-1111

or

111 111 1111

jeffmott
04-18-2003, 12:44 PM
/^\d{3}([- ])\d{3}\1\d{4}$/

florida
04-21-2003, 06:14 AM
Thanks