--- title: HTMLFormElement.reportValidity() slug: Web/API/HTMLFormElement/reportValidity translation_of: Web/API/HTMLFormElement/reportValidity ---
{{APIRef("HTML DOM")}}

方法 HTMLFormElement.reportValidity() 返回布尔值,如果form表单的子表单控件满足验证限制条件,则该方法返回 true ,否则返回false。当返回 false 时, 每个不合法的子控件的 invalid 事件将会被触发,并且验证中存在的问题会报告该用户。 

Syntax

HTMLFormElement.reportValidity()

Return value

{{domxref("布尔")}}

Example

document.forms['myform'].addEventListener('invalid', function() {
  // Optional response here
}, false);

document.forms['myform'].addEventListener('submit', function() {
  document.forms['myform'].reportValidity();
}, false);

Specifications

Specification Status Comment
{{SpecName("HTML WHATWG", "forms.html#dom-cva-reportvalidity", "HTMLFormElement.reportValidity()")}} {{Spec2("HTML WHATWG")}}
{{SpecName("HTML5.1", "semantics.html#the-constraint-validation-api", "HTMLFormElement.reportValidity()")}} {{Spec2("HTML5.1")}} Initial definition

Browser compatibility

{{Compat("api.HTMLFormElement.reportValidity")}}