Jump to content

Validate (XXX) XXX-XXXX


phpretard

Recommended Posts

Why wont this work...It's driving me nuts!

 

<?
$string = "(232) 555-5555";

if (preg_match('/^\(?[0-9]{3}\)?|[0-9]{3}[-. ]? [0-9]{3}?[0-9]{4}$/', $string)) { 
echo "successful.";
}else{
echo "not successful";
}

?>

 

Thank you!

Link to comment
Share on other sites

This works:

 

$string = "(232) 555-5555";

if (preg_match('/^\(?\d{3}\)?[-\.\s]?\d{3}[-\.\s]?\d{4}$/', $string)) {
    echo "successful.";
}else{
    echo "not successful";
}

 

From what I could gather of yours the brackets are optional and the two gaps could be either a space, hypon or dot?

 

Examples:

 

(111) 111 1111

(111)-1111111

(111)111.1111

111-111-1111

111.111.1111

111 111 1111

Link to comment
Share on other sites

Yeah - obviously you can't count on the user to enter the data correctly. If you look at his original regular expression it looks like he's tried to put in that flexibility but being unsuccessful.

Link to comment
Share on other sites

I'm reminded of the part in the book Mastering Regular Expressions where the author mentions the 'Hall of Shame' where some sites will explicitly say something like "Do not add any dashes or spaces" for a specific form field for example. This is considered 'lazy programming' as the programmer in such an instance doesn't let the user enter that kind of info, and just strip out those characters and check what's left.

 

Same principal could be applied here.. instead of checking for a format and fidgeting with dashes, dots and parentheses.. why not take the phone number entry, strip out all non numerical characters, and see what's left?

 

Example:

$str = '(232).654.3210';
$str = preg_replace('#[^0-9]#', '', $str); // get rid of useless characters
echo (strlen($str) != 10)? 'Invalid phone number format.' : 'Valid phone number format.';
// optionally set your prefered formatting with number given...
//	echo $phoneNumber = '(' . substr($str, 0, 3) . ') ' . substr($str, 3, 3) . '-' . substr($str, 6, 4);

Link to comment
Share on other sites

Well that doesn't check if it's valid.

 

For example:

$str = '0kl3i2841dfiw8 we _=5  6 adf8';

 

That would be valid with your code.

 

The point is, you strip out/replace all non-numeric characters. What this will do is give you just the numbers. If someone enters something like you have above, it would be valid, and it would just pull the numbers. It would be the exact same if they just typed in 1234432565 which would be valid. They key is the honest people will enter in the number how they want without fuss and the ones who do not want their number entered will enter in a fake number anyway.

 

This way accommodates both people and does not tick off the honest person because they have to follow a set mask, which is annoying. However, the field should be limited to x characters on the form side so like 15 or 16 characters max. But as long as you get 10 digits out of it, thats a phone number and it is validated.

 

EDIT:

I mean honestly how are you going to tell if the phone number is truly a number? Find a webservice or database of numbers/area codes and match them to their zipcode ??? I highly doubt it. Entering phone numbers is more on the lines of the "honor policy" than anything.

Link to comment
Share on other sites

Well put, premiso... that's exactly my point!

 

@Ken2k7,

 

Given that the example $string = "(232) 555-5555", I am going to assume this comes from an user submission (course, I have been wrong before) and I posted based off of that assumption (yes, I know what they say about assumptions). Granted, The OP never does explain how $string came about... We can only work with what we are given.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.