HTMLInputElement: 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 July 2015.

The HTMLInputElement.setCustomValidity() method sets a custom validity message for the element.

Syntax

js
setCustomValidity(message)

Parameters

message

The message to use for validity errors.

Return value

None (undefined).

Exceptions

None.

Examples

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.

js
function validate(inputID) {
  const input = document.getElementById(inputID);
  const validityState = input.validity;

  if (validityState.valueMissing) {
    input.setCustomValidity("You gotta fill this out, yo!");
  } else if (validityState.rangeUnderflow) {
    input.setCustomValidity("We need a higher number!");
  } else if (validityState.rangeOverflow) {
    input.setCustomValidity("That's too high!");
  } else {
    input.setCustomValidity("");
  }

  input.reportValidity();
}

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.

Specifications

Specification
HTML
# dom-cva-setcustomvalidity

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 4 (Release date: 2010-01-25)
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 12 (Release date: 2015-07-29)
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 (Release date: 2010-06-07)
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 4 (Release date: 2010-06-21)
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 4.4 (Release date: 2013-12-09)
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 on iOS – Full support
WebView on iOS 4 (Release date: 2010-06-21)
footnote Full support

Legend

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

Full support
Full support
See implementation notes.

See also