ValidityState

ValidityState interface represents the validity states that an element can be in, with respect to constraint validation. Together, they help explain why an element's value fails to validate, if it's not valid.

特性

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.

badInput 只读
A Boolean that is true if the user has provided input that the browser is unable to convert.
customError 只读
A Boolean indicating whether the element's custom validity message has been set to a non-empty string by calling the element's setCustomValidity() 方法。
patternMismatch 只读
A Boolean that is true if the value does not match the specified pattern ,和 false if it does match. If true, the element matches the :invalid CSS pseudo-class.
rangeOverflow 只读
A Boolean that is true if the value is greater than the maximum specified by the max attribute, or false if it is less than or equal to the maximum. If true, the element matches the :invalid and :out-of-range and CSS pseudo-classes.
rangeUnderflow 只读
A Boolean that is true if the value is less than the minimum specified by the min attribute, or false if it is greater than or equal to the minimum. If true, the element matches the :invalid and :out-of-range CSS pseudo-classes.
stepMismatch 只读
A Boolean that is true if the value does not fit the rules determined by the step attribute (that is, it's not evenly divisible by the step value), or false if it does fit the step rule. If true, the element matches the :invalid and :out-of-range CSS pseudo-classes.
tooLong 只读
A Boolean that is true if the value exceeds the specified maxlength for HTMLInputElement or HTMLTextAreaElement objects, or false if its length is less than or equal to the maximum length. 注意: This property is never true in Gecko, because elements' values are prevented from being longer than maxlength . If true, the element matches the the :invalid and :out-of-range CSS pseudo-classes.
tooShort 只读
A Boolean that is true if the value fails to meet the specified minlength for HTMLInputElement or HTMLTextAreaElement objects, or false if its length is greater than or equal to the minimum length. If true, the element matches the :invalid and :out-of-range CSS pseudo-classes.
typeMismatch 只读
A Boolean that is true if the value is not in the required syntax (when type is email or url ),或 false if the syntax is correct. If true, the element matches the :invalid CSS pseudo-class.
valid 只读
A Boolean that is true if the element meets all its validation constraints, and is therefore considered to be valid, or false if it fails any constraint. If true, the element matches the :valid CSS pseudo-class; the :invalid CSS pseudo-class otherwise.
valueMissing 只读
A Boolean that is true if the element has a required attribute, but no value, or false otherwise. If true, the element matches the :invalid CSS pseudo-class.

规范

规范 状态 Comment
HTML Living Standard
The definition of 'ValidityState' in that specification.
实时标准 实时标准
HTML 5.1
The definition of 'ValidityState' in that specification.
Recommendation No change from the previous snapshot HTML5 .
HTML5
The definition of 'ValidityState' in that specification.
Recommendation First snapshot of HTML Living Standard containing this interface.

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
ValidityState Chrome 15 Edge 12 Firefox 4 IE 10 Opera 12.1 Safari 11 WebView Android 4 Chrome Android Yes Firefox Android 64 Opera Android 12.1 Safari iOS 5 Samsung Internet Android Yes
badInput Chrome 25 Edge 14 Firefox 29 IE No Opera 15 Safari 11 WebView Android 4.4 Chrome Android Yes Firefox Android 64 Opera Android 14 Safari iOS 7 Samsung Internet Android Yes
tooLong Chrome 15 Edge 12
12
Not supported in the unlikely case of the value being initially set too long, and then changed by the user to a still incorrect state. Per caniuse.com.
Firefox 4
4
Not supported in the unlikely case of the value being initially set too long, and then changed by the user to a still incorrect state. Per caniuse.com.
IE 10 Opera 15 Safari 11 WebView Android 4 Chrome Android Yes Firefox Android 64 Opera Android 14 Safari iOS 5 Samsung Internet Android Yes
tooShort Chrome 40 Edge 17 Firefox 51 IE No Opera 27 Safari 11 WebView Android 67 Chrome Android Yes Firefox Android 64 Opera Android 27 Safari iOS 10 Samsung Internet Android Yes

图例

完整支持
完整支持
不支持
不支持
见实现注意事项。

另请参阅

  1. ValidityState
  2. 特性
    1. badInput
    2. patternMismatch
    3. rangeOverflow
    4. rangeUnderflow
    5. stepMismatch
    6. tooLong
    7. typeMismatch
  3. Related pages for HTML DOM
    1. BeforeUnloadEvent
    2. DOMStringMap
    3. ErrorEvent
    4. GlobalEventHandlers
    5. HTMLAnchorElement
    6. HTMLAreaElement
    7. HTMLAudioElement
    8. HTMLBRElement
    9. HTMLBaseElement
    10. HTMLBaseFontElement
    11. HTMLBodyElement
    12. HTMLButtonElement
    13. HTMLCanvasElement
    14. HTMLContentElement
    15. HTMLDListElement
    16. HTMLDataElement
    17. HTMLDataListElement
    18. HTMLDialogElement
    19. HTMLDivElement
    20. HTMLDocument
    21. HTMLElement
    22. HTMLEmbedElement
    23. HTMLFieldSetElement
    24. HTMLFormControlsCollection
    25. HTMLFormElement
    26. HTMLFrameSetElement
    27. HTMLHRElement
    28. HTMLHeadElement
    29. HTMLHeadingElement
    30. HTMLHtmlElement
    31. HTMLIFrameElement
    32. HTMLImageElement
    33. HTMLInputElement
    34. HTMLIsIndexElement
    35. HTMLKeygenElement
    36. HTMLLIElement
    37. HTMLLabelElement
    38. HTMLLegendElement
    39. HTMLLinkElement
    40. HTMLMapElement
    41. HTMLMediaElement
    42. HTMLMetaElement
    43. HTMLMeterElement
    44. HTMLModElement
    45. HTMLOListElement
    46. HTMLObjectElement
    47. HTMLOptGroupElement
    48. HTMLOptionElement
    49. HTMLOptionsCollection
    50. HTMLOutputElement
    51. HTMLParagraphElement
    52. HTMLParamElement
    53. HTMLPictureElement
    54. HTMLPreElement
    55. HTMLProgressElement
    56. HTMLQuoteElement
    57. HTMLScriptElement
    58. HTMLSelectElement
    59. HTMLShadowElement
    60. HTMLSourceElement
    61. HTMLSpanElement
    62. HTMLStyleElement
    63. HTMLTableCaptionElement
    64. HTMLTableCellElement
    65. HTMLTableColElement
    66. HTMLTableDataCellElement
    67. HTMLTableElement
    68. HTMLTableHeaderCellElement
    69. HTMLTableRowElement
    70. HTMLTableSectionElement
    71. HTMLTemplateElement
    72. HTMLTextAreaElement
    73. HTMLTimeElement
    74. HTMLTitleElement
    75. HTMLTrackElement
    76. HTMLUListElement
    77. HTMLUnknownElement
    78. HTMLVideoElement
    79. HashChangeEvent
    80. History
    81. ImageData
    82. Location
    83. MessageChannel
    84. MessageEvent
    85. MessagePort
    86. Navigator
    87. NavigatorGeolocation
    88. NavigatorID
    89. NavigatorLanguage
    90. NavigatorOnLine
    91. NavigatorPlugins
    92. PageTransitionEvent
    93. Plugin
    94. PluginArray
    95. PopStateEvent
    96. PortCollection
    97. PromiseRejectionEvent
    98. RadioNodeList
    99. Transferable
    100. Window
    101. WindowBase64
    102. WindowEventHandlers
    103. WindowTimers