$php_mysql$ Posted July 7, 2011 Share Posted July 7, 2011 hey mates this is how i validate a field to check for phone number if($adsData['phone'] == '') { $error['phone'] = 'Enter your contact number.'; }else if(!is_numeric($adsData['phone']) || strlen($adsData['phone']) < 12) { $error['phone'] = 'Insert a valid phone number!'; } so what im tryin it to make this field not necessary even if left empty but only check if its numeric and is of 12 chars like this f(!is_numeric($adsData['phone']) || strlen($adsData['phone']) < 12) { $error['phone'] = 'Insert a valid phone number!'; } but still in my form the error message ('Insert a valid phone number!) shows, any idea why? even after i removed $adsData['phone'] == '' Link to comment https://forums.phpfreaks.com/topic/241309-form-vlidation-issue/ Share on other sites More sharing options...
monkeytooth Posted July 7, 2011 Share Posted July 7, 2011 What are some examples of phone numbers your keying in to test this logic? Link to comment https://forums.phpfreaks.com/topic/241309-form-vlidation-issue/#findComment-1239500 Share on other sites More sharing options...
$php_mysql$ Posted July 7, 2011 Author Share Posted July 7, 2011 no what im keying is to not show error message even if the field phone number is left empty while submiting but yes only check is its numeric and if users inputs alphabets only then error message shows Link to comment https://forums.phpfreaks.com/topic/241309-form-vlidation-issue/#findComment-1239503 Share on other sites More sharing options...
monkeytooth Posted July 7, 2011 Share Posted July 7, 2011 Ahh.. well.. if(!is_numeric($adsData['phone']) || strlen($adsData['phone']) < 12) { $error['phone'] = 'Insert a valid phone number!'; } when it hits that point in the code is still checking to see if the field is whats desired. In your case numeric and a length no less than 12 char. And an empty field in this case is not one that matches numeric constraints... so i'd say wrap that in one more if statement. if((isset($adsData['phone']))AND(!empty($adsData['phone']))AND(trim($adsData['phone']) !== "")){ if((!is_numeric($adsData['phone'])) || strlen($adsData['phone']) < 12)) { $error['phone'] = 'Insert a valid phone number!'; } } This way if the field is set, and not empty it will check your constraints.. otherwise it will just pass on by. Link to comment https://forums.phpfreaks.com/topic/241309-form-vlidation-issue/#findComment-1239506 Share on other sites More sharing options...
$php_mysql$ Posted July 7, 2011 Author Share Posted July 7, 2011 neh not helping mate Link to comment https://forums.phpfreaks.com/topic/241309-form-vlidation-issue/#findComment-1239521 Share on other sites More sharing options...
$php_mysql$ Posted July 7, 2011 Author Share Posted July 7, 2011 can is not be done like this? if(!is_numeric($data['phone']) || strlen($data['phone']) < 12 || $data['phone'] !== '') { $errormsg['phone'] = 'not a valid number.'; } Link to comment https://forums.phpfreaks.com/topic/241309-form-vlidation-issue/#findComment-1239522 Share on other sites More sharing options...
monkeytooth Posted July 7, 2011 Share Posted July 7, 2011 So not numeric, with a length of 12 or less and not '' ? So if I type abcdefghijkl you want the field to validate? Link to comment https://forums.phpfreaks.com/topic/241309-form-vlidation-issue/#findComment-1239550 Share on other sites More sharing options...
$php_mysql$ Posted July 7, 2011 Author Share Posted July 7, 2011 if type abcd or number less than 10 only then i want the validation to take place and show the message if type nothing i do not wish for it to show any error message to the user and from should gets submited but it is not happening. Link to comment https://forums.phpfreaks.com/topic/241309-form-vlidation-issue/#findComment-1239560 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.