Email Validator provides a fast, pretty robust e-mail validator. Only checks form, not function. It implements RFC-compliant length checks for local parts, domain parts, and individual domain segments, while using a comprehensive regex pattern to validate email structure including special characters and proper domain formatting.
npm install email-validatorconst validator = require("email-validator");For ES6 modules:
import * as EmailValidator from 'email-validator';Named imports:
const { validate, tester } = require("email-validator");import { validate, tester } from 'email-validator';const validator = require("email-validator");
// Validate email addresses
const isValid1 = validator.validate("test@email.com"); // true
const isValid2 = validator.validate("invalid-email"); // false
const isValid3 = validator.validate("user@domain.co.uk"); // true
// Using named imports
const { validate } = require("email-validator");
const result = validate("example@domain.org"); // trueValidates an email address format using regex pattern and RFC-compliant length checks.
/**
* Validate an email address.
* @param {string} email - The email address to validate.
* @returns {boolean} - Returns true if email format is valid, false otherwise.
*/
function validate(email: string): boolean;Validation Rules:
Usage Examples:
const { validate } = require("email-validator");
// Valid emails
validate("user@example.com"); // true
validate("test.email+tag@domain.co.uk"); // true
validate("user123@sub.domain.org"); // true
validate("a@single-character-in-local.org"); // true
// Invalid emails
validate("@missing-local.org"); // false
validate("missing-at-sign.net"); // false
validate("user@"); // false
validate(""); // false
validate(null); // false
// Length validation
validate("the-local-part-is-invalid-if-it-is-longer-than-sixty-four-characters@sld.net"); // false
validate("user@the-domain-part-is-invalid-if-it-is-longer-than-two-hundred-and-fifty-five-characters.and-this-domain-is-way-too-long.to-be-considered-valid.according-to-rfc-standards.so-it-should-return-false.example.org"); // falseAccess to the underlying regular expression pattern used for email validation.
/**
* Regular expression pattern used for email format validation
*/
const tester: RegExp;The regex pattern: /^[-!#$%&'*+\/0-9=?A-Z^_a-z{|}~](.?[-!#$%&'*+/0-9=?A-Z^_a-z{|}~])*@[a-zA-Z0-9](-*\.?[a-zA-Z0-9])*\.[a-zA-Z](-?[a-zA-Z0-9])+$/
Usage Examples:
const { tester } = require("email-validator");
// Direct regex testing
const isMatch = tester.test("user@example.com"); // true
// Custom validation with additional checks
function customValidate(email) {
if (!email || typeof email !== 'string') return false;
return tester.test(email) && email.length <= 254; // Additional overall length check
}For TypeScript users, the package includes type definitions:
/**
* Validate an email address.
* @param email - The email address to validate.
* @returns boolean indicating if email format is valid
*/
export function validate(email: string): boolean;
/**
* Regular expression pattern used for email validation
*/
export const tester: RegExp;