Contact Form 7 - Email Validation

Contact Form 7 - Email Validation

Sagar Nangare
Reference

Block none-business(/Free/Certain) address from using the contact form

Code (function.php)

function varified_email($email){
    if(
        preg_match('/@gmail.com/i', $email) ||
        preg_match('/@hotmail.com/i', $email) ||
        preg_match('/@live.com/i', $email) ||
        preg_match('/@msn.com/i', $email) ||
        preg_match('/@aol.com/i', $email) ||
        preg_match('/@yahoo.com/i', $email) ||
        preg_match('/@inbox.com/i', $email) ||
        preg_match('/@gmx.com/i', $email) ||
        preg_match('/@me.com/i', $email)
    ){
        return false; // It's a publicly available email address
    }else{
        return true; // It's probably a company email address
    }
}
function your_validation_filter_func($result,$tag){
    $email = $_POST['company-email'];  //Get the input from email field with the id you defined when setting up the form

    if(!varified_email($email)){ // Isn't a varified email address (it matched the list of free email providers)
        $result->invalidate( $tag, "Please input a correct business email" );
    }
                
    return $result;
}
add_filter( 'wpcf7_validate_email*', 'your_validation_filter_func', 20, 2 ); // CF7 fillter

Key

change the name of the email field in the form to the one set in the function above (i.e. company-email in the example code above)

Show Comments