JavaScript simple email validation using check-more-types library
In this JavaScript tutorial we learn how to simply validate an email address using the check-more-types library.
Create the sample project
Firstly we create an empty project with the following command.
mkdir sample-project
cd sample-project
npm init -y
And install the check-more-types module using the following command.
npm install check-more-types --save
How to validate email address in JavaScript
In the following JavaScript code we show you how to use the check-more-types library to check if an email address is valid or not.
In the sample-project directory create a new file named index.js as follows.
index.js
var check = require('check-more-types');
var result1 = check.email('contact@toricode.com');
var result2 = check.email('contact@toricode');
var result3 = check.email('contact.toricode.com');
console.log("contact@toricode.com validation result: " + result1);
console.log("contact@toricode validation result: " + result2);
console.log("contact.toricode.com validation result: " + result3);
Run the index.js file using the following command.
node index.js
contact@toricode.com validation result: true
contact@toricode validation result: false
contact.toricode.com validation result: false
More examples of using check-more-types library to check email address
index.js
var check = require('check-more-types');
if(check.email('info@toricode.com')) {
console.log('The email is valid!');
}
console.log(check.email('tori code'));
console.log(check.email('javascript@toricode.com'));
Run the index.js file using the following command.
node index.js
The email is valid!
false
true
Happy Coding 😊