PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Nov 19, 2017 9:01 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Nov 27, 2013 9:07 am 
Offline
Forum Commoner

Joined: Thu Oct 10, 2013 8:40 am
Posts: 26
Location: Manchester
I want to validate the phone number using a regular expression. Phone number should be global. It may contain special characters like "hypen" or "Paranthesis".

_________________
Tayla the great


Top
 Profile  
 
PostPosted: Wed Nov 27, 2013 2:22 pm 
Offline
DevNet Resident

Joined: Sun Jun 14, 2009 3:13 pm
Posts: 1146
You can find lots of regexs on the internet to suit your needs. I don't know specifically what you mean by a "global" phone number but here's a regex I've used in the past with some test cases. There are some international phone numbers with an extra digit that fails this regex.

Syntax: [ Download ] [ Hide ]
$numbers[]='+1 404-223-1122';
$numbers[]='1-234-567-8901';
$numbers[]='1-234-567-8901 x1234';
$numbers[]='1-234-567-8901 ext1234';
$numbers[]='1 (234) 567-8901';
$numbers[]='1.234.567.8901';
$numbers[]='1/234/567/8901'; //fails
$numbers[]='12345678901';

foreach($numbers as $number){
  echo $number.'=';
  if(preg_match('/^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/',$number)){
    echo "Pass\n";
  }
  else {
    echo "Fail\n";
  }
}


Top
 Profile  
 
PostPosted: Wed Nov 27, 2013 4:55 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6578
Location: WA, USA
Best method I know: strip everything but the numbers and check the length. Then reformat however you want.

If you want extensions, allow letters too and then do a regex (because it's easier than the alternatives). Even then all you really need is to check the lengths of numbers.


Top
 Profile  
 
PostPosted: Wed Nov 27, 2013 7:00 pm 
Offline
DevNet Resident

Joined: Sun Jun 14, 2009 3:13 pm
Posts: 1146
I used to do that, but I ran into problems where different regions like the phone numbers to be formatted differently. Like in Mexico they like to group numbers by 2's. So I found it easier to try to work with whatever format user's were used to using.


Top
 Profile  
 
PostPosted: Sat Dec 20, 2014 7:52 pm 
Offline
Forum Newbie

Joined: Sat Dec 20, 2014 7:38 pm
Posts: 4
With this tool you can enter your variants and generate regex based on them http://www.regex-builder.com/

_________________
http://www.regex-builder.com/ online regular expression composer based on text analysis


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group