DOM Methods

ElementInternals.setValidity()

Build a form with the HTML form generator

Updates constraint flags. If any flag is true, supply a nonempty message; an optional anchor must be a shadow-including descendant. Clear the errors with an empty flags object. Form-associated elements only. Example result: true.

Formal Syntax & Structure

html syntaxhtml
internals.setValidity(flags, message?, anchor?)

Parameters & Attributes

NameTypeDescription
flagsValidityStateFlagsTrue flags indicate failures.
messagestringRequired when any failure flag is true.
anchorHTMLElementOptional validation UI anchor.

Return Value

undefined

No return value.

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({valueMissing:true}, "Choose a city"); result.textContent = internals.validity.valueMissing;  })().catch(error => { result.textContent = "Error: " + error.name; });</script>
HTML Reference