This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

patternMismatch

Summary

Returns whether the input field value does not match the rules defined by the pattern attribute.

Property of dom/ValidityStatedom/ValidityState

Syntax

Note: This property is read-only.

var result = element.validity.patternMismatch;

Return Value

Returns an object of type BooleanBoolean

Whether a value does not match the rules defined by the pattern attribute.

Examples

The following example validates a UK Postal code field on the blur event handler. If an invalid UK postal code is entered, when the form is submitted the custom validity message is displayed.

<script type="text/javascript">
function validatePostCode(evt){
var el=evt.target;
if(el.validity){
// HTML5 aware browsers
if(el.validity.patternMismatch){
el.setCustomValidity('Not a valid UK Postal Code.eg:G1 1AA ');
}else{
el.setCustomValidity("");
}
}else{
// fallback for legacy browsers.


}
}
document.getElementById('txtPostcode').addEventListener('blur',validatePostCode,false);
</script>

Related specifications

W3C HTML5
Working Draft
WHATWG HTML
Living Standard

See also

Other articles

Regular Expression Library

Attributions