Hey everybody,
I'm trying to validate phone numbers (include area codes, and later country codes), and I'm wanting to accept:
555-555-5555
I've been going over the regex tutorials, but I'm having a hard time figuring this out. Thanks to anyone that can help.
Phone Number Validation
Moderator: General Moderators
-
pennythetuff
- Forum Newbie
- Posts: 22
- Joined: Sun Feb 19, 2006 6:05 pm
- Location: Kokomo, Indiana
-
pennythetuff
- Forum Newbie
- Posts: 22
- Joined: Sun Feb 19, 2006 6:05 pm
- Location: Kokomo, Indiana
Here's how I'm implementing it:
I have a problem though. When I put 5555-555-5555 through it won't error out. If I put 555-555-55555 it won't error out. If I put 555-5555-5555 it WILL error out. Am I implementing this wrong?
Code: Select all
if(!preg_match("/\d{3}-\d{3}-\d{4}/", $_POST['phone_number'])) {
$error_message[$error_counter++] = "Please enter a valid phone number.";
}- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
no, you didn't implement it wrong; it's looking for it anywhere in the string supplied.
will force full string matching.
Code: Select all
if(!preg_match("/^\d{3}-\d{3}-\d{4}$/s", $_POST['phone_number'])) {
$error_message[$error_counter++] = "Please enter a valid phone number.";
}-
pennythetuff
- Forum Newbie
- Posts: 22
- Joined: Sun Feb 19, 2006 6:05 pm
- Location: Kokomo, Indiana