Email Validation Function Documentation

The validateEmail function is used to validate email addresses. It returns an object with two properties: "isValid" (boolean) and "errorMsg" (string). The "errorMsg" property will contain the error message if the email address is invalid, or it will be null if the email address is valid.

What is the difference between validateEmail and isEmail ? isEmail just does simple validations like checking if the string has the formatting of an email like several validators out there, validateEmail does not, it does complex validations and better than that, you can customize the validations passing the information in the parameters.

Import

The function can be imported using ES6 syntax from the "multiform-validator" package:

import { validateEmail } from "multiform-validator";

Alternatively, you can import the function using CommonJS syntax with require (Node.js):

const { validateEmail } = require("multiform-validator");

Function Signature

interface OptionsParams {
    maxLength?: number;
    country?: string;
    errorMsg?: (string | null)[];
    validDomains?: boolean | string[];
}

const defaultOptionsParams: OptionsParams = {
    maxLength: 400,
    country: "",
    errorMsg: defaultErrorMsg,
    validDomains: false,
};

function validateEmail(
	email: string,
	{
        maxLength,
        country,
        errorMsg,
        validDomains,
	}: OptionsParams = defaultOptionsParams,
): { isValid: boolean, errorMsg: string | null };

Parameters

Default list of valid domains

[
	'@gmail.com',
	'@outlook.com',
	'@yahoo.com',
	'@icloud.com',
	'@hotmail.com',
	'@mail.ru',
	'@yandex.ru',
	'@gmx.com',
	'@zoho.com',
	'@protonmail.com',
	'@protonmail.ch'
];

Default Error Messages

[
	'Invalid value passed',
	'This e-mail is not valid',
	'Email too big, try again',
	'This email is not valid in the country',
	'Email domain is not allowed.',
	'Unknown error',
]

Examples

const result1: ValidateFunctions = validateEmail("foor@bar.com", {
    maxLength: 30,
    country: "us",
});
console.log(result1);
// Output: { isValid: false, errorMsg: 'This email is not valid in the country' }

const customErrorMsg: string[] = [
    "Invalid format",
    "Invalid phone number",
    "Unknown error",
];
const result2: ValidateFunctions = validateEmail("foor@bar.com", {
    maxLength: 30,
    country: "br",
    errorMsg: customErrorMsg,
});
console.log(result2);
// Output: { isValid: false, errorMsg: 'This email is not valid in the country' }

const result3: ValidateFunctions = validateEmail("invalid", {
    maxLength: 30,
    errorMsg: ["My own error message"],
});
console.log(result3);
// Output: { isValid: false, errorMsg: 'This e-mail is not valid' }

const result4: ValidateFunctions = validateEmail("joao@myOwnDomain.com", {
    validDomains: ["@myOwnDomain.com"],
});
console.log(result4);
// Output: { isValid: true, errorMsg: null }

const result5: ValidateFunctions = validateEmail("joaoaoao@gmail.com.com", {
    validDomains: true,
});
console.log(result5);
// Output: { isValid: true, errorMsg: null }

const result6: ValidateFunctions = validateEmail("foo@bar.com", {
    maxLength: 25,
    validDomains: true,
});
console.log(result6);
// Output: { isValid: false, errorMsg: 'Email domain is not allowed.' }

const result7: ValidateFunctions = validateEmail("foo@gmail.com", {
    maxLength: 25,
    validDomains: ["@myownemail.com"],
});
console.log(result7);
// Output: { isValid: false, errorMsg: 'Email domain is not allowed.' }

const result8: ValidateFunctions = validateEmail("foo@gmail.com", {
    maxLength: 25,
    errorMsg: [null, null, null, null, "This is my own error for domain"],
    validDomains: ["@myownemail.com"],
});
console.log(result8);
// Output: { isValid: false, errorMsg: 'This is my own error for domain' }

const result9: ValidateFunctions = validateEmail("foo@myownemail.com", {
    maxLength: 25,
    errorMsg: [null, null, null, null, "This is my own error for domain"],
    validDomains: ["@myownemail.com"],
});
console.log(result9);
// Output: { isValid: true, errorMsg: null }

const result10: ValidateFunctions = validateEmail("foo@gmail.com", {
    maxLength: 25,
    errorMsg: ["OwnError1", "OwnError2", "OwnError3", "OwnError4", "OwnError5"],
    validDomains: ["@myownemail.com"],
});
console.log(result10);
// Output: { isValid: false, errorMsg: 'OwnError5' }

const result11: ValidateFunctions = validateEmail("foo@myownemail.com", {
    maxLength: 25,
    errorMsg: ["OwnError1", "OwnError2", "OwnError3", "OwnError4", "OwnError5"],
    validDomains: ["@myownemail.com"],
});
console.log(result11);
// Output: { isValid: true, errorMsg: null }