Node Email Validation: How to Verify User Email in Node.JS

Last Updated Mar 08, 2022
Emma Jagger

Engineer, maker, Google alumna, CMU grad

Email address verification is very important for the success of your business. It helps you to find real clients. Without it, your database will be filled with fake email IDs. 

Most systems implement the verification functionality by running a simple regex check against the email IDs. However, it won’t be enough to stop fraudulent activities. With regex or regular expression, you can eliminate syntactically incorrect email addresses. But how do you know whether the remaining IDs exist or not? Even if the entered address is syntactically correct, it might not be active anymore. Also, there are plenty of temporary email address providers, which are very popular among spammers. How can you stop them?

You can think about sending a confirmation email once the user registers into your site. But it will not be enough to stop spammers. They can simply log into their email IDs and click on the link to verify their identity. How can you verify the email address in Node.js without sending the confirmation email to the users? In this post, you will find all the details of Node email validation. 

Related: How to Validate a Phone Number in Node.js

Don't reinvent the wheel.
Abstract's APIs are production-ready now.

Abstract's suite of API's are built to save you time. You don't need to be an expert in email validation, IP geolocation, etc. Just focus on writing code that's actually valuable for your app or business, and we'll handle the rest.

Get started for free

Why is Verifying An Email Address Important with Node.js?

Verifying an email address is important because it helps you to eliminate fake IDs from your database. So, you can get access to real clients and grow your business. Also, it prevents spammers from signing up using disposable email IDs, like those from Mailinator. You don’t want to see fake people registering on your site. Your business will fail miserably. To ensure that your database is clean and you are interacting with only the real people, it’s very important to verify email addresses with Node.js and perform Node email validation.

How Do I Check if An Email is Valid in Node?

You can verify email addresses in Node and perform Node email validation easily by utilizing Deep Email Validator. It can perform the validation effectively by checking the local part for common typos, DNS records, and the SMTP server response. Also, it can determine if the email ID is generated by disposable email services - it should return true. 

You just need to follow these steps:

1. First, you have to install Deep Email Validator by using this command:

npm install deep-email-validator

2. Then you have to create the logic for performing the verification. It will look like this:


const express = require('express');
const router = express.Router();
const emailValidator = require('deep-email-validator');

async function isEmailValid(email) {
  return emailValidator.validate(email)
}

3. When the asynchronous function is run, you will get this response:


{
  valid: false,
  validators: {
    regex: { valid: true },
    typo: { valid: true },
    disposable: { valid: true },
    mx: { valid: true },
    smtp: { valid: false, reason: 'Mailbox not found.' }
  },
  reason: 'smtp'
}

As you can see, Deep Email Validator runs five different checks to ensure the validity of the email address. In this case, the ID is invalid because there is an issue with SMTP. The mailbox is not found. That’s why the valid field is set to false and the reason field is set to smtp. It should not return false.

4. Now, you can create a simple route by utilizing the logic shown in the previous step:


router.post('/register', async function(req, res, next) {
  const {email, password} = req.body;

  if (!email || !password){
    return res.status(400).send({
      message: "Email or password missing."
    })
  }

  const {valid, reason, validators} = await isEmailValid(email);

  if (valid) return res.send({message: "OK"});

  return res.status(400).send({
    message: "Please provide a valid email address.",
    reason: validators[reason].reason
  })

});

Here, you have defined a POST method for preventing the use of fake email addresses during user registration. If the email ID or password is not entered, a message will be displayed accordingly. Also, if an invalid email address is entered, the reason behind the issue will be displayed.  

That’s how you utilize Deep Email Validator with Node.js. By using this code, you can verify the email address effectively and perform Node email validation.  

What the Node email validation code works for

Detecting common typos

Deep Email Validator utilizes Mailcheck, which is a simple email typo-detection library. It comes with a list of common domain names. It can detect common typos by using the Sift4 algorithm to compare the domain name of the given email address to the existing list.

How does the Sift4 algorithm work? It works by determining Levenshtein Distance, which is the minimum number of single-character edits required to be made for transforming one string to another. For example, let’s consider these two email addresses: 

'mail@yahoo.com'
'mail@yahooo.com'

As you can see, the second email ID is invalid, as it has an extra ‘o.’ If you delete it, both of the email addresses will become the same. So, it takes only one edit to transform the second string to the first one. That’s why they have a Levenshtein Distance of 1. 

Detecting a temporary email address

Temporary email addresses are identified by utilizing a package, called disposable-email-addresses. It contains a list of common disposable email address hosts, which are scraped from Wikipedia disposable email service list. They are matched against the provided email address to ensure validity.

Read More: How to do Web Scraping in Node.js

Checking for MX records

Mail Exchange (MX) records are DNS records. They are used to specify a mail server to handle a domain's email. You must configure it to receive emails to your domain. 

Deep Email Validator utilizes Node’s in-built ‘dns’ module internally to query the DNS servers and determine the validity of MX records. Domains with invalid MX records will produce an error. 

Determining whether an SMTP server is online

SMTP stands for Simple Mail Transfer Protocol. Mail servers use it to send, receive, and relay outgoing mail between email senders and receivers.

Deep Email Validator utilizes Node’s in-built ‘net’ library to connect to the default SMTP port 25. Then it queries to determine the existence of the given mailbox. If everything is fine, the server responds with a 250 OK message - that's a great sign. 

Abstract’s Email Validation and Verification API verifies the email addresses on your database effectively by checking typos, SMTP server response, and MX records. Also, it looks for disposable email ID providers. Try it now for free.

Best Node Email Validation Packages

There are plenty of Node email validation packages for Node.js. But which is the best Node email validation package? Let’s take a look at them.

1. Kickbox

Kickbox is a Node email validation package which helps you to reduce bounce rates by eliminating invalid email addresses. Also, it prevents spammers from registering into your Node.js application by using fake IDs. 

2. Node-email-validation

Node-email-validation is a simple module for verifying email IDs. It is fast, robust, and easy to use. 

3. Sumars-js

Sumars-js is a simple user management and registration system for Node.js. It enables you to verify the email addresses used for registration conveniently.

4. Isemail

Isemail is an email address validation library for Node.js. It helps you to validate the IDs according to RFCs 5321, 5322, and others.

5. Email-addresses

Email-addresses is a Node.js library. You can use it to parse the email IDs using the grammar specified in RFC 5322. In this way, you can easily check whether the user-provided email address is genuine or not. 

Alternatives to Node Email Validation

The solution that has been mentioned in this post works pretty well. However, there is a limitation. It will require extra time and effort for maintaining the code. That means you will have to go through extra hassles. Is there any alternative way that can make your life a lot easier?

You can try using Abstract’s Email Validation and Verification API. It is lightweight and super-fast. But more importantly, it is completely hassle-free. It doesn’t need extra effort for maintaining the code. Abstract deals with everything. So, you can save a lot of time. 

Let’s take a look at a practical example by verifying this email address with Abstract’s Email Validation and Verification API: johnsmith@gmail.com


https://emailvalidation.abstractapi.com/v1/
  ? api_key = YOUR_UNIQUE_API_KEY
  & email = johnsmith@gmail.com

If the request is successful, you will get this JSON response:


{
  "email": "johnsmith@gmail.com",
  "autocorrect": "",
  "is_valid_format": true,
  "is_free_email": true,
  "is_disposable_email": false,
  "is_role_email": false,
  "is_catchall_email": false,
  "is_mx_found": true,
  "is_smtp_valid": true,
  "quality_score": 0.99,
}

Notice that "is_valid_format" is set to true. That means the given email address has the correct format. Also, you get a variety of useful data, like quality score, email role, SMTP validation, etc.

As you can see, Abstract’s Email Validation and Verification has made the entire process very simple. It doesn’t require you to write any code from scratch. You just need to get the API key and make a request for verifying the email address. So, you don’t have to worry about spending extra time maintaining the code. It can make your life a lot easier.

Wrapping up

That’s how you validate email addresses in Node.js. You can use the Deep Email Validator package to perform the verification. However, the easiest way is using an email validation API. It will enable you to avoid all the extra hassles and perform validation effortlessly.

Node Email Validation FAQs

How do I know if a Node.js email is real?

You can identify the real email address in Node.js easily by using the Deep Email Validator package. It can eliminate fake email IDs efficiently by checking common typos, DNS records, and the SMTP server response. However, it will require you to maintain the code, which means extra hassle.

If you don’t want to face hassle, you can try using Abstract’s Email Validation and Verification API. It will allow you to detect the real email IDs without worrying about maintaining the code. 

Can you check if an email address is active?

You can check if an email address is active by pinging the email box via the SMTP connection. Simply connect to the SMTP server and request if the email ID exists. If it’s active, you will get the “250 OK” response. If it’s no longer active, you will get a negative response, like this: “550-5.1.1 User Unknown.”

The easiest way to find the active email address is using one of the best email validation APIs. You just need to make an API call and pass the email ID. If you get a positive response, like "is_smtp_valid": true, the email ID is fully active.

Validate email instantly using Abstract's email verification API.

Get started for free
Validate email instantly using Abstract's email verification API.
Get started