inboxok
v1.2.0
Published
Email validation for forms: syntax, disposable domains, typo suggestions ('Did you mean [email protected]?'), role and free-provider detection. Synchronous, no DNS, runs in browsers, edge and Node. Zero dependencies.
Downloads
527
Maintainers
Readme
inboxok
Email validation for forms: checks the syntax, blocks throwaway inboxes and catches typos.
Many email validators only return true or false. inboxok tells you why an address failed, gives you a message you can show to the user, and suggests a fix for common typos (Did you mean [email protected]?). It is synchronous and uses no network, so you can run it in the browser while the user types, and again on the server.
Need DNS/MX checks on the server? Use validmail. inboxok is the fast check you run in the form. validmail does the network lookups on the server.
Features
- Clear errors: a
reasoncode for your code and amessagefor your users, not justfalse. - Typo suggestions:
gmial.com,hotmal.com,gmail.con,gmail,comandgmailcombecome the right address. Real domains likeemail.comoracme.coare left alone. - Disposable email blocking: a list of about 430 throwaway inbox providers (subdomains match too), plus your own block and allow lists.
- Practical syntax rules: RFC 5321/5322 length limits, dot rules, valid domain endings, Unicode addresses (
josé@exämple.de). Quoted local parts and IP addresses are opt-in. - Catches copy-paste junk: zero-width spaces, non-breaking spaces and control characters.
- Role and free-provider detection:
admin@,noreply@; Gmail, Outlook, Yahoo and more, for "use your work email" forms. normalize()for duplicates:[email protected]becomes[email protected].- Safe on any input: no regular expressions over the whole input, so no ReDoS (a slow-regex attack). A 100,000-character input takes well under a millisecond.
- Runs anywhere: browsers, edge runtimes, Node.js, Bun and Deno. Zero dependencies, about 8 KB gzipped including the lists.
Installation
npm install inboxokpnpm add inboxok
yarn add inboxok
bun add inboxokQuick start
import inboxok from 'inboxok'
// CommonJS: const inboxok = require('inboxok')
inboxok('[email protected]').valid
// => true
inboxok('[email protected]').message
// => 'Did you mean [email protected]?'
inboxok('[email protected]')
// => { valid: false, reason: 'disposable', message: 'Disposable email addresses are not allowed.', ... }
inboxok('ana@@example.com').message
// => 'Email address can only have one @.'
inboxok('ana@gmail,com').message
// => 'Email address can\'t contain ",". Did you mean [email protected]?'Table of contents
Usage
Validate a sign-up form
validate() (the same function as the default export) never throws. It returns valid, a reason code when the address is invalid, and a message for the user.
import { validate } from 'inboxok'
function checkEmail(value) {
const result = validate(value)
if (!result.valid) return { error: result.message }
return { email: result.email } // trimmed, domain lowercased
}
checkEmail('')
// => { error: 'Enter an email address.' }
checkEmail('ana [email protected]')
// => { error: "Email address can't contain spaces." }
checkEmail(' [email protected] ')
// => { email: '[email protected]' }Any value works as input. null and undefined give reason: 'empty'. Other non-strings (a number, a File from FormData) give reason: 'syntax'.
Show a "Did you mean ...?" hint
When the domain looks misspelled, the result has a suggestion. The address can still be valid (gmial.com is a real domain shape), so show the hint without blocking the form.
import { validate, suggest } from 'inboxok'
const result = validate('[email protected]')
result.valid // => true
result.suggestion // => '[email protected]'
result.message // => 'Did you mean [email protected]?'
// Or get only the suggestion:
suggest('[email protected]') // => '[email protected]'
suggest('[email protected]') // => '[email protected]'
suggest('[email protected]') // => undefined (a real provider, not a typo)Add your own domains so that typos of them are caught too:
suggest('[email protected]', { domains: ['acmecorp.com'] })
// => '[email protected]'Block disposable emails
Disposable (throwaway) addresses are rejected by default. Subdomains match too. The result always has a disposable flag, even when you turn blocking off.
import { validate, isDisposable } from 'inboxok'
validate('[email protected]').reason
// => 'disposable'
validate('[email protected]', { blockDisposable: false }).disposable
// => true
isDisposable('inbox.mailinator.com')
// => trueAdd your own domains with blockDomains, or make exceptions with allowDomains:
validate('[email protected]', { blockDomains: ['example.com'] }).message
// => 'Email addresses from sub.example.com are not allowed.'
validate('[email protected]', { allowDomains: ['mailinator.com'] }).valid
// => trueThe built-in list is small on purpose. It covers the providers people actually use (Mailinator, Guerrilla Mail, 10 Minute Mail, YOPmail, Temp-Mail, TrashMail, Maildrop and others), and every entry is on both of the big community lists. If you need full coverage, pass a bigger list to blockDomains.
Ask for a work email
Use blockFree to reject free providers such as Gmail, and blockRole to reject shared mailboxes such as admin@.
import { validate, isFreeProvider, isRole } from 'inboxok'
validate('[email protected]', { blockFree: true }).message
// => 'Use your work email address.'
validate('[email protected]', { blockRole: true }).message
// => 'Use a personal email address, not a shared one like admin@.'
isFreeProvider('[email protected]') // => true
isRole('[email protected]') // => trueNormalize emails to find duplicates
normalize() turns different spellings of the same inbox into one value, so one person cannot create many accounts. Store the original address for sending mail, and the normalized one for lookups.
import { normalize } from 'inboxok'
normalize(' [email protected] ')
// => '[email protected]'
normalize('[email protected]')
// => '[email protected]'
normalize('[email protected]', { lowercase: false, plusTags: false })
// => '[email protected]'normalize() does not validate. Call validate() first.
Use custom or translated error messages
Pass messages to replace the built-in English text. You can use the placeholders {email}, {local}, {domain} and {suggestion}.
import { validate } from 'inboxok'
const messages = {
syntax: 'Bitte geben Sie eine gültige E-Mail-Adresse ein.',
disposable: 'Wegwerf-Adressen von {domain} sind nicht erlaubt.',
suggestion: 'Meinten Sie {suggestion}?',
}
validate('[email protected]', { messages }).message
// => 'Wegwerf-Adressen von mailinator.com sind nicht erlaubt.'
validate('[email protected]', { messages }).message
// => 'Meinten Sie [email protected]?'The built-in messages are more specific than one per reason (Email address can't contain spaces., ...two dots in a row.). Setting a reason replaces all of its messages. When an invalid address also has a suggestion, the two messages are joined with a space. If you want your own layout, use reason and suggestion directly.
API
All functions are named exports. The default export is validate, and it also carries every function as a property: inboxok.isEmail, inboxok.suggest and so on.
validate(email, options?)
function validate(email: unknown, options?: ValidateOptions): ValidationResult
// default export: inboxok(email, options?) is the same functionChecks an address and explains the problem, if any. It never throws.
| Option | Type | Default | Description |
|---|---|---|---|
| allowUnicode | boolean | true | Accept non-ASCII letters: josé@exämple.de, 用户@例子.中国. Punycode (xn--) is always accepted. |
| allowQuoted | boolean | false | Accept quoted local parts: "ana silva"@example.com. |
| allowIp | boolean | false | Accept IP addresses: ana@[192.168.0.1], ana@[IPv6:2001:db8::1], [email protected]. |
| requireTld | boolean | true | Require a dot and a valid ending (TLD) in the domain. false accepts ana@localhost. |
| blockDisposable | boolean | true | Reject throwaway inbox providers. |
| blockFree | boolean | false | Reject free providers (Gmail, Outlook, Yahoo, iCloud, GMX, ISPs and others). |
| blockRole | boolean | false | Reject role mailboxes (admin, noreply, support, info, sales and others). |
| blockDomains | readonly string[] | [] | Extra domains to reject with reason 'blocked'. Subdomains match too. |
| allowDomains | readonly string[] | [] | Domains that are never flagged as disposable, blocked or free, and never "corrected". Subdomains match too. This does not restrict other domains. |
| suggest | boolean | true | Suggest corrections for misspelled domains. |
| domains | readonly string[] | [] | Extra domains for suggestions, checked before the built-in list. |
| messages | Partial<Record<MessageKey, string>> | English | Your own messages, per reason plus suggestion. See custom messages. |
Domain lists accept 'example.com', '@example.com' or '*.example.com', in any case.
The result
{
valid: boolean
reason?: Reason // set only when valid is false
email: string // trimmed, domain lowercased, Unicode in NFC form
local: string // the part before the @
domain: string // the lowercased part after the @
message: string // text for your users; '' when valid with no suggestion
suggestion?: string // corrected address; can also be set on invalid results
disposable: boolean // these three flags are set even when the matching
free: boolean // block option is off
role: boolean
}Reasons
| reason | When | Example default message |
|---|---|---|
| 'empty' | '', only spaces, null, undefined | Enter an email address. |
| 'syntax' | missing or extra @, spaces, bad characters, misplaced dots | Email address is missing an @. |
| 'too_long' | over 64 bytes before the @, over 254 in total, or a domain label over 63 | The part before the @ is too long. |
| 'domain' | invalid domain name | Domain names can't start or end with a hyphen. |
| 'tld' | no ending (ana@gmail) or an invalid one (.c, .c0m) | The domain "gmail" needs an ending like .com. |
| 'ip' | an IP address, and allowIp is off | Use a domain name, not an IP address. |
| 'unicode' | non-ASCII characters, and allowUnicode is off | Email address can't contain "é". |
| 'disposable' | a throwaway inbox, and blockDisposable is on (default) | Disposable email addresses are not allowed. |
| 'blocked' | the domain is in blockDomains | Email addresses from example.com are not allowed. |
| 'free' | a free provider, and blockFree is on | Use your work email address. |
| 'role' | a role mailbox, and blockRole is on | Use a personal email address, not a shared one like admin@. |
isEmail(email, options?)
function isEmail(email: unknown, options?: ValidateOptions): booleanReturns validate(email, options).valid, without computing suggestions. With the default options this also rejects disposable domains. Pass { blockDisposable: false } for a syntax-only check.
suggest(email, options?)
function suggest(email: string, options?: SuggestOptions): string | undefinedReturns the corrected address, or undefined when there is nothing to fix.
| Option | Type | Default | Description |
|---|---|---|---|
| domains | readonly string[] | [] | Extra domains to suggest, checked before the built-in list. |
How it decides: the name and the ending are compared separately. Swapped letters (gmial) count as one typo. Long names like hotmail allow one typo, very long names like protonmail allow two, and short names like aol or web only get a misspelled ending fixed (aol.con). This way ordinary domains are not "corrected" into big providers. Known providers (about 280, including regional ones like hotmail.ca and gmx.de) are never corrected.
suggest('[email protected]') // => '[email protected]'
suggest('ana@gmailcom') // => '[email protected]'
suggest('[email protected]') // => '[email protected]'
suggest('[email protected]') // => '[email protected]' (.con, .cmo, .nte, .ogr are not real endings)
suggest('[email protected]') // => undefined (.co is a real ending)normalize(email, options?)
function normalize(email: string, options?: NormalizeOptions): stringTrims the address, lowercases it and removes +tags and Gmail dots. The domain is always lowercased. Quoted local parts are left as they are. Throws a TypeError if email is not a string.
| Option | Type | Default | Description |
|---|---|---|---|
| lowercase | boolean | true | Lowercase the local part too. |
| plusTags | boolean | true | Remove +tags on every domain: [email protected] becomes [email protected]. |
| gmailDots | boolean | true | Remove dots in Gmail addresses, and treat googlemail.com as gmail.com. |
isDisposable(emailOrDomain), isFreeProvider(emailOrDomain), isRole(emailOrLocal)
function isDisposable(emailOrDomain: string): boolean
function isFreeProvider(emailOrDomain: string): boolean
function isRole(emailOrLocal: string): booleanCheck a full address, a bare domain or a bare local part against the built-in lists. isDisposable also matches subdomains. isRole ignores ., - and _, and removes +tags.
isDisposable('[email protected]') // => true
isFreeProvider('[email protected]') // => false
isRole('[email protected]') // => trueBuilt-in lists
const disposableDomains: readonly string[] // throwaway inbox domains
const freeProviders: readonly string[] // free providers and ISPs
const popularDomains: readonly string[] // domains used for typo suggestions, most popular first
const roleAccounts: readonly string[] // role names, without '.', '-' and '_'All lists are frozen arrays.
Types
import type {
ValidationResult, // ValidResult | InvalidResult, narrowed by `valid`
ValidResult,
InvalidResult,
Reason, // 'empty' | 'syntax' | 'too_long' | ... | 'role'
MessageKey, // Reason | 'suggestion'
ValidateOptions,
SuggestOptions,
NormalizeOptions,
Inboxok, // type of the default export
} from 'inboxok'Comparison
Each of these packages does one part of the job. inboxok does all four in one small synchronous call.
| | email-validator | validator.isEmail | mailcheck | disposable-email-domains | inboxok |
|---|---|---|---|---|---|
| Syntax check | Yes | Yes | No | No | Yes |
| Says why it failed | No (boolean) | No (boolean) | - | - | Yes (reason + message) |
| josé@exämple.de | No (rejected) | Yes | - | - | Yes |
| Rejects [email protected] | No (accepted) | Yes | - | - | Yes |
| Rejects a trailing zero-width space | Yes | No (accepted) | - | - | Yes |
| Disposable domains | No | No | No | Yes (121k domains, 2.4 MB JSON) | Yes (~430 curated, ~8 KB total) |
| Matches x.mailinator.com (subdomain) | - | No (host_blacklist is exact) | - | No (not in the main list) | Yes |
| Typo suggestions | No | No | Yes | No | Yes |
| Suggestion for [email protected] | - | - | [email protected] | - | [email protected] |
| [email protected], [email protected] (real domains) | - | - | Suggests gmail.com, acme.com | - | No suggestion |
| Role / free-provider detection | No | No | No | No | Yes |
| TypeScript types | Yes | @types/validator | @types/mailcheck | - | Yes (built in) |
Checked against email-validator 2.0.4, validator 13.15, mailcheck 1.1.1 and disposable-email-domains 1.0.62.
If you need full disposable coverage, disposable-email-domains has a much bigger list. You can pass it to the blockDomains option.
Notes and limitations
- inboxok checks that an address is well formed and worth sending to, without the network. It cannot tell if the domain accepts mail or if the mailbox exists. That needs DNS/SMTP checks on a server (see validmail).
- Comments (
ana(work)@example.com) and other obsolete RFC 5322 forms are rejected. - Domain endings are checked for shape (letters, at least two, or
xn--), not against the official IANA list. So[email protected]passes. - Unicode domain labels are length-checked in their punycode form. Full IDNA2008/UTS 46 checks (mixed scripts, right-to-left rules) are not done.
- Privacy relays that forward to a real inbox (
duck.com, Apple's Hide My Email, SimpleLogin, Firefox Relay) are not on the disposable list. People use them for accounts they keep.
Requirements
- Node.js 20 or newer. Also runs in browsers, edge runtimes, Bun and Deno (no
node:imports, no DNS). - ESM and CommonJS builds.
- TypeScript types included.
- Zero dependencies.
License
MIT
