iconicCreator Posted February 1, 2009 Share Posted February 1, 2009 Okay I have been teaching myself PHP form processing and validation. What I'm trying to do is validate a form field that requires numbers. How do I do this? I searched google and found some codes but can't understand it. The problem is this, I want the error message to appear in the form label. Example: <label for"phoneNumber">Phone Number: ERROR MESSAGE GOES HERE </label> <input type="text" name="phoneNumber" /> How do I add this validation so that this format is required 555-555-555 and an error message appears if the user enters the wrong format or if the user enter more then nine digits. Any ideas or tutorial will be greatly appreciated. IC Link to comment https://forums.phpfreaks.com/topic/143354-solved-how-do-i-validate-a-number-field-in-a-form/ Share on other sites More sharing options...
Snart Posted February 1, 2009 Share Posted February 1, 2009 I'm assuming you're using POST as the method for your form, and you call the same page to process it. So (I will be foregoing the usual mysql injection checks and so on to concentrate on the real issue): <?php $error = ''; if(array_key_exists('phoneNumber', $_POST)) { if(!preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{3}$/', $_POST['phoneNumber'])) { $error = 'Wrong phone number!'; } } ?> <label for"phoneNumber">Phone Number: <?php echo $error; ?> </label> <input type="text" name="phoneNumber" /> Link to comment https://forums.phpfreaks.com/topic/143354-solved-how-do-i-validate-a-number-field-in-a-form/#findComment-751871 Share on other sites More sharing options...
iconicCreator Posted February 1, 2009 Author Share Posted February 1, 2009 I'm assuming you're using POST as the method for your form, and you call the same page to process it. So (I will be foregoing the usual mysql injection checks and so on to concentrate on the real issue): <?php $error = ''; if(array_key_exists('phoneNumber', $_POST)) { if(!preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{3}$/', $_POST['phoneNumber'])) { $error = 'Wrong phone number!'; } } ?> <label for"phoneNumber">Phone Number: <?php echo $error; ?> </label> <input type="text" name="phoneNumber" /> Hey thanks for your help! I tried this out and worked with it. However, the phone number field keeps indicating wrong number even when I typed a number in the form phone number field. Eg: 555-555-555 comes up as in correct number. Any ideas when this is? This is what I have so far. <?php $error = ''; if(array_key_exists('phoneNumber', $_POST)) { if(!preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{3}$/', $_POST['phoneNumber'])) { $error = 'Required!'; } } ?> <div class="phone_fieldWrapper"> <label for="phoneNumber">Phone Number: <span class="warning"> <?php echo $error; ?></span></label> <input name="phoneNumber" type="text" class="phoneNumber_inputControl" id="phoneNumber" /> </div> Thanks for your patience with my ignorance! Link to comment https://forums.phpfreaks.com/topic/143354-solved-how-do-i-validate-a-number-field-in-a-form/#findComment-752181 Share on other sites More sharing options...
iconicCreator Posted February 1, 2009 Author Share Posted February 1, 2009 I finally figured it out, The problem was the number in the last curly bracket should be 4 not 3. That's because the last sets of digits of the phone number is four not three. Eg: 5555. Anyway, thanks very much, IC. Link to comment https://forums.phpfreaks.com/topic/143354-solved-how-do-i-validate-a-number-field-in-a-form/#findComment-752214 Share on other sites More sharing options...
the182guy Posted February 1, 2009 Share Posted February 1, 2009 Be careful with validating phone number fields if users don't all have US phone numbers. Does it accept country codes? e.g. +44(0)161775544 If you will only ever have US users then you don't have a problem. Link to comment https://forums.phpfreaks.com/topic/143354-solved-how-do-i-validate-a-number-field-in-a-form/#findComment-752220 Share on other sites More sharing options...
iconicCreator Posted February 1, 2009 Author Share Posted February 1, 2009 Be careful with validating phone number fields if users don't all have US phone numbers. Does it accept country codes? e.g. +44(0)161775544 If you will only ever have US users then you don't have a problem. That's an excellent point so lets assumed I was going to validate U.S and international phone numbers, the what would I need to add? Eg: 233-243-556941 IC Link to comment https://forums.phpfreaks.com/topic/143354-solved-how-do-i-validate-a-number-field-in-a-form/#findComment-752222 Share on other sites More sharing options...
the182guy Posted February 1, 2009 Share Posted February 1, 2009 If you want to accept international phone numbers then that means any phone number in the world? If so then you will have to relax the validation I think because lets say a user enters his international phone number e.g here in the UK +44(0)161987654, if your validation fails on that then the user will just enter any garbage that passes the validation and you will have garbage data in your database. The best way to check your reg expression is google for some international phone numbers and see if they pass your validation. What I tend to do when validating phone numbers is use relaxed validation, for example check there are no alphabetic characters present, check there is some data present, check there are no symbols present except ()+- Afterall if the user does not want to tell you their phone number, they will enter a made up one regardless of your validation checks. Link to comment https://forums.phpfreaks.com/topic/143354-solved-how-do-i-validate-a-number-field-in-a-form/#findComment-752234 Share on other sites More sharing options...
iconicCreator Posted February 2, 2009 Author Share Posted February 2, 2009 If you want to accept international phone numbers then that means any phone number in the world? If so then you will have to relax the validation I think because lets say a user enters his international phone number e.g here in the UK +44(0)161987654, if your validation fails on that then the user will just enter any garbage that passes the validation and you will have garbage data in your database. The best way to check your reg expression is google for some international phone numbers and see if they pass your validation. What I tend to do when validating phone numbers is use relaxed validation, for example check there are no alphabetic characters present, check there is some data present, check there are no symbols present except ()+- Afterall if the user does not want to tell you their phone number, they will enter a made up one regardless of your validation checks. Very great point, I have on occasion enter fake phone numbers when required and I don't want to provide it. So I think I'm just going make phone numbers U.S only. and if I was going to make an international phone number required, I'll just relax the validation to insure no alphabetic characters. But how do I go about doing this? Lets say the numbers start with a 0 and I have between zero and nine. IC Link to comment https://forums.phpfreaks.com/topic/143354-solved-how-do-i-validate-a-number-field-in-a-form/#findComment-752260 Share on other sites More sharing options...
redarrow Posted February 2, 2009 Share Posted February 2, 2009 with out or with country code. example country code +44(0)161-775-544 normal code 161-775-544 Example corrected. <?php $error = ''; if(array_key_exists('phoneNumber', $_POST)) { if(!preg_match('/^((\+)[0-9]{2}\([0-9]\)[0-9]{3}-|[0-9]{3}-)[0-9]{3}-[0-9]{3}$/', $phonenumber)) { $error = 'Required!'; } } ?> <div class="phone_fieldWrapper"> <label for="phoneNumber">Phone Number: <span class="warning"> <?php echo $error; ?></span></label> <input name="phoneNumber" type="text" class="phoneNumber_inputControl" id="phoneNumber" /> </div> Link to comment https://forums.phpfreaks.com/topic/143354-solved-how-do-i-validate-a-number-field-in-a-form/#findComment-752269 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.