GO-Ve01
ServicesDevelopersBooksDictionaryPapersWikidataEncyclopediaSearchBabelCareersDeck

ValidityState: patternMismatch property

HTML DOM

HTML DOM

The read-only **`patternMismatch`** property of the [`ValidityState`](/docs/Web/API/ValidityState) interface indicates if the value of an input, after having been edited by the user, does not conform to the constraints set by the element's [`pattern`](/docs/Web/HTML/Reference/Attributes/pattern) attribute.

The `patternMismatch` property will be true if and only if the following conditions are all true:

- the field supports the [`pattern`](/docs/Web/HTML/Reference/Attributes/pattern) attribute — which means the input is of `type` text, tel, email, url, password, or search
- the [`pattern`](/docs/Web/HTML/Reference/Attributes/pattern) attribute contains a valid regular expression
- the input value doesn't conform to the constraints set by the [`pattern`](/docs/Web/HTML/Reference/Attributes/pattern) value.

## Value

A boolean that is `true` if the `ValidityState` object does not conform to the constraints.

## Examples

Given the following:

```html
<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 the [`pattern`](/docs/Web/HTML/Reference/Attributes/pattern) attribute set on each.

If the values are too long or too short, or contain characters that aren't digits, `patternMismatch` will be true. When `true`, the element matches the :invalid CSS pseudo-classes.

```css
input:invalid {
border: red solid 3px;
}
```

300

Note, in this case, we get a `patternMismatch` not a validityState.tooLong or validityState.tooShort if the values are too long or too short because it is the pattern that is dictating the length of the value. Had we used [`minlength`](/docs/Web/HTML/Reference/Attributes/minlength) and [`maxlength`](/docs/Web/HTML/Reference/Attributes/maxlength) attributes instead, we may have seen validityState.tooLong or validityState.tooShort being true.

> [!NOTE]
> The `email` input type requires, at minimum, a match of `x@y` and the `url` type requires, at minimum, a match to x:, with no pattern attribute present. When invalid, the validityState.typeMismatch will be true, if there is no pattern attribute (or if the pattern attribute is not valid for that input type).

## Specifications

## Browser compatibility

## See also

- [Constraint validation](/docs/Web/HTML/Guides/Constraint_validation)
- [Forms: Data form validation](/docs/Learn_web_development/Extensions/Forms/Form_validation)
- [Regular Expressions](/docs/Web/JavaScript/Guide/Regular_expressions)

source: MDN Web Docs mirror · page

Cite this

"ValidityState: patternMismatch property". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/API/ValidityState/patternMismatch

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·