Custom validation to check business email and phone number in Contact Form 7 in wordpress Custom validation to check business email and phone number in Contact Form 7 in wordpress wordpress wordpress

Custom validation to check business email and phone number in Contact Form 7 in wordpress


Add following code to your theme's functions.php file.

    // Add custom validation for CF7 form fields    function is_company_email($email){ // Check against list of common public email providers & return true if the email provided *doesn't* match one of them            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){            $type = $tag['type'];            $name = $tag['name'];            if('yourid' == $type){ // Only apply to fields with the form field name of "company-email"                    $the_value = $_POST[$name];                    if(!is_company_email($the_value)){ // Isn't a company email address (it matched the list of free email providers)                            $result['valid'] = false;                            $result['reason'][$name] = 'You need to provide an email address that isn\'t hosted by a free provider.<br />Please contact us directly if this isn\'t possible.';                    }            }            return $result;    }   add_filter( 'wpcf7_validate_text', 'your_validation_filter_func', 10, 2 ); // Email field or contact number field   add_filter( 'wpcf7_validate_text*', 'your_validation_filter_func', 10, 2 ); // Req. Email field or contact number

You can achieve your desired result by the above code.

NOTE: I have validated only Email.You can do same for contact like I did for Email.

Answer for second problem:

Now as you have mentioned that you want it for only one form then you can do something like this:

wpcf7_add_shortcode( 'yourid', 'wpcf7_text_shortcode_handler', true );

Then, use a tag like this inside the form:

[yourid your-id-number-field] 

If you want to understand the tag syntax then go through this page.

Hope it helps you.


Use the following code, I've altered a bit, that will do the work,

if ( 'email' == $tag->basetype || 'email*' == $tag->basetype ) {            $arr = explode( '@', $_POST[$name] );            if( ! empty( $arr[1] ) ){                    $domain = strtolower( trim( $arr[1] ) );            } else {                    $domain = false;            }            if ( 'email*' == $type && '' == $_POST[$name] ) {                    $result['valid'] = false;                    $result['reason'][$name] = wpcf7_get_message( 'invalid_required' );            } elseif ( '' != $_POST[$name] && ! is_email( $_POST[$name] ) ) {                    $arr = explode( '@', $_POST[$name] );                    $result['valid'] = false;                    $result['reason'][$name] = wpcf7_get_message( 'invalid_email' );            } elseif ( $domain && in_array( $domain, $FREE_DOMAINS ) ) {                    $result['valid'] = false;                    $result['reason'][$name] = wpcf7_get_message( 'invalid_email' );            }    }

Paste the above code in text.php file inside contact form 7 plugin module.


Best answer found here. Define a function to check for the domains.

function is_business_email($email){    if (        preg_match('/@hotmail.com/i', $email) ||        preg_match('/@gmail.com/i', $email) ||        preg_match('/@yahoo.co/i', $email) ||        preg_match('/@yahoo.com/i', $email) ||        preg_match('/@mailinator.com/i', $email) ||        preg_match('/@gmail.co.in/i', $email) ||        preg_match('/@aol.com/i', $email) ||        preg_match('/@yandex.com/i', $email) ||        preg_match('/@msn.com/i', $email) ||        preg_match('/@gawab.com/i', $email) ||        preg_match('/@inbox.com/i', $email) ||        preg_match('/@gmx.com/i', $email) ||        preg_match('/@rediffmail.com/i', $email) ||        preg_match('/@in.com/i', $email) ||        preg_match('/@live.com/i', $email) ||        preg_match('/@hotmail.co.uk/i', $email) ||        preg_match('/@hotmail.fr/i', $email) ||        preg_match('/@yahoo.fr/i', $email) ||        preg_match('/@wanadoo.fr/i', $email) ||        preg_match('/@wanadoo.fr/i', $email) ||        preg_match('/@comcast.net/i', $email) ||        preg_match('/@yahoo.co.uk/i', $email) ||        preg_match('/@yahoo.com.br/i', $email) ||        preg_match('/@yahoo.co.in/i', $email) ||        preg_match('/@rediffmail.com/i', $email) ||        preg_match('/@free.fr/i', $email) ||        preg_match('/@gmx.de/i', $email) ||        preg_match('/@gmx.de/i', $email) ||        preg_match('/@yandex.ru/i', $email) ||        preg_match('/@ymail.com/i', $email) ||        preg_match('/@libero.it/i', $email) ||        preg_match('/@outlook.com/i', $email) ||        preg_match('/@uol.com.br/i', $email) ||        preg_match('/@bol.com.br/i', $email) ||        preg_match('/@mail.ru/i', $email) ||        preg_match('/@cox.net/i', $email) ||        preg_match('/@hotmail.it/i', $email) ||        preg_match('/@sbcglobal.net/i', $email) ||        preg_match('/@sfr.fr/i', $email) ||        preg_match('/@live.fr/i', $email) ||        preg_match('/@verizon.net/i', $email) ||        preg_match('/@live.co.uk/i', $email) ||        preg_match('/@googlemail.com/i', $email) ||        preg_match('/@yahoo.es/i', $email) ||        preg_match('/@ig.com.br/i', $email) ||        preg_match('/@live.nl/i', $email) ||        preg_match('/@bigpond.com/i', $email) ||        preg_match('/@terra.com.br/i', $email) ||        preg_match('/@yahoo.it/i', $email) ||        preg_match('/@neuf.fr/i', $email) ||        preg_match('/@yahoo.de/i', $email) ||        preg_match('/@aim.com/i', $email) ||        preg_match('/@bigpond.net.au/i', $email)    ) {        return false; // It is a free email address    } else {        return true; // It is likely a business email address    }}

Then hook into it

function custom_email_validation_filter($result, $tag){    $field_name = 'company-email';    $tag = new WPCF7_Shortcode($tag);    if ($field_name == $tag->name) {        $the_value = isset($_POST[$field_name]) ? trim($_POST[$field_name]) : "";        if (!is_business_email($the_value)) {            $result->invalidate($tag, "Please enter a valid business email");        }    }    return $result;}add_filter('wpcf7_validate_email', 'custom_email_validation_filter', 10, 2);add_filter('wpcf7_validate_email*', 'custom_email_validation_filter', 10, 2);

I'm checking for field names company-email you can change this for your case