ElementInternals: setValidity() method
MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونهکد
{{APIRef("Web Components")}}
The **setValidity()** method of the {{domxref("ElementInternals")}} interface sets the validity of the element.
Syntax
setValidity(flags)
setValidity(flags, message)
setValidity(flags, message, anchor)Parameters
flags{{Optional_Inline}}- : A dictionary object containing one or more flags indicating the validity state of the element:
valueMissing- : A boolean value that is
trueif the element has a [required](/en-US/docs/Web/HTML/Reference/Elements/input#required) attribute, but no value, orfalseotherwise. Iftrue, the element matches the {{cssxref(":invalid")}} CSS pseudo-class. typeMismatch- : A boolean value that is
trueif the value is not in the required syntax (when [type](/en-US/docs/Web/HTML/Reference/Elements/input#type) isemailorurl), orfalseif the syntax is correct. Iftrue, the element matches the {{cssxref(":invalid")}} CSS pseudo-class. patternMismatch- : A boolean value that is
trueif the value does not match the specified [pattern](/en-US/docs/Web/HTML/Reference/Elements/input#pattern), andfalseif it does match. Iftrue, the element matches the {{cssxref(":invalid")}} CSS pseudo-class. tooLong- : A boolean value that is
trueif the value exceeds the specifiedmaxlengthfor {{domxref("HTMLInputElement")}} or {{domxref("HTMLTextAreaElement")}} objects, orfalseif its length is less than or equal to the maximum length. Iftrue, the element matches the {{cssxref(":invalid")}} and {{cssxref(":out-of-range")}} CSS pseudo-classes. tooShort- : A boolean value that is
trueif the value fails to meet the specifiedminlengthfor {{domxref("HTMLInputElement")}} or {{domxref("HTMLTextAreaElement")}} objects, orfalseif its length is greater than or equal to the minimum length. Iftrue, the element matches the {{cssxref(":invalid")}} and {{cssxref(":out-of-range")}} CSS pseudo-classes. rangeUnderflow- : A boolean value that is
trueif the value is less than the minimum specified by the [min](/en-US/docs/Web/HTML/Reference/Elements/input#min) attribute, orfalseif it is greater than or equal to the minimum. Iftrue, the element matches the {{cssxref(":invalid")}} and {{cssxref(":out-of-range")}} CSS pseudo-classes. rangeOverflow- : A boolean value that is
trueif the value is greater than the maximum specified by the [max](/en-US/docs/Web/HTML/Reference/Elements/input#max) attribute, orfalseif it is less than or equal to the maximum. Iftrue, the element matches the {{cssxref(":invalid")}} and {{cssxref(":out-of-range")}} and CSS pseudo-classes. stepMismatch- : A boolean value that is
trueif the value does not fit the rules determined by the [step](/en-US/docs/Web/HTML/Reference/Elements/input#step) attribute (that is, it's not evenly divisible by the step value), orfalseif it does fit the step rule. Iftrue, the element matches the {{cssxref(":invalid")}} and {{cssxref(":out-of-range")}} CSS pseudo-classes. badInput- : A boolean value that is
trueif the user has provided input that the browser is unable to convert. 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 {{domxref('HTMLInputElement.setCustomValidity', 'setCustomValidity()')}} method.
توجه: 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 a message.
message{{Optional_Inline}}- : A string containing a message, which will be set if any
flagsaretrue. This parameter is only optional if allflagsarefalse. anchor{{Optional_Inline}}- : An {{domxref("HTMLElement")}} which can be used by the user agent to report problems with this form submission.
Return value
None ({{jsxref("undefined")}}).
Exceptions
NotSupportedError{{domxref("DOMException")}}- : Thrown if the element does not have its
formAssociatedproperty set totrue. - {{jsxref("TypeError")}}
- : Thrown if one or more
flagsistrue. NotFoundError{{domxref("DOMException")}}- : Thrown if
anchoris given, but the anchor is not a shadow-including descendant of the element.
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
{{Specifications}}
Browser compatibility
{{Compat}}