--- title: ValidityState slug: Web/API/ValidityState translation_of: Web/API/ValidityState ---
{{APIRef("HTML DOM")}} {{gecko_minversion_header("2.0")}}

ValidityState 介面表示了一個元素目前在其檢核條件下驗證的正確性狀態(validity states)。同時,它們也可以協助解釋元素值檢核失敗的原因,如果元素值為不合法的。

屬性

For each of these Boolean properties, a value of true indicates that the specified reason validation may have failed is true, with the exception of the valid property, which is true if the element's value obeys all constraints.

{{domxref("ValidityState.badInput")}} {{ReadOnlyInline}}
Is a {{jsxref("Boolean")}} indicating the user has provided input that the browser is unable to convert.
{{domxref("ValidityState.customError")}} {{ReadOnlyInline}}
Is a {{jsxref("Boolean")}} indicating the element's custom validity message has been set to a non-empty string by calling the element's setCustomValidity() method.
{{domxref("ValidityState.patternMismatch")}} {{ReadOnlyInline}}
Is a {{jsxref("Boolean")}} indicating the value does not match the specified {{htmlattrxref("pattern", "input")}}.
{{domxref("ValidityState.rangeOverflow")}} {{ReadOnlyInline}}
Is a {{jsxref("Boolean")}} indicating the value is greater than the maximum specified by the {{htmlattrxref("max", "input")}} attribute.
{{domxref("ValidityState.rangeUnderflow")}} {{ReadOnlyInline}}
Is a {{jsxref("Boolean")}} indicating the value is less than the minimum specified by the {{htmlattrxref("min", "input")}} attribute.
{{domxref("ValidityState.stepMismatch")}} {{ReadOnlyInline}}
Is a {{jsxref("Boolean")}} indicating the value does not fit the rules determined by the {{htmlattrxref("step", "input")}} attribute (that is, it's not evenly divisible by the step value).
{{domxref("ValidityState.tooLong")}} {{ReadOnlyInline}}
Is a {{jsxref("Boolean")}} indicating the value exceeds the specified maxlength for {{domxref("HTMLInputElement")}} or {{domxref("HTMLTextAreaElement")}} objects. Note: This will never be true in Gecko, because elements' values are prevented from being longer than maxlength.
{{domxref("ValidityState.tooShort")}} {{ReadOnlyInline}}
Is a {{jsxref("Boolean")}} indicating the value fails to meet the specified minlength for {{domxref("HTMLInputElement")}} or {{domxref("HTMLTextAreaElement")}} objects.
{{domxref("ValidityState.typeMismatch")}} {{ReadOnlyInline}}
Is a {{jsxref("Boolean")}} indicating the value is not in the required syntax (when {{htmlattrxref("type", "input")}} is email or url).
{{domxref("ValidityState.valid")}} {{ReadOnlyInline}}
Is a {{jsxref("Boolean")}} indicating the element meets all constraint validations, and is therefore considered to be valid.
{{domxref("ValidityState.valueMissing")}} {{ReadOnlyInline}}
Is a {{jsxref("Boolean")}} indicating the element has a {{htmlattrxref("required", "input")}} attribute, but no value.

規範

Specification Status Comment
{{ SpecName('HTML WHATWG', 'forms.html#the-constraint-validation-api', 'ValidityState') }} {{Spec2('HTML WHATWG')}} Live Standard
{{ SpecName('HTML5.1', '#the-constraint-validation-api', 'ValidityState') }} {{Spec2('HTML5.1')}} No change from the previous snapshot {{SpecName('HTML5 W3C')}}.
{{ SpecName('HTML5 W3C', 'forms.html#the-constraint-validation-api', 'ValidityState') }} {{Spec2('HTML5 W3C')}} First snapshot of  {{SpecName('HTML WHATWG')}} containing this interface.

瀏覽器相容性

{{ CompatibilityTable() }}

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{CompatVersionUnknown}} {{ CompatVersionUnknown }} 10 {{ CompatVersionUnknown() }} 10.0.3
badInput {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatGeckoDesktop(29)}} {{CompatUnknown}} {{CompatUnknown}} 10.0.3
Feature Android Chrome for Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{CompatVersionUnknown}} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
badIndput {{CompatUnknown}} {{CompatUnknown}} {{CompatGeckoMobile(29)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

參見