carley_bell Posted January 24, 2009 Share Posted January 24, 2009 Hi, I am trying to do a server side form validation for a phone number that is not a required field, but if they choose to enter the field I want it to be a validated phone number. I have tried about 50 different versions switching the variables any way I new how but I can't seem to get it to work. I know it is something simple but I can't get it to work. // make sure phone number is valid if (!$_POST['field_6']) { } else (!preg_match('/^.?[0-9]{3}.?.?[0-9]{3}.?[0-9]{4}$/i', $_POST['field_6'])){ die('Invalid phone number. Please use your browsers back button to go back to the form '); } Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/142203-solved-phone-number-validation-for-not-required-field/ Share on other sites More sharing options...
nuttycoder Posted January 24, 2009 Share Posted January 24, 2009 have you tried: if (isset($_POST['field_6'])) { (!preg_match('/^.?[0-9]{3}.?.?[0-9]{3}.?[0-9]{4}$/i', $_POST['field_6'])){ die('Invalid phone number. Please use your browsers back button to go back to the form '); } Quote Link to comment https://forums.phpfreaks.com/topic/142203-solved-phone-number-validation-for-not-required-field/#findComment-744964 Share on other sites More sharing options...
.josh Posted January 24, 2009 Share Posted January 24, 2009 (assuming we are talking about american 10 digit area code+phone number numbers...) My suggestion is that instead of trying to write some way complex regex that handles variations of some arbitrary format, you just cut to the chase and see if there's 10 digits. That way users can use any format they want. $number = preg_replace('~[^0-9]~','',$number); if (strlen($number) == 10) { // number is good, do something } else { // number is bad, do something } Quote Link to comment https://forums.phpfreaks.com/topic/142203-solved-phone-number-validation-for-not-required-field/#findComment-744965 Share on other sites More sharing options...
carley_bell Posted January 24, 2009 Author Share Posted January 24, 2009 I tried this one: if (isset($_POST['field_6'])) { (!preg_match('/^.?[0-9]{3}.?.?[0-9]{3}.?[0-9]{4}$/i', $_POST['field_6'])){ die('Invalid phone number. Please use your browsers back button to go back to the form '); } but I am getting this error: Parse error: syntax error, unexpected '{' in D:\Host\processor.php on line 19 to make it easier I can figure out preg_match(... stuff, I just need to figure out how to make it ignore the validation if nothing is entered into the input, but validate it if they do enter a phone number. thanks Quote Link to comment https://forums.phpfreaks.com/topic/142203-solved-phone-number-validation-for-not-required-field/#findComment-744999 Share on other sites More sharing options...
nuttycoder Posted January 24, 2009 Share Posted January 24, 2009 oops my bad <?php if (isset($_POST['field_6'])) { if(!preg_match('/^.?[0-9]{3}.?.?[0-9]{3}.?[0-9]{4}$/i', $_POST['field_6'])) { die('Invalid phone number. Please use your browsers back button to go back to the form '); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142203-solved-phone-number-validation-for-not-required-field/#findComment-745006 Share on other sites More sharing options...
carley_bell Posted January 24, 2009 Author Share Posted January 24, 2009 It is displaying the error "Invalid phone number. Please use your browsers back button to go back to the form" when nothing is entered instead of ignoring it and moving on to the next validation Quote Link to comment https://forums.phpfreaks.com/topic/142203-solved-phone-number-validation-for-not-required-field/#findComment-745011 Share on other sites More sharing options...
CroNiX Posted January 24, 2009 Share Posted January 24, 2009 if (isset($_POST['field_6']) && !empty($_POST['field_6'])) ?? Quote Link to comment https://forums.phpfreaks.com/topic/142203-solved-phone-number-validation-for-not-required-field/#findComment-745021 Share on other sites More sharing options...
carley_bell Posted January 24, 2009 Author Share Posted January 24, 2009 that did the trick...I don't understand how, but it worked Quote Link to comment https://forums.phpfreaks.com/topic/142203-solved-phone-number-validation-for-not-required-field/#findComment-745024 Share on other sites More sharing options...
CroNiX Posted January 24, 2009 Share Posted January 24, 2009 Well, you didn't want to run the validation when there is no value, so thats what that does. Quote Link to comment https://forums.phpfreaks.com/topic/142203-solved-phone-number-validation-for-not-required-field/#findComment-745027 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.