Domain validation for e-mail in WooCommerce checkout form

Providing an invalid e-mail address is one of the most common mistakes people do when shopping online and WooCommerce is not an exception. It is a mistake so common, that at least 1 of the 100 people will make a typo in their e-mail address when filling out a checkout form.

Woocommerce does not validate e-mails by domain

Its 2024 and WooCommerce still does not do a proper domain validation for e-mail addresses and that is one of the reasons why WooCommerce store owners often get calls from their customers about order not being placed. All it does is a basic check if the provided e-mail address is in a correct format.

How to properly deal with the problem:

  1. Reduce the chance for user to enter an incorrect e-mail address – make the e-mail field larger, with large enough, easy-to-read text and contrasting colors so it is easy to spot typos.
  2. Check the entered e-mail address against common syntax errors – make sure it is provided in a proper format (WooCommerce already does that so skip to next step)
  3. Check the domain of entered e-mail address against whitelist of domain names used by common e-mail providers (e.g. gmail.com, outlook.com, yahoo.com and so on)
  4. Check the domain of entered e-mail address against blacklist of domain names (e.g. gnail.com, gmai.com, gail.com, outloo.com and so on). This blacklist is important because for each known e-mail provider there are tons of similar domain names which are active and validity of the entered e-mail address can’t be ensured in other ways. An e-mail address may be valid, but if the domain is similar to a known e-mail provider – it is a high chance that the user has made a typo.
  5. Check if the domain of entered e-mail address is active by querying its DNS records. This is also very important when dealing with user made typos.

Code snippet: Additional e-mail validation for WooCommerce checkout form

Just place this code in your theme functions.php file or add it as a must-use plugin. This code snippet will do additional validation for the billing e-mail address input field on WooCommerce checkout form by checking if a domain of the e-mail provider is valid (whitelist check, blacklist check and validation of domain DNS records)

add_action('woocommerce_checkout_process', 'dwp_email_validation_on_checkout');
function dwp_email_validation_on_checkout() {
     $email = $_POST['billing_email']; 
     //basic precheck if the entered e-mail address is valid
     if(!empty($email) && strpos($email, '@') !== false && filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $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'];
        if(in_array($email_domain, $email_domains_whitelist)) {
            $res = true;
        }
        if(in_array($email_domain, $email_domains_blacklist) || (!checkdnsrr($email_domain.'.', 'MX') && !checkdnsrr($email_domain.'.', 'A'))){
            $res = false;
        } 
        $res = true;

        if(empty($res)) {
		     wc_add_notice( esc_html__( 'You entered an invalid or non-reachable e-mail address. Please check it and try again.', 'woocommerce' ), 'error' );
        }
     }
}

Leave a Comment on Domain validation for e-mail in WooCommerce checkout form
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.
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.