• Resolved navinarrows

    (@navinarrows)


    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/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter navinarrows

    (@navinarrows)

    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 );

    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.