References

Beginner-friendly references for web development, with live, editable examples.

The HTML pattern attribute

Attribute All modern browsers
In short

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

Live 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 pattern with 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 in pattern="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?
Browsers skip pattern on empty fields, on number inputs and when the expression is invalid. Chrome and Firefox log an invalid pattern to the console, such as one with an unescaped hyphen inside square brackets.
Is ^ or $ needed at the ends of a pattern?
No, the pattern attribute already has to match the entire value, as if ^ and $ were there. Adding them yourself does no harm.
How can a textarea be checked without pattern?
A textarea has no pattern attribute, so browsers ignore one written on it. Check its value from a script and report problems with setCustomValidity().
How do I make a pattern case-insensitive?
Write the pattern as (?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.
Which characters need a backslash in a pattern?
Inside a pattern's square brackets, the v flag makes ( ) [ ] { } / - | and doubled marks like && need a backslash to match literally. Outside brackets, escape ^ $ . * + ? ( ) [ ] { } | the same way but leave a hyphen bare, and escape a literal backslash everywhere.
What pattern accepts only digits?
Use pattern=[0-9]+ for any number of digits, or [0-9]{5} for exactly five, on a type=text input. Add inputmode=numeric to ask for a virtual keyboard capable of numeric input.
Is there a pattern tag in HTML?
No, pattern is an attribute of the input element, so HTML has no pattern tag. A pattern element does exist in SVG, where it defines a tiled graphic for fills and strokes.