Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The setCustomValidity() method of the HTMLObjectElement interface sets a custom validity message for the element.
The message to use for validity errors.
None (undefined).
None.
In this example, we pass the ID of an input element and set different error messages depending on whether the value is missing, too low, or too high. Note that the message will not be displayed immediately. Attempting to submit the form will display the message, or you can call the reportValidity() method on the element.
It's vital to set the message to an empty string if there are no errors. As long as the error message is not empty, the form will not pass validation and will not be submitted.
| HTML # dom-cva-setcustomvalidity-dev |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Aug 4, 2024 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.