Domain validation for e-mail in WooCommerce checkout an registration forms

Further down in this article you will find a ready to use solution on how to properly validate customer e-mail addresses on the WooCommerce Checkout and WooCommerce Registration forms (both WooCommerce Classic and WooCommerce Blocks checkout are supported!) – if you’re not into reading, you can jump straight to the solution.

How a proper e-mail validation should work:

Code snippet: Additional e-mail validation for WooCommerce checkout and registration forms

//DoInWP - Function to check if e-mail address is valid and reachable
if(!function_exists('dwp_check_if_email_is_valid')) {
  function dwp_check_if_email_is_valid($email) {
      $email_precheck = !empty($email) && strpos($email, '@') !== false && filter_var($email, FILTER_VALIDATE_EMAIL);
      if($email_precheck === FALSE) {
          return false;
      }
      $email_domain = preg_replace('/^.+?@/', '', $email);
      $email_domains_whitelist = ['gmail.com', 'yahoo.com', 'outlook.com', 'icloud.com'];
      $email_domains_blacklist = ['gmai.com', 'gmeil.com', 
        'gmail.co', 'gmaii.com', 'gmailc.com', 'gmil.com', 'gmsil.com', 'gnail.com',
        'icloudl.com', 'yaho.com', 'example.com'];
      if(in_array($email_domain, $email_domains_whitelist)) {
          return true;
      }
      if(in_array($email_domain, $email_domains_blacklist) || (!checkdnsrr($email_domain.'.', 'MX') && !checkdnsrr($email_domain.'.', 'A'))){
          return false;
      } 
      return true;
  }
}

//DoInWP - E-mail Validation for WooCommerce Classic Checkout
add_action('woocommerce_checkout_process', function() {
    if(isset($_POST['billing_email']) && !dwp_check_if_email_is_valid($_POST['billing_email'])) {
        wc_add_notice( esc_html__( 'You entered an invalid or non-reachable e-mail address. Please check it and try again.', 'woocommerce' ), 'error' );
    } 
});

//DoInWP - E-mail Validation for WooCommerce Blocks Checkout
add_action('woocommerce_store_api_checkout_update_order_from_request', function($order, $request) {
    $email = $order->get_billing_email();
     if(!dwp_check_if_email_is_valid($email)) {
        throw new \Automattic\WooCommerce\StoreApi\Exceptions\RouteException(
                'woocommerce_rest_checkout_invalid_email_domain',
                esc_html__( 'You entered an invalid or non-reachable e-mail address. Please check it and try again.', 'woocommerce' ),
                400
        );
     }
}, 10, 2);


//DoInWP - E-mail Validation on WooCommerce Registration Form
add_filter('woocommerce_registration_errors', function($errors, $username, $email) {
     if(empty($email)) {
        return $errors;
    }
    if(!dwp_check_if_email_is_valid($email)) {
            $errors->add(
                'woocommerce_registration_error_invalid_email_domain',
                esc_html__('You entered an invalid or non-reachable e-mail address. Please check it and try again.', 'woocommerce'));
    }
    return $errors;            
}, 10, 3);

4 Comments on Domain validation for e-mail in WooCommerce checkout an registration forms
About the author
I'm a full-stack WordPress developer with a 10+ years of solid experience in the core web development languages, development processes / techniques, web security, Linux server management and with pretty good understanding about proper semantics, UX/UI, technical SEO, good design and basic knowledge of company leadership. On top of that - a distant 5+ years experience as a computer and electronics repair technician which often enables me to understand also how the things work at the hardware level.

4 thoughts on “Domain validation for e-mail in WooCommerce checkout an registration forms

  1. One other question I have for you regarding this code.
    Does it validate the email as soon as you type it in and tab to the next field or do you have to press CHECHOUT/PLACE ORDER on the checkout page before it does its thing?
    It is not working either way for me at the moment unless you have a fix?
    I thought it might be the Woocommerce Checkout Manager plugin that was causing the issue but I deactivated it and no difference

  2. Thanks for providing this post and the code.
    Unfortunately I tried pasting it into my functions.php file and it did not do anything. Invalid emails were still allowed and I am not sure why. I am not an expert by any means on code snippets so I may not have done something I was supposed to do.

    1. Hey Glenn,

      First of all – a big thanks for reaching out and pointing out the problem! 🙂 The solution was a bit outdated and did not include the support for the more-recent WooCommerce Blocks Checkout which was released on November 2023 (it was only working for WooCommerce classic checkout forms). In reply to your comment, I’ve updated the code snippet with more universal solution which support both WooCommerce checkout mechanisms and will validate e-mails also on WooCommerce registration form.

Your feedback matters!…
I hope you found this article helpful. Feel free to add some comments - your feedback is very important to me, as it drives my motivation and helps me to improve the content.