How to validate email addresses in Node.js

Not all email validation is created equal. Most email validation simply checks the syntax of the email address. This is not very useful because the email address may not exist — but it will still show a valid syntax check. With this approach we will not only check whether the syntax of an email address is correct, but we will also check if the email address exists.

The first step is to add a reference in our package.json

"dependencies": {
"cloudmersive-validate-api-client": "^1.1.2"
}

From here, we will import the appropriate library:

var CloudmersiveValidateApiClient = require('cloudmersive-validate-api-client');

Finally, we will make a quick call to the emailFullValidation method:

var defaultClient = CloudmersiveValidateApiClient.ApiClient.instance;// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveValidateApiClient.EmailApi();var email = "email_example"; // String | Email address to validate, e.g. \"support@cloudmersive.com\". The input is a string so be sure to enclose it in double-quotes.var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.emailFullValidation(email, callback);

This will give us the exact result that we need:

{
"ValidAddress": true,
"MailServerUsedForValidation": "mx.zoho.com"
}

Note that it tells us that it was a valid address, as well as which server was used for validation. It also checks for syntactic correctness prior to connecting to the remote server. It’s that easy!

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store