Jump to content

form vlidation issue


$php_mysql$

Recommended Posts

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

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.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.