looking for UK phone validator class

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
MrGreen
Forum Newbie
Posts: 2
Joined: Thu Jun 17, 2004 1:00 am

looking for UK phone validator class

Post by MrGreen »

i'm looking for a simple php class that i can use to validate UK phone numbers
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Show us a valid phone number?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Try:

Code: Select all

if(preg_match("!^(44[ ]?)?[0-9]{3,5}[ ]?[0-9]{3}[ ]?[0-9]{3,4}$!", trim(preg_replace("!\\s{1,}!", " ", str_replace("+", "", str_replace("(", "", str_replace(")", "", $num))))), $matches)){
  echo 'Valid Number';
} else {
  echo 'Invalid Number';
}
Where $num is the phone number.
MrGreen
Forum Newbie
Posts: 2
Joined: Thu Jun 17, 2004 1:00 am

thanks, one more question

Post by MrGreen »

this is valid only for regular phone number, right?
do you have an example of a valid number that i can test it with?
is there an adition that i can use that can work for cellular numbers also?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Yeah, it's only valid for UK numbers (see http://www.wtng.info/wtng-uu.html#United%20Kingdom for the format).
I've no idea how you go about validating mobile numbers as i'm not sure of all the operator specific numbers.
Post Reply