Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 668
Gopinath Perumal Avatar asked Oct 15 '14 07:10

Gopinath Perumal


People also ask

How do I create a custom validation in contact form 7?

In Contact Form 7, a user-input validation is implemented as a filter function. The filter hook used for the validation varies depending on the type of form-tag and is determined as: wpcf7_validate_ + {type of the form-tag}. So, for text form-tags, the filter hook wpcf7_validate_text is used.

How can I add my mobile number in contact form 7?

If you would like to add additional fields, like a phone number or company name, you can do so by utilizing the options across the top. For example, if you are wondering how to add a phone number in Contact Form 7, select the tel option. A window will pop up with several options.


1 Answers

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.

like image 102
Rohil_PHPBeginner Avatar answered Nov 08 '22 21:11

Rohil_PHPBeginner