Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 7th, 2009, 10:51 AM
diseased013's Avatar
diseased013 diseased013 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Coast, MS
Posts: 278 diseased013 User rank is Sergeant Major (2000 - 5000 Reputation Level)diseased013 User rank is Sergeant Major (2000 - 5000 Reputation Level)diseased013 User rank is Sergeant Major (2000 - 5000 Reputation Level)diseased013 User rank is Sergeant Major (2000 - 5000 Reputation Level)diseased013 User rank is Sergeant Major (2000 - 5000 Reputation Level)diseased013 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 4 Days 5 h 9 m 31 sec
Reputation Power: 41
Matching phone numbers

I am trying to figure out a regexp that I found on http://stackoverflow.com/questions/...mber-validation

I believe it is supposed to validate phone numbers in both U.S. and International format (accept a country code) as well as supporting extensions...

Quote:
Originally Posted by SOURCE
If you're talking about form validation, the regexp to validate correct meaning as well as correct data is going to be extremely complex because of varying country and provider standards. It will also be hard to keep up to date.

I interpret the question as looking for a broadly valid pattern, which may not be internally consistent - for example having a valid set of numbers, but not validating that the trunk-line, exchange, etc. to the valid pattern for the country code prefix.

North America is straightforward, and for international I prefer to use an 'idiomatic' pattern which covers the ways in which people specify and remember their numbers:
Code:
^(((((\d{3}))|(\d{3}-))\d{3}-\d{4})|(+?\d{2}((-| )\d{1,8}){1,5}))(( x| ext)\d{1,5}){0,1}$

The North American pattern makes sure that if one parenthesis is included both are. The international accounts for an optional initial '+' and country code. After that, you're in the idiom. Valid matches would be:
Code:
        (xxx)xxx-xxxx
        (xxx)-xxx-xxxx
        (xxx)xxx-xxxx x123
        12 1234 123 1 x1111
        12 12 12 12 12 
        12 1 1234 123456 x12345
        +12 1234 1234
        +12 12 12 1234
        +12 1234 5678
        +12 12345678

This may be biased as my experience is limited to North America, Europe and a small bit of Asia.


I am attempting to use this regexp in a script I am working on but when I try to pass anything through it the match fails. Even valid examples listed above in the creator's comments...

I am not very skilled with regular expressions and was hoping for some help determining if this is correct or not.

Thanks in advance for your time/replies
__________________

Reply With Quote
  #2  
Old August 7th, 2009, 12:12 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,585 DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
Try this regex: ".*"

Why do you want to validate phone numbers? I don't think they should be validated, especially on web pages. This is a bugbear of mine ever since I had to make up a random string of numbers to log into a website that insisted I enter a US-style phone number despite the fact that on the previous page I had told it I was not in the USA. Unless you are going to have the computer phone the user, there is absolutely no reason to validate the number.

I would consider these to be valid entries for the phone number field on a web page:

"123 456 7890 until 6pm, then 098 765 4321"
"123 456 7890 or try my mobile on 098 765 4321"
"ex-directory - mind your own business"


If you try and force the user to enter a valid phone number when they would rather communicate by other means then they will either enter a random number or give up and go elsewhere. If I was feeling particularly malicious I might enter the number for a premium rate sex line.

Dave

Reply With Quote
  #3  
Old August 7th, 2009, 12:30 PM
diseased013's Avatar
diseased013 diseased013 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Coast, MS
Posts: 278 diseased013 User rank is Sergeant Major (2000 - 5000 Reputation Level)diseased013 User rank is Sergeant Major (2000 - 5000 Reputation Level)diseased013 User rank is Sergeant Major (2000 - 5000 Reputation Level)diseased013 User rank is Sergeant Major (2000 - 5000 Reputation Level)diseased013 User rank is Sergeant Major (2000 - 5000 Reputation Level)diseased013 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 4 Days 5 h 9 m 31 sec
Reputation Power: 41
Quote:
Originally Posted by DevCoach
Try this regex: ".*"


I'm mainly just trying to standardize the information that's being collected to keep the database as clean as possible.

Given all the varying flavors of phone number syntax in the world I didn't even really know if it was possible but it seems like it should be to a point. Even if I just collect country code followed by the rest of the digits without spaces that would be better than having an array of different formats scattered throughout my data.

I may be better off collecting it in 3 parts
Code:
(int)(area code)(local number)


Then I could auto assign U.S. users int. code, let other countries enter theirs optionally, require area code and local. Then when processing strip everything out that wasn't a digit, and append all the pieces together for the DB entry.

That sound like a smarter way to go?

Edit: No, no it doesn't

Thanks for the advice I'm going to take it

Last edited by diseased013 : August 7th, 2009 at 12:37 PM.

Reply With Quote
  #4  
Old August 7th, 2009, 12:50 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,585 DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
If you just want to standardize the database field then just strip out everything except digits - after all, that is what you will enter into the phone keypad.

Dave

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Matching phone numbers

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap