The pattern attribute sets a regular expression that a text-like <input> value should match. If the value is not empty and does not match, constraint validation fails (ValidityState.patternMismatch is true).
Not deprecated and not experimental. Per MDN, the regex must match the entire value (implied full-string match). No / delimiters around the pattern. Stable since about July 2015.
Client-side only.
Users can bypass browser checks. Always validate the same rules on the server. Also put the format in visible text near the field — do not rely on title alone for accessibility.
Syntax
HTML Syntax
syntax.html
<input pattern="regexp">
regexp is a JavaScript regular expression without surrounding slashes. Invalid or empty pattern values are ignored (empty pattern with a non-empty value is treated specially — see Edge Cases).
Values
Attribute Values
The value is a regex string. Common study patterns:
Character class lists allowed characters explicitly.
{3,16} sets min and max length in the same regex.
Spaces and symbols outside the class fail the match.
Prefer type="email" for emails instead of inventing an email regex.
Edge Cases & Pitfalls
Most “pattern never works” bugs are wrong input type, missing required, or wrapping the regex in /.
types
Only certain input types
Works on text, search, tel, url, email, password. On number or date, use the constraints those types already provide — not pattern.
empty
Empty value skips pattern
An empty string does not trigger patternMismatch. Add required when the field must be filled.
slashes
No /regex/ wrappers
Write pattern="\d{4}", not pattern="/\d{4}/". Forward slashes are not part of the HTML attribute value.
a11y
Visible format text
MDN: describe the pattern in visible text near the control. title may appear in tooltips/errors but is not enough alone for accessibility.
server
Not a security boundary
Constraint validation is optional UX. Re-check formats on the server every time.
Compatibility
Browser Support
pattern is an HTML attribute for constraint validation on text-like inputs in the browser. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
pattern
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support pattern with HTML5 constraint validation.
97%Global support (approx.)
Google ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetModern
Full support
Internet Explorer10+
Supported
patternWidely supported
Bottom line: Safe to use for UX validation on supported input types. Always pair with visible hints and server-side checks.
Remember
Key Takeaways
Role: regex must match the whole non-empty value.
Types: text, search, tel, url, email, password.
Add required when empty should also fail.
Explain the format in visible text; validate on the server too.
One line:pattern is a full-string regex for text-like inputs — teach the format on the page, then check again on the server.
Frequently Asked Questions
On supported text-like inputs, pattern is a regular expression. For constraint validation, a non-empty value must match the entire pattern or ValidityState.patternMismatch is true.
text, search, tel, url, email, and password. It does not apply to number, date, checkbox, radio, file, and similar types.
No. Per MDN, the browser matches the entire value — as if the pattern were wrapped with implied start/end anchors. Do not wrap the pattern in forward slashes.
An empty string does not fail pattern by itself. Combine pattern with required if the field must be filled and match the regex.
Prefer type=email for normal email syntax. Use pattern when you need a custom format (for example a company domain) on text, tel, or other supported types.
No. It is Baseline widely available since about July 2015. Client-side pattern checks are a UX aid — always validate again on the server.