Page 1 of 1

Phone Number Validation

Posted: Sat Dec 17, 2005 3:19 pm
by seodevhead
Hey guys, was wondering if anyone had any particular code suggestions for validating phone numbers.. reg ex's, etc. Have been looking around without luck. Thanks and take care!

Posted: Sat Dec 17, 2005 3:43 pm
by bokehman
I'd go with a regex but you need to decide how strict you want to be.

Posted: Sat Dec 17, 2005 4:00 pm
by hawleyjr

Code: Select all

#http://www.owasp.org/software/validation.html
/*
	phone
	^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$
	US phone number with or without dashes
*/
function isPhoneNumber($phn){

	if (preg_match ("/^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$/", $phn)) 
		return TRUE;
	else
		return FALSE;
	
}

Posted: Sun Dec 18, 2005 1:11 am
by Jenk
all depends on where the user is based... in the uk alone we have several formats of phone number:
National:
0x0 xxxx xxxx

01xxx xxx xxx

Local:
xxx xxx
xxxx xxxx

Mobile:
07xxx xxx xxx

Posted: Sun Dec 18, 2005 12:08 pm
by Chris Corbyn
Yep... If you're going to do this w/out <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> your visitors off then it will have to be very very loose checking. You could provide fixed-length input boxes for them to type in too... that would make it clearer what is expected to be used.