Node email address validation library
This library is a port of the PHP is_email
function by Dominic Sayers. It's almost entirely based off of hapijs/isemail
...except that it's been compiled to ES5
.
hapijs
has openly chosen not to compile their code to ES5
It is up to the developer to make the code work in their environment. Across the org, we do not include a single line of cleint-side transformation code.
The hapijs organization hosts modules that are designed for server-side use, and we as an organization decided to relatively quickly drop support for older versions of Node.js. We use the latest features to ease development, and browser support is not within scope.
However, there are those that might need an ES5
version of isemail
and don't want to specify transpilation for only this package in the node_modules
folder in a webpack config, for example.
$ npm install isemail-es5
The tests were pulled from is_email
's extensive test suite on October 15, 2013. Many thanks to the contributors! Additional tests have been added to increase code coverage and verify edge-cases.
Run any of the following.
$ lab
$ npm test
$ make test
remember to npm install
to get the development dependencies!
Determines whether the email
is valid or not, for various definitions thereof. Optionally accepts an options
object. Options may include errorLevel
.
Use errorLevel
to specify the type of result for validate()
. Passing a false
literal will result in a true or false boolean indicating whether the email address is sufficiently defined for use in sending an email. Passing a true
literal will result in a more granular numeric status, with zero being a perfectly valid email address. Passing a number will return 0
if the numeric status is below the errorLevel
and the numeric status otherwise.
The tldBlacklist
option can be either an object lookup table or an array of invalid top-level domains. If the email address has a top-level domain that is in the whitelist, the email will be marked as invalid.
The tldWhitelist
option can be either an object lookup table or an array of valid top-level domains. If the email address has a top-level domain that is not in the whitelist, the email will be marked as invalid.
The allowUnicode
option governs whether non-ASCII characters are allowed. Defaults to true
per RFC 6530.
Only one of tldBlacklist
and tldWhitelist
will be consulted for TLD validity.
The minDomainAtoms
option is an optional positive integer that specifies the minimum number of domain atoms that must be included for the email address to be considered valid. Be careful with the option, as some top-level domains, like io
, directly support email addresses.
As of 3.1.1
, the callback
parameter is deprecated, and will be removed in 4.0.0
.
$ node
> var Isemail = require('isemail-es5');
undefined
> Isemail.validate('[email protected]');
true
> Isemail.validate('[email protected]', {errorLevel: true});
0
> Isemail.validate('[email protected]', {errorLevel: true});
6
> Isemail.validate('[email protected]', {errorLevel: 7});
0
> Isemail.validate('[email protected]', {errorLevel: 6});
6