Coding

How to Check if an Email Address Is Valid (Format vs Real)

To check if an email is valid, test the format with a well-tested validator (or <input type="email"> in a browser), then send a confirmation link to prove the address works. A format check only tells you the text looks like an address. It cannot tell you that the mailbox exists or that the person owns it.

There are three different questions behind "is this email valid", and each needs a different check:

QuestionHow to checkWhat it proves
Does it look like an address?Syntax check (browser, library, regex)No typos like a missing @
Can its domain receive mail?DNS lookup for MX recordsThe domain is set up for email
Is it real and does the user own it?Send a message with a confirmation linkThe mailbox exists and the user can read it

Check the format in the browser

An input with type="email" validates the format when the form is submitted:

<input type="email" name="email" required>

MDN says browsers use an algorithm equivalent to this regular expression:

/^[\w.!#$%&'*+/=?^`{|}~-]+@[a-z\d](?:[a-z\d-]{0,61}[a-z\d])?(?:\.[a-z\d](?:[a-z\d-]{0,61}[a-z\d])?)*$/i

Three details surprise people:

  • An empty value is valid. Add required if the field must be filled in.
  • The domain does not need a dot, so me@example passes. MDN lists it as a valid value.
  • Leading and trailing whitespace is stripped before the check, so " [email protected] " is accepted.

The HTML standard calls this definition a "willful violation" of RFC 5322. It says the RFC's syntax is too strict before the @, too vague after it, and too lax for practical use. So the browser check is deliberately simpler than the email RFCs, and it is the same rule most front-end code can lean on.

To read the result in JavaScript, call input.checkValidity(), which returns true or false.

Check the format in Python

The email-validator package does the syntax check for you. Install it with pip install email-validator, then:

from email_validator import validate_email, EmailNotValidError

try:
    result = validate_email("[email protected]", check_deliverability=False)
    clean_address = result.normalized
except EmailNotValidError as error:
    print(str(error))

Per its README, the library rejects obsolete address forms, special-use domains such as localhost, and domains without a dot by default. EmailNotValidError is the base exception and subclasses ValueError. It also returns a normalized form, which the README says to store and use for lookups.

With check_deliverability=True (the default), it also asks DNS for the domain's MX record. If there is none, it can fall back to A/AAAA records unless a reject-all SPF record is present. The README suggests turning that off on login forms to avoid repeated DNS queries, and leaving it on at sign-up. It does not contact a mail server, so it cannot tell whether a particular mailbox exists.

Why not write your own regex

OWASP's guidance is to use well-tested libraries instead of a custom regex, and to reject only clearly malformed input. A strict pattern rejects real addresses, such as ones with a + tag ([email protected]). If you do want a quick sanity check, a loose one is safer than a clever one:

/^[^\s@]+@[^\s@]+$/

This only requires one @, no spaces, and text on both sides. It catches typos and lets everything else through to the next stage.

Length limits worth enforcing

RFC 5321, section 4.5.3.1, sets these maximums, all in octets:

  • Local part (before the @): 64.
  • Domain: 255.
  • Path, including punctuation and element separators: 256.

The path includes the surrounding angle brackets, so an address itself fits in 254 characters. Rejecting anything longer than 254 characters is a cheap guard against oversized input.

Check that the domain can receive mail

A DNS lookup shows whether the domain publishes MX records. On Windows, macOS and Linux:

nslookup -type=MX example.org

A domain with no MX record (and no address record to fall back on) cannot receive mail, so a typo like [email protected] is only caught if that misspelled domain has no mail setup. This step catches dead domains. It does not catch a mistyped address at a real provider, and it says nothing about the mailbox.

Confirm the address is real: send a verification link

The only reliable proof is to send a message and have the user act on it. OWASP's cheat sheet recommends:

  • Use cryptographically secure, random tokens.
  • Make tokens single-use and time-limited.
  • Do not activate the account before verification is completed.

OWASP also says not to disclose whether an email exists in your system, and to use consistent responses on login and reset flows. For disposable-address services, it suggests risk-based controls over strict blocking.

To create a secure token, generate it on the server. For example, in Python, secrets.token_urlsafe(32) returns a random URL-safe string. Store a hash or the token with an expiry time, and delete it once used.

Validate on the server too

MDN is explicit that users can edit the HTML or bypass it, so a site must not rely on browser validation for security. Run the same format check on the server for every address it receives, especially before storing it or using it in a query. Treat the address as untrusted text: use parameterized queries and escape it when it is shown on a page.

If a valid address is rejected

  • Plus tags or unusual characters: your regex is too strict. Switch to a library or the loose pattern above.
  • New or long top-level domains: patterns that cap the domain ending at 2 to 4 letters reject valid domains. Don't cap it.
  • International characters: the browser pattern above only accepts ASCII. Internationalised addresses need a library that supports them, so check its documentation.
  • Trailing space from copy and paste: trim the value before validating.

Frequently asked questions

Can I check if an email address exists without sending an email?
Not reliably. A format check only tests the text, and a DNS lookup only tests that the domain is set up to receive mail. Neither confirms that a specific mailbox exists, and some mail servers accept any recipient and bounce later. Sending a message with a confirmation link is the only dependable proof.
Is a regular expression enough to validate an email address?
It is enough to catch obvious typos, such as a missing @ or a space. OWASP advises using a well-tested library instead of a custom regex, and rejecting only clearly malformed input, because strict patterns reject addresses that are valid.
Does a@b count as a valid email address?
In a browser's type=email field, yes: the domain part does not need a dot, and MDN lists me@example as valid. Many libraries reject it by default. Decide which you want, and note that most public addresses have a dot in the domain.
What is the maximum length of an email address?
RFC 5321 sets 64 octets for the local part (before the @), 255 octets for the domain, and 256 octets for the whole path including the angle brackets. Subtracting the two brackets leaves 254 characters for the address itself.

Sources