Custom validation to check business email and phon

2019-05-01 16:42发布

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.

4条回答
霸刀☆藐视天下
2楼-- · 2019-05-01 17:17

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: example@gmail.com
                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 !!!!

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-05-01 17:37

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.

查看更多
祖国的老花朵
4楼-- · 2019-05-01 17:40

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.

查看更多
淡お忘
5楼-- · 2019-05-01 17:40

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: example@yahoo.co.in
                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 :-)

查看更多
登录 后发表回答