User verification for checking email and phone number in contact form 7 in wordpress

I am new to wordpress and php, I use contact form 7 on my Wordpress site. In this I need to check the email address, block all free domains such as gmail, yahoo, etc. I need to check the Indian phone number with the country code.

I had 4 types of contact form, but I need this spot check for only one form. I googled and found this one , but it does not work. Someone please help me with this problem.

Thanks.

+5
source share
4 answers

Add the following code to your functions.php theme .

  // 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 a publicly available email address }else{ return true; // It 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 the desired result with the above code.

NOTE. I checked only Email . You can do the same for contact as for email.

Answer the second problem:

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

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

Then use this tag inside the form:

 [yourid your-id-number-field] 

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

Hope this helps you.

+5
source

Use the following code, I changed a bit that will do the job,

 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 into the text.php file inside the contact form module 7.

+1
source

Decision:

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

 contact-form-7/modules/text.php 

2) In your browser / text editor, press Ctrl + F , then search for the code below.

  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 eg: 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 the $ domains array, you can add any number of free email domains

4) Update contact form 7 text.php .

5) Block free domains. Use the code below in the "Change 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 !!!!

0
source

Thanks @ Palani Camaraj. But what if I want to block all subdomains of hosting providers, for example; google.co. **, yahoo.co. **, etc? My array values ​​continue to grow.

I found another solution for this and twice blew up the array first using "@" and then again using ".". So, in your code above for step 3, the code you can replace 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 eg: example@yahoo.co.in array(example, yahoo.co.in)*/ $lasta = explode('@', $value); /*once again explode yahoo.**.** if the previous explode is not null eg: 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' ) ); } } 

Thanks :-) Happy Coding :-)

0
source

Source: https://habr.com/ru/post/1204711/


All Articles