Email

Email address validation.

  • Parity matrix

Validate

Validates an email address: a practical subset of the WHATWG HTML "valid e-mail address".

  • Local part: letters, digits and _'+-., with no leading or trailing dot, no trailing apostrophe and no two dots in a row, at most 64 characters. The whole address is capped at 254 characters (RFC 5321, section 4.5.3.1). Until 2.4.0 there was no cap on either.
  • Domain: at least one dot, labels of up to 63 characters that neither start nor end with a hyphen, the final label 2 to 63 letters or a punycode label (xn--, up to 63 characters, so user@example.xn--p1ai is valid). A punycode label with nothing after xn--, ending in a hyphen or over 63 characters is rejected. Until 2.4.0 a punycode final label was rejected.
  • Quoted local parts, address literals, single-label domains such as user@localhost and the local part characters ! # $ % & * = ? ^ { | } ~, the slash and the backtick are rejected, although WHATWG allows some of them.
  • The check ignores case. A value that is not a string is invalid.
  • A Pix email key does not use this function. pixKey.isValid follows the pattern of the DICT API, which is wider in some places: it accepts fulano@example, a@localhost and a{b}@example.com, which this function rejects.
ParameterTypeRequired
valuestringyes
returnsboolean

Check if an email address is valid. A practical subset of the WHATWG HTML definition.

  • Local part: letters, digits and _'+-., with no leading or trailing dot, no trailing apostrophe and no two dots in a row, and at most 64 characters. The whole address is capped at 254 characters (RFC 5321, section 4.5.3.1); up to 2.4.0 there was no cap on either.
  • Domain: at least one dot, labels of up to 63 characters, final label 2 to 63 letters or a punycode label (xn--, up to 63 characters, so user@example.xn--p1ai is valid).
  • Rejected, although WHATWG allows some of them: quoted local parts, address literals, single label domains such as user@localhost, and the local part characters ! # $ % & * = ? ^ { | } ~, the slash and the backtick.
import { isValidEmail } from '@brazilian-utils/brazilian-utils';

isValidEmail('john.doe@hotmail.com'); // true
isValidEmail('invalid.email'); // false
isValidEmail('user@example.xn--p1ai'); // true (punycode top-level domain)
isValidEmail('a%b@example.com'); // false (% is outside the accepted local part set)

Source: WHATWG HTML, valid e-mail address and RFC 5322.

Code: brazilian-utils/javascript
Try it with JavaScript isValidEmail
The inputs start with the first shared case. Change one to see the new result.

Runs @brazilian-utils/brazilian-utils 2.5.0 in your browser.

Shared test cases (51) and the result in each library email.isValid

Official sources

See also Pix key

Last updated on

On this page