How To Validate Or Email Or Phone Number In Contact Form 7

How To Validate Or Email Or Phone Number In Contact Form 7

In this article I will show you: How to validate or Email or phone number in Contact Form 7

Step1: Create your form:

You can place that snippet in content of form tab.

<label> Full Name (*):
     </label>

<label> Email:
    [email your-email] </label>

<label> Whatsapp:
    [tel phone-number] </label>
<label> Subject
     </label>

<label> Special Requirements:
    [textarea your-message] </label>

[submit "Send"]

Step 2: Validate Or Email Or Phone Number

You can place that snippet in functions.php in your theme’s folder.

add_filter( 'wpcf7_validate_email', 'custom_confirmation_validation_filter', 20, 2 );
 
function custom_confirmation_validation_filter( $result, $tag ) {
    if ( 'your-email' == $tag->name ) {
        $your_email = isset( $_POST['your-email'] ) ? trim( $_POST['your-email'] ) : '';
        $whatapps = isset( $_POST['phone-number'] ) ? trim( $_POST['phone-number'] ) : '';
        if ( $your_email == '' ) {
        	if($whatapps == '')
            	$result->invalidate( $tag, "You must be entry Email or Whatsapp" );
        }
    }
 
    return $result;
}

Result like this:

Comments

comments

Share