phpretard Posted May 6, 2009 Share Posted May 6, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/157080-validate-xxx-xxx-xxxx/ Share on other sites More sharing options...
trq Posted May 6, 2009 Share Posted May 6, 2009 Retard, Define won't work. Quote Link to comment https://forums.phpfreaks.com/topic/157080-validate-xxx-xxx-xxxx/#findComment-827411 Share on other sites More sharing options...
phpretard Posted May 6, 2009 Author Share Posted May 6, 2009 Ok so can you help? I know it doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/157080-validate-xxx-xxx-xxxx/#findComment-827412 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 $str = trim($str); // just in case echo preg_match('#^\(?\d{3}\)?\s*\d{3}-\d{4}#', $str); Quote Link to comment https://forums.phpfreaks.com/topic/157080-validate-xxx-xxx-xxxx/#findComment-827428 Share on other sites More sharing options...
Adam Posted May 7, 2009 Share Posted May 7, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/157080-validate-xxx-xxx-xxxx/#findComment-828329 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 MrAdam, wouldn't that make 1111111111 valid as well? Quote Link to comment https://forums.phpfreaks.com/topic/157080-validate-xxx-xxx-xxxx/#findComment-828412 Share on other sites More sharing options...
Adam Posted May 7, 2009 Share Posted May 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157080-validate-xxx-xxx-xxxx/#findComment-828417 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 If you change your [-\.\s]? to (-|\.|\s), I think it should work. Quote Link to comment https://forums.phpfreaks.com/topic/157080-validate-xxx-xxx-xxxx/#findComment-828421 Share on other sites More sharing options...
nrg_alpha Posted May 7, 2009 Share Posted May 7, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/157080-validate-xxx-xxx-xxxx/#findComment-828543 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 Well that doesn't check if it's valid. For example: $str = '0kl3i2841dfiw8 we _=5 6 adf8'; That would be valid with your code. Quote Link to comment https://forums.phpfreaks.com/topic/157080-validate-xxx-xxx-xxxx/#findComment-828546 Share on other sites More sharing options...
premiso Posted May 7, 2009 Share Posted May 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157080-validate-xxx-xxx-xxxx/#findComment-828577 Share on other sites More sharing options...
nrg_alpha Posted May 7, 2009 Share Posted May 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157080-validate-xxx-xxx-xxxx/#findComment-828591 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 Actually, I had more than 1 point in that post. Sorry if it wasn't explicit. The title says "Validate (XXX) XXX-XXXX" so I assume not any ol' format will do. But I definitely understood what you were trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/157080-validate-xxx-xxx-xxxx/#findComment-828603 Share on other sites More sharing options...
nrg_alpha Posted May 7, 2009 Share Posted May 7, 2009 In case you're wondering about this 'Hall of Shame', here is the author's brother's website. People can learn from such bad form regulation! Quote Link to comment https://forums.phpfreaks.com/topic/157080-validate-xxx-xxx-xxxx/#findComment-828623 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 In case you're wondering about this 'Hall of Shame', here is the author's brother's website. People can learn from such bad form regulation! That is awesome! Good share. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/157080-validate-xxx-xxx-xxxx/#findComment-828629 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.