View Single Post
  #1 (permalink)  
Old 09-01-2008, 12:55 PM
chili5's Avatar   
chili5 chili5 is offline
Code Warrior
 
Join Date: Mar 2008
Age: 16
Posts: 3,930
Last Blog:
DWITE 2
Credits: 0
Rep Power: 35
chili5 is a name known to allchili5 is a name known to allchili5 is a name known to allchili5 is a name known to allchili5 is a name known to allchili5 is a name known to all
Default Phone number regexp

I'm trying to come up with a regexp that can be used to validate Canadian and United States phone number. Now in Canada, it is required to use a 3 digit area code, then the actual phone number. From what I know it's not required in the United States to do that.

Well I came up with:

PHP Code:
if (preg_match("/[0-9]{3}?-[0-9]{3}-[0-9]{4}/""555-5555")) {
    echo 
"Valid phone number.";

that matches any 3 digit code, then another 3 digit code, then any 4 digit codes. Such as 555-555-5555 but if the phone number being validated doesn't use an area code then it will fail because it doesn't match. If the user provides

555-5555 it will match the first part [0-9]{3}? but then will fail the next part of the regexp. Any ideas to correct this?
__________________
I've found time can heal most anything
And you just might find who you're supposed to be
I didn't know who I was supposed to be at fifteen.
~ Taylor Swift
Reply With Quote

Sponsored Links