Skip to content

Repository files navigation

📨 Email Address Validator

This lightweight module provides precise email address validation, plus optional MX record verification of the domain, returning a Boolean value (true or false).

🟢 » Node.js

Installation via npm

npm install @sefinek/email-validator

Installation via yarn

yarn add @sefinek/email-validator

Example

import emailValidator from '@sefinek/email-validator';

const TEST_EMAIL = 'contact@sefinek.net';
if (emailValidator(TEST_EMAIL)) {
    console.log(`Email ${TEST_EMAIL} is valid.`);
} else {
    console.log(`Email ${TEST_EMAIL} is NOT valid!`);
}

The package is ESM. require() works too on Node.js 22.12+ and returns the function directly, without .default:

const emailValidator = require('@sefinek/email-validator');

MX record verification (Node.js only)

Beyond syntax validation, you can check whether the domain actually accepts mail by verifying its MX records. The function lives under the /mx subpath because it depends on node:dns and is therefore not included in the browser bundle. It accepts a full email address (the domain after the last @ is used) or a bare domain. It never throws and resolves exclusively to a Boolean.

import emailValidator from '@sefinek/email-validator';
import verifyMx from '@sefinek/email-validator/mx';

const TEST_EMAIL = 'contact@sefinek.net';

// Cheap, synchronous syntax check first; only hit DNS if it passes.
if (emailValidator(TEST_EMAIL) && await verifyMx(TEST_EMAIL)) {
    console.log(`Email ${TEST_EMAIL} is valid and the domain accepts mail.`);
}

By default, the system resolvers are used. You can query specific DNS servers (and tune timeout/tries) via the optional second argument. This uses a dedicated resolver and never mutates the global DNS configuration:

await verifyMx('contact@sefinek.net', {
    servers: ['1.1.1.1', '8.8.8.8'], // each entry may include a port, e.g. '8.8.8.8:53'
    timeout: 2000,                   // ms per query before a retry (-1 = system default)
    tries: 2,                        // attempts before giving up
    validateSyntax: true,            // reject syntactically invalid addresses before any DNS lookup
    failOpen: true,                  // resolve to true when the lookup fails transiently (timeout, SERVFAIL)
});

A domain passes when it has an MX record or, per RFC 5321, no MX but an A/AAAA record (implicit MX). A null MX (MX 0 ., RFC 7505) means the domain accepts no mail and fails. Internationalized domains are converted to punycode first.

Only definitive answers (the domain does not exist, or has no mail records) always resolve to false. When the lookup fails transiently (timeout, SERVFAIL), the result is unknown: it resolves to false by default, or to true with failOpen: true, which avoids rejecting real users during a DNS outage.

With validateSyntax: true, the input is first checked against the syntax validator and an invalid address resolves to false without a DNS query. Note that this requires a full email address, so a bare domain is rejected when the flag is enabled.

🌍 » Browser

jsdelivr.net

https://cdn.jsdelivr.net/npm/@sefinek/email-validator@3/dist/email-validator.min.js

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>email-validator</title>
</head>
<body>
    <h1>email-validator</h1>

    <script src="https://cdn.jsdelivr.net/npm/@sefinek/email-validator@3/dist/email-validator.min.js"></script>
    <script>
        const email = 'contact@sefinek.net';

        if (emailValidator(email)) {
            console.log(`✔️ Email ${email} is valid.`);
        } else {
            console.log(`❎ Email ${email} is NOT valid!`);
        }
    </script>
</body>
</html>

Demo

https://sefinek.net/projects/email-validator

⭐ » Thank you

If you find this module helpful, please consider giving the repository a star. For any questions or issues, please create a new Issue.

🔑 License

Copyright © Sefinek. Licensed under the MIT License.

Releases

Sponsor this project

Used by

Contributors

Languages