DOM Methods
ElementInternals.setValidity()
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
| Name | Type | Description |
|---|---|---|
| flags | ValidityStateFlags | True flags indicate failures. |
| message | string | Required when any failure flag is true. |
| anchor | HTMLElement | Optional 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>