DOM Methods

ElementInternals.checkValidity()

Build a form with the HTML form generator

Checks constraints and dispatches invalid if the element is invalid. It does not display the native validation UI. Only supported for form-associated custom elements. Example result: false.

Formal Syntax & Structure

html syntaxhtml
internals.checkValidity()

Return Value

boolean

True when valid or not a validation candidate.

Working Example

Example Snippethtml
<form id="demo-form">  <demo-field name="city"></demo-field></form><p>  Result:  <output id="result"></output></p><script>  const result = document.getElementById("result");  (async () => {  class DemoField extends HTMLElement { static formAssociated = true; constructor() { super(); this.internals = this.attachInternals(); } }  customElements.define("demo-field", DemoField);  const field = document.querySelector("demo-field"); const internals = field.internals;  internals.setValidity({customError:true}, "Invalid"); result.textContent = internals.checkValidity();  })().catch(error => { result.textContent = "Error: " + error.name; });</script>
HTML Reference