Phone Number Validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Phone Number Validation

Post 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!
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

I'd go with a regex but you need to decide how strict you want to be.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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;
	
}
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Post Reply