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

5

7

I am new to wordpress and php, I'm using contact form 7 in my wordpress website. In that I need to validate the email address, block all free domains like gmail, yahoo,etc., I need to validate Indian phone number with country code.

I had 4 types of contact form but I need this custom validation for only one form. I googled and found this but it is not working. Someone please help me with this issue.

Thanks.

Caddis answered 15/10, 2014 at 7:39 Comment(0)
N
7

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.

Nihon answered 15/10, 2014 at 10:33 Comment(4)
Hey Rohil, I tried your code, but it doesn't work. :( I've copy + pasted it and installed it as a separate plugin. I can still enter every email address in the [email] field. What can I do?Holst
Make sure you have activated that plugin and if it is activated then please do some debugging. :)Nihon
Of course, it is activated. :) Debugging is easier said than done. Never did anything in PHP, just Ruby and JS....Holst
I wont mind to help you out of this but its difficult for me guide you through comments. Still here an attempt : 1) Make sure you entered field ID (yourid in if condition). 2) If you made necessary changes in above code, then try to see value of $type and $name by adding var_dump($type) just below where is defined and then addexit;. Just show you starting point.Nihon
O
2

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

Oecd answered 8/6, 2021 at 20:28 Comment(0)
S
1

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.

Shipworm answered 15/10, 2014 at 7:50 Comment(2)
There's absolutely no need to modify core CF7 functionality to achieve this. What happens when the OP wants to update the plugin?Waldman
@mevius Pls say a way to achieve this.Caddis
R
0

Solution :

1) Open your contact form 7 plugin text.php file,

contact-form-7/modules/text.php

2) In your browser/Text Editor, Press Ctrl+F, then search for the below code.

   if ( 'email' == $tag->basetype ) {
       if ( $tag->is_required() && '' == $value ) {
          $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
        } 
       elseif ( '' != $value && ! wpcf7_is_email( $value ) ) {
        $result->invalidate( $tag, wpcf7_get_message( 'invalid_email' ));
        }       
      }

3) Replace the above code with

if ( 'email' == $tag->basetype ) {

    /*add the domain names you want to block in the $domains array*/
    $domains = array("gmail.com","yahoo.com","yahoo.co.in");
    /*explode will store the string into array
         e.g: [email protected]
                array(example, gmail.com)*/
    $udomain = explode('@', $value);

    //select the email domain from the above splitted array
    $email_domain = $udomain[1];

    // check name is 'company-email' else default validation will work
    if('company-email' == $tag->name) {
        //check entered value = $value exists in $domain array
        if(in_array($email_domain, $domains)) {
            //display error
            $result->invalidate( $tag, "Please enter your company email address" );
        }
    }

    //email field is empty
    if ( $tag->is_required() && '' == $value ) {
        $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );   
    } 

    //check basic email validation
   elseif ( '' != $value && ! wpcf7_is_email( $value ) ) {
        $result->invalidate( $tag, wpcf7_get_message( 'invalid_email' ) );
    }
}

Note: I

In $domains array, you can add any number of free email domains

4) Update your contact form 7 text.php file.

5) To block free domains. Use the below code in Edit contact form,

  [email* company-email class:yourclass ] //bock free domains
  [email* your-email class:yourclass ] //default contact form 7 validation


 Note: If we use 'company-email', it will block free domains
       If we use 'your-email'/'someothername', default contact  form email validation will work.

6) Enjoy using wordpress !!!!

Reluctivity answered 22/11, 2015 at 8:53 Comment(0)
H
0

Thanks @Palani Kamaraj. But what if I want to block all the sub-domain's of free hosting providers, like; google.co.** , yahoo.co.**, etc ? My array values, goes on increasing.

I found another solution for this, and exploded the array twice first with '@' then later again with '.' So in your code above for step 3, the code you can replace with below

if ( 'email' == $tag->basetype ) {

    /*add the domain names you want to block in the $domains array*/
    $domains = array("gmail","yahoo","hotmail","aol","yahoo", "email", "ymail", "live", "msn");

    /*explode will store the string into array
         e.g: [email protected]
                array(example, yahoo.co.in)*/
    $lasta = explode('@', $value);

   /*once again explode yahoo.**.** if the previous explode is not null
         e.g: yahoo.co.in
                array(yahoo, co, in)*/

    if ($lasta != "") {
       $host = explode('.',$lasta[1]);                      
    }

   /* select the email domain from the above splitted array eg: yahoo */
    $email_domain = $host[0];


    // check name is 'company-email' else default validation will work
    if('company-email' == $tag->name) {
        //check entered value = $value exists in $domain array
        if(in_array($email_domain, $domains)) {
            //display error
            $result->invalidate( $tag, "Please enter your company email address" );
        }
    }

    //email field is empty
    if ( $tag->is_required() && '' == $value ) {
        $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );   
    } 

    //check basic email validation
   elseif ( '' != $value && ! wpcf7_is_email( $value ) ) {
        $result->invalidate( $tag, wpcf7_get_message( 'invalid_email' ) );
    }
}

Thank You :-) Happy Coding :-)

Homily answered 23/12, 2016 at 10:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.