HTMLButtonElement: checkValidity() method
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
ThecheckValidity() method of theHTMLButtonElement interface returns a boolean value which indicates if the element meets anyconstraint validation rules applied to it. If false, the method also fires aninvalid event on the element. Because there's no default browser behavior forcheckValidity(), canceling thisinvalid event has no effect. It always returns true if the<button> element'stype is"button" or"reset", because such buttons are never candidates forconstraint validation.
Note:An HTML<button> element of the"submit" type with a non-nullvalidationMessage is considered invalid, will match the CSS:invalid pseudo-class, and will causecheckValidity() to return false. Use theHTMLButtonElement.setCustomValidity() method to set theHTMLButtonElement.validationMessage to the empty string to set thevalidity state to be valid.
In this article
Syntax
checkValidity()Parameters
None.
Return value
Returnstrue if the element's value has no validity problems; otherwise, returnsfalse.
Examples
In the following example, callingcheckValidity() returns eithertrue orfalse.
const element = document.getElementById("myButton");console.log(element.checkValidity());Specifications
| Specification |
|---|
| HTML> # dom-cva-checkvalidity-dev> |