The HTML pattern attribute
The HTML pattern attribute tests a text field's value against a regular expression such as [0-9]{5}. An empty field skips the test, and any other value has to match from first character to last, so a ZIP code of 02139 passes while 2139 and 021390 trip validity.patternMismatch.
Overview
The HTML pattern attribute holds a regular expression that a text field's entire value must match. Only text, search, url, tel, email and password <input> types check it. A number input ignores it, and type=number isn't appropriate for US postal codes anyway.
The browser anchors the expression at both ends, as though it began with ^(?: and ended with )$. So [0-9]{4} rejects 12345 and x1234. The group matters for alternatives, since cat|dog rejects catdog, which the JavaScript regular expression /^cat|dog$/ would accept.
Patterns compile with the JavaScript v flag, which reserves more characters inside square brackets. A literal hyphen, pipe, slash or parenthesis there needs a backslash, so [a-z0-9-]+ is a syntax error. The browser drops an invalid pattern, which leaves the field with no pattern check.
An empty value is never tested against pattern, which leaves blank fields to the required attribute. On an email input with multiple, every comma-separated address has to match the pattern on its own.
Syntax
<label for="zip">ZIP code (5 digits)</label>
<input id="zip" name="zip" pattern="[0-9]{5}"
title="Five digits, such as 02139">
Values
| Value |
|---|
| A JavaScript regular expression (without delimiters). |
Example
<p><label for="code">Order number, such as ABC-1234</label><br>
<input id="code" name="code" pattern="[A-Z]{3}-[0-9]{4}" title="Three capital letters, a dash and four digits" value="abc-1234"></p>
<p id="msg" role="status" style="overflow-wrap:anywhere"></p>
<p>Try ABC-1234, then ABC-12345, xABC-1234 or an empty field.</p>
<script>
const code = document.getElementById('code');
const msg = document.getElementById('msg');
function show() {
if (!code.value) msg.textContent = 'Empty, so the pattern is not checked.';
else if (code.validity.patternMismatch) msg.textContent = code.value + ' does not match [A-Z]{3}-[0-9]{4}.';
else msg.textContent = code.value + ' matches.';
}
code.addEventListener('input', show);
show();
</script>
Best practices
- Test each
patternwith the v flag before you ship it. In the console,new RegExp('[a-z0-9-]+', 'v')throws a SyntaxError, while the page shows no sign that the browser dropped that pattern. - Reuse the same expression on the server inside
^(?:and)$. Without those anchors, a JavaScript RegExp built from the same text also accepts values that only contain a match, such as x1234. - Phrase the title as a description, such as "Five digits", not as an error. Browsers may show it as a tooltip before anything has gone wrong.
- Narrow
type="url"with pattern instead of replacing the type, as inpattern="https://.+"for https addresses only. The type's own format check still runs, and pattern adds its rule on top.
Accessibility
Describe a pattern's expected format in visible text near the field. Add a title that describes the pattern as well, but don't rely on assistive technology reading it, even though Chrome exposes the title as the field's accessible description.
Frequently asked questions
Why is the pattern attribute being ignored?
Is ^ or $ needed at the ends of a pattern?
How can a textarea be checked without pattern?
How do I make a pattern case-insensitive?
(?i:[a-z]+) to make it ignore case. These modifiers need Chrome 125, Firefox 132, Safari 26 or later, so use [A-Za-z] for older browsers.






















