Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

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…

Read the release notes

pattern

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 pattern attribute 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).

Values

ValueMeaning
A regular expressionThe browser compiles it as ^(?:pattern)$ with the v flag, so the whole value must match. Do not add your own ^ and $.
Attribute absentNo pattern constraint (the default).
Invalid expressionIgnored: 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 fieldAlways 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.

PatternMatches
[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.

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.

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.

Related

Which to choose

  • pattern — the value has a fixed shape you define (codes, IDs, usernames).
  • type="email" / type="url" — the browser already knows the shape; add pattern only to narrow it.
  • data-mask — a fixed format the user should see as they type (phone, card number).
  • data-accept — only some characters are allowed but the format is free.
↑