This topic is locked

Complete Validation for U.S. Phone Numbers

4/25/2007 7:32:29 PM
PHPRunner General questions
K
kenlyle author

Does anyone have a complete validation routine for U.S. phone numbers? The default one only checks that phone numbers consists of numbers, dashes

and parenthesis. The remaining issues seem to include:
-10 digits total

-3 digits, maybe parens, maybe space, maybe hyphen, 3 digits, maybe parens, maybe space, maybe hyphen, 4 digits.
Sergey says it's in validate.js...
Thanks,

Ken

D
Dale 4/27/2007

This is what I changed to validate phone numbers.

search for the validateObject.type.indexOf("IsPhoneNumber") in the generated validate.php and modify the snippet.

Test it out and if it all works for you, place this snippet in the validate.php file in the source folder of your templates.

PS. This is version 3.0 im using this in. I modified the IsZipCode also for 5 or 9 zips and the canadian postal code format.
else if(validateObject.type.indexOf("IsPhoneNumber")>=0)

//IsPhoneNumber tells whether the field is a Phone number or not

//These Phone number fields are valid if there is nothing in the field

{

var phone2 = /^\s*([( ]{1}[0-9]{3}[) ]{1}[. -]{0,1}|[0-9]{3}[- .])?[0-9]{3}[- .]{1}[0-9]{4}$/;
if (isWhitespace(validateObject.val)!=true)

{
str = "";

str = removeSpaces(validateObject.val);

if (!str.match(phone2))

{

errors += validateObject.HTMLname + "\n";

PhnMsg += validateObject.HTMLname + " formats: (555) 321-1234, 555-321-1234, 321-1234 (Period separator allowed)"+"\n";

}

}

}