devcrea

Why HTML input type="email" Accepts a@b (TLD Fix)

HTML input type=email allows a@b under WHATWG rules: v-flag-safe TLD pattern, Constraint Validation API, and a 16-case browser and PHP test matrix.

// The short answer

Browsers accept a@b and user@localhost because the WHATWG HTML standard intentionally allows single-label domains as a willful violation of RFC 5322. Pair type="email" with a v-flag-safe pattern attribute such as pattern="[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}" to require a dot and TLD in the browser, while keeping authoritative parsing and confirmation on the server.

Full walkthrough, edge cases and why it works
In this guide · 5 sections
  1. 01Why type="email" Accepts a@b and user@localhost
  2. 02How to Require a TLD Safely with pattern
  3. 03Do You Need a JavaScript Email Regex?
  4. 04Test Matrix: 16 Email Addresses Across Browsers and a Server Parser
  5. 05Common Failure Modes and How to Verify Your Form

Browsers accept a@b and user@localhost inside <input type="email"> on purpose, not because of a rendering bug. Because the WHATWG HTML Living Standard intentionally permits single-label domains, requiring a dot and a top-level domain (TLD) means pairing type="email" with a v-flag-safe pattern attribute while still keeping authoritative validation on the server.

  • Root cause: WHATWG's valid email definition allows single-label hostnames (username@domain) with zero dots.
  • Quick fix: Add pattern="[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}" to your <input type="email">.
  • Compatibility limit: Modern Chromium, Firefox, and Safari compile pattern with the JavaScript v (unicodeSets) flag, so any unescaped syntax error makes the browser silently ignore your pattern.
  • Verification: Run input.checkValidity() and inspect input.validity.patternMismatch in DevTools against dotless, plus-tagged, uppercase, and long-TLD addresses.

Why type="email" Accepts a@b and user@localhost

The WHATWG Willful Violation of RFC 5322

Email syntax on the open internet follows RFC 5322, which is notoriously permissive and allows comments, quoted strings, and IP literals that your web app almost certainly neither expects nor supports. Rather than forcing browsers to implement that entire grammar, the WHATWG HTML Living Standard declares a willful violation of RFC 5322 and defines a narrower, practical subset for <input type="email">.

That subset deliberately allows dotless domains like localhost or internal corporate hostnames (admin@intranet). In DNS and mail routing, a single-label domain works fine on private networks. MDN reflects this directly, noting that both username@domain and username@domain.tld pass native browser validation.

What the Browser Actually Validates

Under the hood, browsers check <input type="email"> against the exact regular expression published in the WHATWG HTML specification:

text1 line
^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$

Look closely at the final group: (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*. Because it ends with * (zero or more repetitions) instead of + (one or more), any domain with a single alphanumeric label, including a@b and user@localhost, slips right through.

One extra spec quirk catches developers off guard every time. If you combine required with the multiple attribute on <input type="email">, the browser treats an empty string or whitespace-only value as a valid list of zero email addresses unless you explicitly check the trimmed value.

How to Require a TLD Safely with pattern

A v-Flag-Safe TLD Pattern for type="email"

Since type="email" already enforces basic structural rules, blocking spaces, missing @ symbols, consecutive dots in the domain (user@example..com), and leading or trailing hyphens in domain labels, your pattern attribute does not need to rebuild the entire WHATWG regex. You only need to require at least one dot and a TLD of two or more letters:

html13 lines
<form id="signup-form">
  <label for="email">Work or personal email</label>
  <input
    type="email"
    id="email"
    name="email"
    required
    autocomplete="email"
    pattern="[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}"
    title="Enter an email address with a domain extension, such as name@example.com"
  />
  <button type="submit">Continue</button>
</form>

Browsers implicitly wrap pattern in ^(?: and )$ and compile it with the JavaScript v (unicodeSets) flag. Escaping the hyphen (\-) inside both character classes stops SyntaxError failures under v-mode parsing. For a closer look at how v-flag escaping works across other input types, check out our HTML pattern attribute v-flag escaping guide.

What This Pattern Allows and Rejects

  • Allows: Standard addresses (name@example.com), plus-addressing (user+newsletter@example.com), subdomains (dev@mail.company.co.uk), long modern gTLDs (hello@studio.design, info@archive.museum), and uppercase characters (USER@EXAMPLE.COM).
  • Rejects: Dotless domains (a@b, user@localhost), trailing dots (user@example.com.), single-letter TLDs (user@example.c), and bare domains without a local part (@example.com).

Do You Need a JavaScript Email Regex?

Using the Constraint Validation API Instead of Custom Regexes

Pasting a 300-character RFC 5322 regex into JavaScript creates two separate validation engines, one in HTML and one in JS, that eventually drift out of sync. Once you have type="email" and a TLD pattern on the input, lean on the browser's native Constraint Validation API to handle custom error messages:

js11 lines
const emailInput = document.querySelector('#email');

emailInput.addEventListener('input', () => {
  emailInput.setCustomValidity('');

  if (emailInput.validity.typeMismatch) {
    emailInput.setCustomValidity('Enter a valid email address format.');
  } else if (emailInput.validity.patternMismatch) {
    emailInput.setCustomValidity('Include a domain extension like .com or .design.');
  }
});

You get a single source of truth this way. validity.typeMismatch fires whenever the native WHATWG check fails (like a missing @ or consecutive dots), and validity.patternMismatch triggers when the address passes type="email" but lacks a valid TLD.

When a JS Regex Still Breaks Valid Users

Hand-rolled JavaScript email regexes often hardcode 2-4 character TLDs (\.[a-z]{2,4}$), which quietly blocks legitimate .design, .agency, .digital, or .photography addresses. Other patterns strip out + signs and lock out anyone who relies on Gmail or Fastmail plus-addressing to filter signups.

Client-side validation, whether HTML pattern or JavaScript, serves strictly as a UX layer to catch typos before submission. It cannot prove that a domain has MX records or that a real mailbox sits on the other end. Always run authoritative validation and send a confirmation link on your server (covered in detail in our client-side validation security and server mirroring).

Test Matrix: 16 Email Addresses Across Browsers and a Server Parser

Test Setup and Prerequisites

To see how each layer behaves in practice, run the same 16 addresses across:

  1. Native <input type="email"> in Chromium (Chrome/Edge), Firefox, and Safari.
  2. The WHATWG spec regex executed directly in JavaScript.
  3. The v-safe TLD pattern paired with <input type="email">.
  4. A server-side parser: PHP's filter_var($email, FILTER_VALIDATE_EMAIL), which you can run locally with php -r 'var_dump(filter_var("a@b", FILTER_VALIDATE_EMAIL));'.

Results Table

Test Address Native type="email" (Chrome / Firefox / Safari) WHATWG Regex type="email" + TLD pattern PHP filter_var
a@b Pass Pass Fail Fail
user@localhost Pass Pass Fail Fail
user@intranet Pass Pass Fail Fail
name@example.com Pass Pass Pass Pass
user+tag@example.com Pass Pass Pass Pass
first.last@sub.example.co.uk Pass Pass Pass Pass
hello@studio.design Pass Pass Pass Pass
USER@EXAMPLE.COM Pass Pass Pass Pass
user_name@example.io Pass Pass Pass Pass
user@example.c Pass Pass Fail Fail
user@example.com. Fail Fail Fail Fail
user@example..com Fail Fail Fail Fail
user@-example.com Fail Fail Fail Fail
user@münchen.de (IDN) Pass (Punycode) Fail (raw) Pass (Punycode) Fail (needs Punycode)
user@xn--mnchen-3ya.de Pass Pass Pass Pass
user name@example.com Fail Fail Fail Fail

Look closely at the IDN row (user@münchen.de). Browsers automatically convert Unicode domain names to ASCII Punycode (xn--mnchen-3ya.de) before running type="email" and pattern validation, which explains why the ASCII TLD pattern passes in the browser even for internationalized domains. On the server, PHP's FILTER_VALIDATE_EMAIL rejects raw Unicode domains unless you convert them with idn_to_ascii() first.

Common Failure Modes and How to Verify Your Form

Silent pattern Failure Under the v Flag

If your pattern has a syntax error under the v flag, like an unescaped - in the wrong spot or a disallowed character class sequence, the browser will not block form submission. Instead, it logs a warning in the DevTools console and ignores your pattern attribute completely, letting a@b slip straight through.

Test your pattern directly in the browser console before you ship:

js2 lines
const input = document.querySelector('#email');
new RegExp(`^(?:${input.pattern})$`, 'v');

If that line throws a SyntaxError, your pattern is being silently ignored in production.

IDN, Uppercase, and multiple Edge Cases

  • Uppercase: Both type="email" and the TLD pattern above accept uppercase letters (USER@EXAMPLE.COM). Normalize the domain portion to lowercase on your server before storing or deduplicating accounts.
  • Internationalized local parts: Non-ASCII characters in the local part (before @, such as üser@example.com) fail native type="email" validation in browsers because WHATWG restricts the local part to ASCII. If your users need full SMTPUTF8 local parts, switch to type="text" with HTML input type="number" gotchas with inputmode="email" and handle validation on the server.
  • multiple attribute: If you allow comma-separated emails with multiple, skip pattern on that same field because pattern evaluates the entire comma-separated string as a single value unless you write it specifically for comma-delimited lists.

Verification Checklist

Run this quick snippet in your browser's DevTools console on the live form page to make sure your field behaves the way you expect:

js18 lines
const input = document.querySelector('#email');
const cases = [
  ['a@b', false],
  ['user@localhost', false],
  ['user@example.c', false],
  ['user+tag@example.com', true],
  ['hello@studio.design', true],
  ['USER@EXAMPLE.COM', true],
  ['user@münchen.de', true]
];

console.table(
  cases.map(([value, expected]) => {
    input.value = value;
    const actual = input.checkValidity();
    return { value, expected, actual, ok: actual === expected };
  })
);

Every row should come back as ok: true. Once the table passes in Chromium, Firefox, and Safari, pair it with your server-side email parser and confirmation workflow so malformed addresses never make it into your database.

Quick answers

Frequently asked questions

Why does HTML input type=email accept a@b and user@localhost?

The WHATWG HTML Living Standard intentionally allows single-label domains like localhost or internal corporate hostnames as a willful violation of RFC 5322. Because the browser's built-in email regular expression repeats the domain dot-and-label group zero or more times, any domain without a dot slips right past native validation.

How do you require a dot and TLD on an HTML email input?

Add a pattern attribute alongside type=email using single-quoted or escaped syntax, such as pattern='[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}', with properly escaped hyphens and dots in your markup. Since type=email already enforces structural rules like a single @ symbol and no consecutive domain dots, your pattern only needs to require at least one dot followed by a two-letter or longer TLD.

Why does the browser silently ignore my email pattern attribute?

Modern Chromium, Firefox, and Safari browsers compile the HTML pattern attribute with the JavaScript v (unicodeSets) flag, which enforces stricter character-class escaping than older regex modes. When an unescaped hyphen or invalid character sequence triggers a SyntaxError, the browser logs a console warning and lets dotless addresses submit without blocking.

Does requiring a TLD in input type=email break plus-addressing or modern domains like .design?

Not if you include the plus sign in the local-part character class and use {2,} instead of capping the TLD at two to four characters. That setup accepts addresses like user+tag@example.com and hello@studio.design while still blocking dotless inputs like a@b.

How do internationalized domain names work with input type=email and a TLD pattern?

Browsers automatically convert Unicode domain names like user@münchen.de into ASCII Punycode (user@xn--mnchen-3ya.de) before running type=email and pattern validation. Non-ASCII characters before the @ symbol will still fail native type=email validation, though, because WHATWG restricts the local part to ASCII.

Should you write a separate JavaScript regex for email form validation?

You do not need a duplicate regex in JavaScript once type=email and a v-safe pattern attribute are on the input. Instead, check input.validity.typeMismatch and input.validity.patternMismatch inside an input event listener to show tailored messages with setCustomValidity().

Why does a required email input with the multiple attribute sometimes allow an empty field?

Under the HTML specification, adding the multiple attribute treats a comma-separated list of zero addresses, including an empty or whitespace-only string, as valid. If you combine required and multiple on an email field, check the trimmed value explicitly before accepting the form.

Is HTML pattern validation enough to protect a backend from invalid email addresses?

No: client-side attributes only catch formatting typos in the browser, and anyone can bypass them with direct HTTP requests. Always validate addresses on your server, convert internationalized domains to Punycode if your parser expects ASCII, and send a confirmation link to verify mailbox ownership.

Something wrong or out of date?

Tools change fast. Tell the editor what no longer works.