CKTIS / HTML
pattern — input validation pattern
The `pattern` attribute defines a regular expression that must match the entire nonempty input value. For an `email` input with `multiple`, each address must match separately. Use `required` to make the field mandatory.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
<input type="text" pattern="[A-Z]{2}[0-9]{3}">pattern applies to input types text, search, tel, url, email, and password, not to number or textarea. It checks the value's format rather than filtering typed characters.
The attribute value is a JavaScript regular expression compiled with the v flag, without /.../ delimiters. Matching covers the entire value, as with ^(?:pattern)$; explicit anchors are unnecessary. An invalid expression establishes no pattern constraint.
A nonempty value that fails to match causes validity.patternMismatch. An empty value does not violate pattern, although required can cause validity.valueMissing. An empty pattern, pattern="", rejects every nonempty value. For email with multiple, the pattern is checked separately against each address; the underlying email or url type constraints still apply.
Describe the required format to users; title, if present, must describe the pattern. In the example, [A-Z]{2}[0-9]{3} means two uppercase ASCII letters followed by three ASCII digits.
Examples
const input = document.createElement('input');
input.type = 'text';
input.pattern = '[A-Z]{2}[0-9]{3}';
input.required = true;
for (const value of ['AB123', 'ab123', 'XAB123', '']) {
input.value = value;
console.log(JSON.stringify({
value,
patternMismatch: input.validity.patternMismatch,
valueMissing: input.validity.valueMissing,
valid: input.checkValidity()
}));
}Result
{"value":"AB123","patternMismatch":false,"valueMissing":false,"valid":true}
{"value":"ab123","patternMismatch":true,"valueMissing":false,"valid":false}
{"value":"XAB123","patternMismatch":true,"valueMissing":false,"valid":false}
{"value":"","patternMismatch":false,"valueMissing":true,"valid":false}The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.