Viewing 2 replies - 1 through 2 (of 2 total)
  • 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 2 replies - 1 through 2 (of 2 total)

The topic ‘Custom logic for form validation’ is closed to new replies.