DOM Properties

ElementInternals.validationMessage

Build a form with the HTML form generator

Returns the current validation message, or an empty string when not applicable. Messages set by the component should describe how to correct the input. Example result: Choose a city.

Formal Syntax & Structure

html syntaxhtml
internals.validationMessage

Return Value

string

Read-only form-association information.

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