0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…
Validates a text input's value against a regular expression. The whole value must match, the pattern is compiled with the v flag, and an empty optional field is always valid.
Overview
The patternattribute gives a text input a regular expression that its value must match. When the value does not match, the control's validity.patternMismatch is true, it matches :invalid, and the form will not submit. It needs no JavaScript.
Applies to:<input> of type text, search, tel, url, email and password. It does not apply to <textarea>, <select> or other input types (number, date, range and so on).
pattern with a visible hint and a custom message
1234567891011
<formdata-validate><form-field><labelfor="username">Username</label><inputtype="text"id="username"name="username"requiredpattern="[a-zA-Z][a-zA-Z0-9_\-]{2,15}"data-message-patternmismatch="Start with a letter, then 2 to 15 letters, digits, hyphens or underscores." /><outputclass="hint"for="username">3 to 16 characters. Starts with a letter.</output><outputclass="error"for="username"aria-live="polite">Check the username format.</output></form-field><buttontype="submit">Create account</button></form>
Values
Value
Meaning
A regular expression
The browser compiles it as ^(?:pattern)$ with the v flag, so the whole value must match. Do not add your own ^ and $.
Ignored: the field is never patternMismatch, so validation silently does nothing. Chromium logs a console error when it checks the field; nothing appears on the page.
Empty field
Always passes pattern. Add required if the field must be filled.
With multiple on type="email", each comma-separated address must match the pattern on its own.
Examples
Common patterns
Each of these compiles with the v flag. Describe the format in visible hint text as well; the pattern alone tells the user nothing.
Pattern
Matches
[0-9]{5}
US ZIP code, 5 digits
[0-9]{5}(-[0-9]{4})?
ZIP or ZIP+4 (a hyphen outside a class needs no escape)
[a-zA-Z][a-zA-Z0-9_\-]{2,15}
Username: a letter, then 2 to 15 letters, digits, _ or -
[A-Z]{3}-[0-9]{4}
Product code such as ABC-1234
#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})
Hex colour with optional #
[\p{L}\p{M}' \-]+
A name in any script (Unicode property escapes such as \p{L} work in pattern)
Escaping in a character class
In v-flag mode these characters must be escaped inside [...]: ( ) [ ] { } / - \ |. Doubled punctuation such as &&, !! or -- is reserved too. A literal hyphen at the end of a class, valid in older regex dialects, makes the whole pattern invalid.
Bad: unescaped hyphen in a class (the pattern is ignored)
Check a pattern in the console the way the browser compiles it: new RegExp('^(?:' + input.pattern + ')$', 'v'). If that throws, the attribute does nothing.
pattern on email and url
The type check runs first, then the pattern. Use pattern to narrow an already valid value, for example to one company domain.
Company addresses only
<form-field><labelfor="work-email">Work email</label><inputtype="email"id="work-email"name="email"autocomplete="email"requiredpattern=".+@example\.com"data-message-patternmismatch="Use your @example.com address." /><outputclass="hint"for="work-email">Your @example.com address.</output><outputclass="error"for="work-email"aria-live="polite"></output></form-field>
In Vanilla Breeze
Invalid styling. A text field that is :user-invalid or [aria-invalid="true"] gets the error border with a thicker inline-start edge, and a double border in forced colours (src/native-elements/input/styles.css). :user-invalid waits until the user has edited the field or tried to submit, so nothing is red on load.
<form-field> collapses the hint and reveals output.error when the field is invalid, and adds a valid or invalid icon (src/custom-elements/form-field/styles.css). It validates when focus leaves the field, then again on every edit once an error has shown, and wires aria-describedby and aria-invalid.
data-message-patternmismatch on the input replaces the browser's generic message with yours (src/web-components/form-field/logic.js). It works with or without data-validate on the form.
form[data-validate] turns off the browser bubbles, shows every field's message inline and can add an error summary. See form-field: JS-enhanced validation.
Input filters.data-mask formats as the user types; data-accept blocks characters outside a set. Both stop bad input early, but keep pattern as the constraint the form actually checks.
The OTP field (<form-field> with input[data-type="otp"]) adds pattern="\d{6}" (or the length you set) when you don't.
Accessibility
State the format in visible text next to the field, with <output class="hint" for> inside <form-field>. The hint is wired to aria-describedby, so screen readers hear it when the field gets focus.
Don't rely on title. Browsers add it to the validation bubble, but it is not shown on touch devices, not reliably announced, and not visible until the user fails.
Write the error as a fix ("Use 5 digits") rather than a verdict ("Invalid input"). data-message-patternmismatch is where that text goes.
Don't use a pattern to reject real names, addresses or phone formats from other countries. Keep patterns for codes you issue yourself.
Browser support
pattern works in every browser. The v flag compilation is current behaviour in all engines (checked November 2026 in Chromium 147, Firefox 157 and WebKit 26.4: the unescaped-hyphen pattern above is ignored in all three). Older browsers compiled with the u flag, where the escaped form also works, so escaping is safe everywhere.
Gotchas
An invalid pattern fails silently. The field accepts anything. Test the pattern with the v flag (see above).
pattern does nothing on <textarea> or <select>. Use minlength/maxlength for textarea length, and setCustomValidity() for anything else.
It does not apply to number, date or range inputs either. Use min, max and step.
Matching is case-sensitive and there is no way to pass flags. Spell out both cases: [a-zA-Z].
Patterns are checked on the client only. Validate again on the server.