Lost Password?


Go Back   CodeCall Programming Forum > Web Development Forum > PHP Forum

PHP Forum Use this forum to discuss all aspects of PHP Development. PHP is a server-side, cross-platform, HTML embedded scripting language that lets you create dynamic web pages.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-01-2008, 12:55 PM
chili5's Avatar   
chili5 chili5 is offline
Code Warrior
 
Join Date: Mar 2008
Age: 15
Posts: 3,591
Rep Power: 31
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?
__________________
Emo Philips - "My computer beat me at checkers, but I sure beat it at kickboxing."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 09-01-2008, 01:05 PM
MeTh0Dz|Reb0rn's Avatar   
MeTh0Dz|Reb0rn MeTh0Dz|Reb0rn is online now
My Posts Are Moderated
 
Join Date: Jul 2008
Posts: 15
Rep Power: 0
MeTh0Dz|Reb0rn is an unknown quantity at this point
Default Re: Phone number regexp

If it fails then have it check for the number without the area code. Then if that fails you can assume that it is an invalid number.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-01-2008, 01:15 PM
chili5's Avatar   
chili5 chili5 is offline
Code Warrior
 
Join Date: Mar 2008
Age: 15
Posts: 3,591
Rep Power: 31
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 Re: Phone number regexp

I could do that, but I didn't think of this before, just check the phone number against two regular expressions (one with the area code, and one without it) like so:

PHP Code:
if (preg_match("/[0-9]{3}?-[0-9]{3}-[0-9]{4}/""555-555-5555") || preg_match("/[0-9]{3}?-[0-9]{4}/","555-5555")) {
    echo 
"Valid phone number.<br />";
} else {
    echo 
"Invalid phone number.<br />";

I think that is more simple, and it still does work.
__________________
Emo Philips - "My computer beat me at checkers, but I sure beat it at kickboxing."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-01-2008, 01:40 PM
MeTh0Dz|Reb0rn's Avatar   
MeTh0Dz|Reb0rn MeTh0Dz|Reb0rn is online now
My Posts Are Moderated
 
Join Date: Jul 2008
Posts: 15
Rep Power: 0
MeTh0Dz|Reb0rn is an unknown quantity at this point
Default Re: Phone number regexp

Yeah that's fine.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-01-2008, 02:10 PM
John's Avatar   
John John is online now
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,432
Last Blog:
Google Web Toolkit
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default Re: Phone number regexp

How about this?
Code:
"/([0-9]{3}-?){1,2}[0-9]{4}/"
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 09-01-2008, 02:21 PM
chili5's Avatar   
chili5 chili5 is offline
Code Warrior
 
Join Date: Mar 2008
Age: 15
Posts: 3,591
Rep Power: 31
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 Re: Phone number regexp

Thanks, I think that would work because the area code is supposed to be in (). Thanks a bunch.
__________________
Emo Philips - "My computer beat me at checkers, but I sure beat it at kickboxing."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 09-01-2008, 02:24 PM
chili5's Avatar   
chili5 chili5 is offline
Code Warrior
 
Join Date: Mar 2008
Age: 15
Posts: 3,591
Rep Power: 31
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 Re: Phone number regexp

Thanks I guess I have a lot of reviewing on this to do.
__________________
Emo Philips - "My computer beat me at checkers, but I sure beat it at kickboxing."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 09-01-2008, 02:24 PM
John's Avatar   
John John is online now
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,432
Last Blog:
Google Web Toolkit
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default Re: Phone number regexp

This:
Code:
([0-9]{3}-?)
simply groups "three digits and a dash" together. The {1,2} quantifier, says that the "three digits and a dash can occur once or twice."

Using your regular expression this may work
Code:
/[0-9]{3}-[0-9]{3}?-[0-9]{4}/
but I like mine better
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 09-01-2008, 06:51 PM
chili5's Avatar   
chili5 chili5 is offline
Code Warrior
 
Join Date: Mar 2008
Age: 15
Posts: 3,591
Rep Power: 31
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 Re: Phone number regexp

I'll go with yours, because I think it works really well, with a lot of different formats. You know any good sites to learn how to use these better? I think they are really hard to learn and a reference of all the characters used would be great.
__________________
Emo Philips - "My computer beat me at checkers, but I sure beat it at kickboxing."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 09-01-2008, 07:23 PM
John's Avatar   
John John is online now
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,432
Last Blog:
Google Web Toolkit
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default Re: Phone number regexp

I'm in the process of writing a tutorial. You can expect it in a few days.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
A small problem in my calc program nolls Pascal/Delphi 18 02-28-2008 12:58 AM
Guess a Number 3n! C and C++ 2 12-02-2007 04:44 PM


All times are GMT -5. The time now is 12:43 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 97%

Ads