web api instance method
ElementInternals: setValidity() method
The setValidity() method of the ElementInternals interface sets the validity of the element.
Syntax
setValidity(flags)
setValidity(flags, message)
setValidity(flags, message, anchor)
Parameters
-
flagsOptional-
: A dictionary object containing one or more flags indicating the validity state of the element:
valueMissingtypeMismatchpatternMismatchtooLong- : A boolean value that is
trueif the value exceeds the specifiedmaxlengthforHTMLInputElementorHTMLTextAreaElementobjects, orfalseif its length is less than or equal to the maximum length. Iftrue, the element matches the:invalidand:out-of-rangeCSS pseudo-classes.
- : A boolean value that is
tooShort- : A boolean value that is
trueif the value fails to meet the specifiedminlengthforHTMLInputElementorHTMLTextAreaElementobjects, orfalseif its length is greater than or equal to the minimum length. Iftrue, the element matches the:invalidand:out-of-rangeCSS pseudo-classes.
- : A boolean value that is
rangeUnderflow- : A boolean value that is
trueif the value is less than the minimum specified by theminattribute, orfalseif it is greater than or equal to the minimum. Iftrue, the element matches the:invalidand:out-of-rangeCSS pseudo-classes.
- : A boolean value that is
rangeOverflow- : A boolean value that is
trueif the value is greater than the maximum specified by themaxattribute, orfalseif it is less than or equal to the maximum. Iftrue, the element matches the:invalidand:out-of-rangeand CSS pseudo-classes.
- : A boolean value that is
stepMismatch- : A boolean value that is
trueif the value does not fit the rules determined by thestepattribute (that is, it’s not evenly divisible by the step value), orfalseif it does fit the step rule. Iftrue, the element matches the:invalidand:out-of-rangeCSS pseudo-classes.
- : A boolean value that is
badInput- : A boolean value that is
trueif the user has provided input that the browser is unable to convert.
- : A boolean value that is
customError- : A boolean value indicating whether the element’s custom validity message has been set to a non-empty string by calling the element’s
setCustomValidity()method.
- : A boolean value indicating whether the element’s custom validity message has been set to a non-empty string by calling the element’s
[!NOTE] To set all flags to
false, indicating that this element passes all constraints validation, pass in an empty object{}. In this case, you do not need to also pass amessage.
-
-
messageOptional- : A string containing a message, which will be set if any
flagsaretrue. This parameter is only optional if allflagsarefalse.
- : A string containing a message, which will be set if any
-
anchorOptional- : An
HTMLElementwhich can be used by the user agent to report problems with this form submission.
- : An
Return value
None (undefined).
Exceptions
NotSupportedErrorDOMException- : Thrown if the element does not have its
formAssociatedproperty set totrue.
- : Thrown if the element does not have its
TypeError- : Thrown if one or more
flagsistrue.
- : Thrown if one or more
NotFoundErrorDOMException- : Thrown if
anchoris given, but the anchor is not a shadow-including descendant of the element.
- : Thrown if
Examples
In the following example setValidity is called with an empty flags parameter to indicate that the element meets constraint validation rules.
this.internals_.setValidity({});
In the following example setValidity is called with the flag valueMissing set to true. A message parameter must then also be passed containing a message.
this.internals_.setValidity({ valueMissing: true }, "my message");
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.