DOM Properties

ElementInternals.validity

Build a form with the HTML form generator

Exposes the component’s validation flags. Update them through setValidity rather than modifying the returned object. Read individual flags such as valueMissing or the aggregate valid flag. In a form-associated custom element, call setValidity with a suitable flags object and message to change the constraint state; passing an empty object clears failures. Example result: true.

Formal Syntax & Structure

html syntaxhtml
internals.validity

Return Value

ValidityState

The current ValidityState for a form-associated custom element.

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.validity.customError;  })().catch(error => { result.textContent = "Error: " + error.name; });</script>
HTML Reference