Viewing 1 replies (of 1 total)
  • Paste this one into your functions.php
    Please note that I have changed my email field name from [your-email] to [company-email]

    // 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) ||
    		preg_match('/@icloud.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 your Company E-Mail 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 ); 
Viewing 1 replies (of 1 total)
  • The topic ‘Validating Contact form by not accepting Gmail Emails’ is closed to new replies.