Title: Custom Email Validation not working
Last modified: August 30, 2016

---

# Custom Email Validation not working

 *  Resolved [navinarrows](https://wordpress.org/support/users/navinarrows/)
 * (@navinarrows)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/custom-email-validation-not-working/)
 * Hi,
 * I am trying to add a custom email validation in text.php which does not allow
   any public domain emails.
    Not sure what am I doing wrong here, its not working
 * [email* company-email placeholder “Business E-Mail”]
 *     ```
       // 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 custom_email_validation_filter($result,$tag){
       	$type = $tag['type'];
       	$name = $tag['name'];
   
       	$_POST[$name] = trim( strtr( (string) $_POST[$name], "\n", " " ) );
   
       	if('company-email' == $name){ // Only apply to fields with the form field name of "company-email"
       		if(!is_company_email( $_POST[$name] )){ // Isn't a company email address (it matched the list of free email providers)
       			$result['valid'] = false;
       			$result['reason'][$name] = 'Please enter a valid Business Email Address.';
       		}
       	}
       	return $result;
       }
   
       add_filter( 'wpcf7_validate_email', 'custom_email_validation_filter', 10, 2 );
       add_filter( 'wpcf7_validate_email*', 'custom_email_validation_filter', 10, 2 );
       ```
   
 * [https://wordpress.org/plugins/contact-form-7/](https://wordpress.org/plugins/contact-form-7/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [navinarrows](https://wordpress.org/support/users/navinarrows/)
 * (@navinarrows)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/custom-email-validation-not-working/#post-6259913)
 * Never mind, figured it out
 *     ```
       // 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 custom_email_validation_filter($result, $tag) {  
   
        $tag = new WPCF7_Shortcode( $tag );
   
          if ( 'company-email' == $tag->name ) {
   
        $the_value = isset( $_POST['company-email'] ) ? trim( $_POST['company-email'] ) : '';
   
                  if(!is_company_email($the_value)){
                            $result->invalidate( $tag, "Please Enter a valid Business Email ID" );
                  }
             }
              return $result;
        }
   
       add_filter( 'wpcf7_validate_email', 'custom_email_validation_filter', 10, 2 );
       add_filter( 'wpcf7_validate_email*', 'custom_email_validation_filter', 10, 2 );
       ```
   
 *  [ironbarrel](https://wordpress.org/support/users/ironbarrel/)
 * (@ironbarrel)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/custom-email-validation-not-working/#post-6260159)
 * Hello,
 * Please, sorry my ignorance, where do i have insert the code ?
    Thanks Romeo

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Custom Email Validation not working’ is closed to new replies.

 * ![](https://ps.w.org/contact-form-7/assets/icon.svg?rev=2339255)
 * [Contact Form 7](https://wordpress.org/plugins/contact-form-7/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/contact-form-7/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/contact-form-7/)
 * [Active Topics](https://wordpress.org/support/plugin/contact-form-7/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/contact-form-7/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/contact-form-7/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [ironbarrel](https://wordpress.org/support/users/ironbarrel/)
 * Last activity: [10 years, 10 months ago](https://wordpress.org/support/topic/custom-email-validation-not-working/#post-6260159)
 * Status: resolved