HTMLOutputElement: setCustomValidity() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since August 2016.

The setCustomValidity() method of the HTMLOutputElement interface sets the custom validity message for the <output> element. Use the empty string to indicate that the element does not have a custom validity error.

The <output> element is not a candidate for constraint validation. The reportValidity() method will not cause the custom error message to be displayed to the user, but does set the customError property of the element's ValidityState object to true and the valid property to false.

Syntax

js
setCustomValidity(string)

Parameters

string

The string containing the error message. The empty string removes any custom validity errors.

Return value

None (undefined).

Examples

In this example, if the <output>'s value is not a non-zero number, we set a custom error message. If it is a number, we set the custom error to an empty string:

js
const cart = document.getElementById("cart-form");
const total = cart.elements("total");
if (parseFloat(total.value)) {
  errorOutput.setCustomValidity("");
} else {
  errorOutput.setCustomValidity("There is an error");
}

Specifications

Specification
HTML
# dom-cva-setcustomvalidity-dev

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
setCustomValidity
Chrome – Full support
Chrome 9 (Release date: 2011-02-03)
footnote
footnote This method only updates the validation error popup, not the tooltip that appears when hovering the mouse over the element, see bug 41380670.
Edge – Full support
Edge 14 (Release date: 2016-08-02)
footnote Full support
Firefox – Full support
Firefox 4 (Release date: 2011-03-22)
footnote Full support
Opera – Full support
Opera 12.1 (Release date: 2012-11-20)
footnote Full support
Safari – Full support
Safari 5.1 (Release date: 2011-07-20)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote
footnote This method only updates the validation error popup, not the tooltip that appears when hovering the mouse over the element, see bug 41380670.
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote Full support
Opera Android – Full support
Opera Android 12.1 (Release date: 2012-10-09)
footnote Full support
Safari on iOS – Full support
Safari on iOS 5 (Release date: 2011-10-12)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote
footnote This method only updates the validation error popup, not the tooltip that appears when hovering the mouse over the element, see bug 41380670.
WebView Android – Full support
WebView Android 3 (Release date: 2011-02-22)
footnote Full support
WebView on iOS – Full support
WebView on iOS 5 (Release date: 2011-10-12)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

See also