Quantcast
Channel: contact form 7 – WordPress.org Forums
Viewing all articles
Browse latest Browse all 9540

Rajkumar Gour on "[Plugin: Contact Form 7] Custom logic for form validation"

$
0
0

you can create a hook for this validation. I am assuming that you have choose the text input type for the phone no.

add_filter( 'wpcf7_validate_text', 'validate_phone', 10, 2 );
add_filter( 'wpcf7_validate_text*', 'validate_phone', 10, 2 );
function validate_phone($result, $tag){
   $tag   =   new WPCF7_Shortcode( $tag );
   $name   =   'phone';
   $value   =   isset( $_POST[$name] )? trim( wp_unslash( strtr( (string) $_POST[$name], "\n", "" ) ) )	:	'';
   $submission   =   WPCF7_Submission::get_instance();
   $posted_data   =	$submission->get_posted_data();

$preffered_method   =   isset( $_POST['preferred_contact_method_is'] )? trim( wp_unslash( strtr( (string) $_POST['preferred_contact_method_is'], "\n", "" ) ) )	:	'';

if($preffered_method =="phone"){
	if(empty($value)){
		$result->invalidate( $name, "Please fill your phone no." );

	}
}
return $result;
}

Viewing all articles
Browse latest Browse all 9540

Trending Articles