PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Dec 13, 2018 2:22 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: regex for phone number
PostPosted: Mon Jul 09, 2012 3:14 am 
Offline
Forum Commoner

Joined: Sun Jul 08, 2012 6:47 am
Posts: 25
Rules:
1. must start with number
2. sum of numeric must be between 7-12 inclusive
3. other valid non-numeric characters include space (" ") and hyphens ("-")
4. no more than one consecutive valid non-numeric characters allowed ("1 2-3" is allowed, "1-2" is allowed, but "1--2" is not, "1- 2" is also not allowed)

right now my regex is this:
\\d([- ]?\\d[- ]?){6,11}

but it passed the test 123--4567. What's wrong? thanks


Top
 Profile  
 
PostPosted: Mon Jul 09, 2012 5:04 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
Would it not be a lot easier to just strip out all non-numeric characters and validate the length? Then format it however you want.


Top
 Profile  
 
PostPosted: Mon Jul 09, 2012 2:18 pm 
Offline
Forum Contributor
User avatar

Joined: Thu May 11, 2006 8:58 pm
Posts: 305
Location: Utah, USA
You might need to run a regex for 3 and a separate regex for 4.


Top
 Profile  
 
PostPosted: Wed Aug 22, 2012 10:08 am 
Offline
Forum Newbie

Joined: Fri Jul 17, 2009 1:15 am
Posts: 18
Try following regex :

((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}

_________________
AccuWebHosting.Com - Windows VPS Hosting
ASP.NET 3.5 | SQL 2005 Database | US Based Hosting Company | 24 X 7 Support | Daily Backups | Uptime Guarantee | Affiliates - $50 Per Sale |
Brick7.com | TheWebHostingDir.com


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

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 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