HTML attribute: pattern
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Thepattern
attribute specifies aregular expression the form control's value should match. If a non-null
value doesn't conform to the constraints set by thepattern
value, theValidityState
object's read-onlypatternMismatch
property will be true.
Try it
<label for="username">Username: (3-16 characters)</label><input name="username" type="text" value="Sasha" pattern="\w{3,16}" required /><label for="pin">PIN: (4 digits)</label><input name="pin" type="password" pattern="\d{4,4}" required />
label { display: block; margin-top: 1em;}input:valid { background-color: palegreen;}input:invalid { background-color: lightpink;}
Overview
Thepattern
attribute is an attribute of thetext,tel,email,url,password, andsearch input types.
Thepattern
attribute, when specified, is a regular expression which the input'svalue
must match for the value to passconstraint validation. It must be a valid JavaScript regular expression, as used by theRegExp
type, and as documented in ourguide on regular expressions.
The pattern's regular expression is compiled with the'v'
flag. This makes the regular expressionunicode-aware, and also changes how character classes are interpreted. This allows character class set intersection and subtraction operations, and in addition to]
and\
, the following characters must be escaped using a\
backslash if they represent literal characters:(
,)
,[
,{
,}
,/
,-
,|
. Before mid-2023, the'u'
flag was specified instead; If you're updating older code, read theunicodeSets
reference.
The pattern's regular expression must match the entire input'svalue
, rather than matching a substring - as if a^(?:
were implied at the start of the pattern and)$
at the end.
No forward slashes should be specified around the pattern text. No regular expression is applied if the attribute value is absent, an empty string, or invalid.
Some of the input types supporting the pattern attribute, notably theemail andurl input types, have expected value syntaxes that must be matched. If the pattern attribute isn't present, and the value doesn't match the expected syntax for that value type, theValidityState
object's read-onlytypeMismatch
property will be true.
Constraint validation
If the input's value is not the empty string and the value does not match the entire regular expression, there is a constraint violation reported by theValidityState
object'spatternMismatch
property beingtrue
.
Note:If thepattern
attribute is specified with no value, its value is implicitly the empty string. Thus,any non-empty inputvalue
will result in constraint violation.
Usability and accessibility considerations
When including apattern
, provide a description of the pattern in visible text near the control. Additionally, include atitle
attribute which gives a description of the pattern. User agents may use the title contents during constraint validation to tell the user that the pattern is not matched. Some browsers show a tooltip with title contents, improving usability for sighted users. Additionally, assistive technology may read the title aloud when the control gains focus, but this should not be relied upon for accessibility.
Only relying on thetitle
attribute for the visual display of text content is discouraged, as many user agents do not expose the attribute in an accessible manner. Although some browsers show a tooltip when an element with a title is hovered, that leaves out keyboard-only and touch-only users. This is one of the several reasons you must include information informing users how to fill out the control to match the requirements.
Whiletitle
s are used by some browsers to populate error messaging, because browsers sometimes also show the title as text on hover, it therefore shows in non-error situations, so be careful not to word titles as if an error has occurred.
Examples
Matching a phone number
Given the following:
<p> <label> Enter your phone number in the format (123) - 456 - 7890 (<input name="tel1" type="tel" pattern="[0-9]{3}" placeholder="###" aria-label="3-digit area code" size="2" />) - <input name="tel2" type="tel" pattern="[0-9]{3}" placeholder="###" aria-label="3-digit prefix" size="2" /> - <input name="tel3" type="tel" pattern="[0-9]{4}" placeholder="####" aria-label="4-digit number" size="3" /> </label></p>
Here we have 3 sections for a north American phone number with an implicit label encompassing all three components of the phone number, expecting 3-digits, 3-digits and 4-digits respectively, as defined by thepattern
attribute set on each.
If the values are too long or too short, or contain characters that aren't digits, thepatternMismatch
will be true. Whentrue
, the element matches the:invalid
CSS pseudo-classes.
input:invalid { border: red solid 3px;}
If we had usedminlength
andmaxlength
attributes instead, we may have seenvalidityState.tooLong
orvalidityState.tooShort
being true.
Specifying a pattern
You can use thepattern
attribute to specify a regular expression that the inputted value must match in order to be considered valid (seeValidating against a regular expression for a crash course on using regular expressions to validate inputs).
The example below restricts the value to 4-8 characters and requires that it contain only lower-case letters.
<form> <div> <label for="uname">Choose a username: </label> <input type="text" name="name" required size="45" pattern="[a-z]{4,8}" title="4 to 8 lowercase letters" /> <span></span> <p>Usernames must be lowercase and 4-8 characters in length.</p> </div> <div> <button>Submit</button> </div></form>
div { margin-bottom: 10px; position: relative;}p { font-size: 80%; color: #999;}input + span { padding-right: 30px;}input:invalid + span::after { position: absolute; content: "✖"; padding-left: 5px;}input:valid + span::after { position: absolute; content: "✓"; padding-left: 5px;}
This renders like so:
Specifications
Specification |
---|
HTML # attr-input-pattern |