{"id":8895,"date":"2019-08-18T23:03:46","date_gmt":"2019-08-18T15:03:46","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=8895"},"modified":"2021-07-30T13:35:48","modified_gmt":"2021-07-30T05:35:48","slug":"js-checkvalidity-setcustomvalidity","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2019\/08\/js-checkvalidity-setcustomvalidity\/","title":{"rendered":"checkValidity\u7b49form\u539f\u751fJS\u9a8c\u8bc1\u65b9\u6cd5\u548c\u5c5e\u6027\u8be6\u7ec6\u4ecb\u7ecd"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=8895\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=8895<\/a><br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/201908-form.png';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201908\/background-thumb.png\" width=\"484\" height=\"300\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u5148\u4eceform\u5185\u7f6e\u9a8c\u8bc1\u7684\u5916\u90e8\u8868\u73b0\u8bf4\u8d77<\/h3>\n<p>\u8bf4\u8d77form\u5185\u7f6e\u9a8c\u8bc1\uff0c\u5f88\u591a\u4eba\u60f3\u5230\u7684\u662f\u8bbe\u7f6e<code>required<\/code>\u8fd9\u6837\u7684HTML\u5c5e\u6027\uff0c\u7136\u540e\u8868\u5355\u63d0\u4ea4\u7684\u65f6\u5019\u51fa\u73b0\u8fd9\u6837\u7684\u63d0\u793a\u6548\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201908\/2019-08-18_193750.png\" width=\"250\" height=\"172\" alt=\"form\u5185\u7f6e\u63d0\u793a\u6548\u679c\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u8fd9\u53ea\u662f\u8868\u8c61\uff0c\u53ea\u662fform\u8868\u5355\u539f\u751f\u9a8c\u8bc1\u7684\u4e00\u5c0f\u90e8\u5206\u3002form\u5185\u7f6e\u9a8c\u8bc1\u7684\u7cbe\u9ad3\u5176\u5b9e\u4e0d\u6b62\u8fd9\u70b9\u8868\u9762\u7684\u4e1c\u897f\u3002<\/p>\n<p>\u4f8b\u5982CSS\u4fa7\u6709\u8bf8\u591a\u5339\u914d\u7684\u4f2a\u7c7b\uff0c\u5982<code>:required<\/code>\u4f2a\u7c7b, <code>:optional<\/code>\u4f2a\u7c7b\u6216\u662f<code>:valid<\/code>\uff0c<code>:invalid<\/code>\u4f2a\u7c7b\u7b49\u3002<\/p>\n<p>\u53c8\u4f8b\u5982JS DOM\u4e8b\u4ef6\u8fd9\u5757\u8868\u5355\u5143\u7d20\u53ef\u4ee5\u76d1\u542c<code>'invalid'<\/code>\u548c<code>'valid'<\/code>\u4e8b\u4ef6\u3002<\/p>\n<p>\u53c8\u4f8b\u5982JS DOM API\u8fd9\u4e00\u5757\u5219\u6709\u8bf8\u591a\u5185\u7f6e\u7684\u9a8c\u8bc1\u65b9\u6cd5\u548c\u5c5e\u6027\uff0c\u8fd9\u5c31\u662f\u672c\u6587\u8981\u4ecb\u7ecd\u7684\u77e5\u8bc6\u70b9\u3002<\/p>\n<h3>\u4e8c\u3001form\u8868\u5355\u5143\u7d20\u5185\u7f6e\u9a8c\u8bc1\u65b9\u6cd5\u548c\u5c5e\u6027<\/h3>\n<p>\u5f00\u95e8\u89c1\u5c71\u4e0d\u6253\u5f2f\uff0c\u51713\u4e2a\u65b9\u6cd5\u548c1\u4e2a\u5c5e\u6027\u3002<\/p>\n<p>3\u4e2a\u65b9\u6cd5\u5206\u522b\u662f<code>checkValidity()<\/code>, <code>reportValidity()<\/code>, <code>setCustomValidity()<\/code>\u65b9\u6cd5\uff0c1\u4e2a\u5c5e\u6027\u662f<code>validity<\/code>\u5c5e\u6027\u3002<\/p>\n<p>\u5177\u4f53\u5982\u4e0b\uff1a<\/p>\n<h4>checkValidity()\u65b9\u6cd5<\/h4>\n<p><code>checkValidity()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u9a8c\u8bc1\u5f53\u524d\u8868\u5355\u63a7\u4ef6\u5143\u7d20\uff0c\u6216\u8005\u6574\u4e2a\u8868\u5355\u662f\u5426\u9a8c\u8bc1\u901a\u8fc7\uff0c\u8fd4\u56de\u503c\u662f\u5e03\u5c14\u503c\uff0c<code>true<\/code>\u6216\u8005<code>false<\/code>\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u67d0\u4e0b\u62c9\u6846\u5143\u7d20\u662f\u5426\u9a8c\u8bc1\u901a\u8fc7\uff1a<\/p>\n<pre>var isSelectPassing = document.querySelector('select').checkValidity();    <span style=\"color:green;\">\/\/ true\u6216false<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201908\/2019-08-18_195012.png\" width=\"371\" height=\"44\" alt=\"\u4e0b\u62c9\u6846\u5143\u7d20\u7684\u9a8c\u8bc1\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6216\u8005\u6574\u4e2a\u8868\u5355\u5143\u7d20\u662f\u5426\u9a8c\u8bc1\u901a\u8fc7\uff1a<\/p>\n<pre>var isFormPassing = document.forms[0].checkValidity();    <span style=\"color:green;\">\/\/ true\u6216false<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201908\/2019-08-18_195215.png\" width=\"286\" height=\"45\" alt=\"form\u5143\u7d20\u8c03\u7528\u9a8c\u8bc1\u65b9\u6cd5\" class=\"alignnone size-medium\" \/><\/p>\n<h4>reportValidity()\u65b9\u6cd5<\/h4>\n<p><code>reportValidity()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u89e6\u53d1\u6d4f\u89c8\u5668\u7684\u5185\u7f6e\u7684\u9a8c\u8bc1\u63d0\u793a\u4ea4\u4e92\uff0c\u8fd4\u56de\u5e03\u5c14\u503c\uff0c<code>true<\/code>\u6216\u8005<code>false<\/code>\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>document.querySelector('select').reportValidity()<\/pre>\n<p>\u4f1a\u89e6\u53d1\u4e0b\u62c9\u6846\u7684\u9519\u8bef\u63d0\u793a\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201908\/2019-08-18_201410.png\" width=\"387\" height=\"175\" alt=\"\u4e0b\u62c9\u6846\u9a8c\u8bc1\u63d0\u793a\u89e6\u53d1\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u53e6\u5916\uff0c<code>reportValidity()<\/code>\u65b9\u6cd5IE\u6d4f\u89c8\u5668\u5e76\u4e0d\u652f\u6301\uff0cEdge 17+\u5f00\u59cb\u652f\u6301\u3002<\/p>\n<h4>setCustomValidity()\u65b9\u6cd5<\/h4>\n<p>\u987e\u540d\u601d\u4e49\u5c31\u662f\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u7684\u9a8c\u8bc1\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u81ea\u5b9a\u4e49\u63d0\u793a\u6587\u5b57\u3002<\/p>\n<p>\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>ele.setCustomValidity(str);<\/pre>\n<p>\u5176\u4e2d\uff0c<code>str<\/code>\u4e3a\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u51fa\u9519\u63d0\u793a\u7684\u6587\u5b57\u4fe1\u606f\uff0c\u5982\u679c\u662f\u7a7a\u5b57\u7b26\u4e32\uff0c\u5219\u8868\u793a\u4e0d\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u63d0\u793a\u3002<\/p>\n<p>\u4f8b\u5982\u4e0a\u9762\u7684<code>reportValidity()<\/code>\u65b9\u6cd5\u7684\u4e0b\u62c9\u51fa\u9519\u63d0\u793a\u662f\u201c\u8bf7\u5728\u5217\u8868\u4e2d\u9009\u62e9\u4e00\u9879\u201d\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u5e0c\u671b\u6539\u6210\u201c\u8bf7\u9009\u62e9\u57ce\u5e02\u201d\uff0c\u5219\u53ef\u4ee5\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre>var eleSelect = document.querySelector('select');\r\nif (eleSelect.validity.valueMissing == true) {\r\n    eleSelect.setCustomValidity('\u8bf7\u9009\u62e9\u57ce\u5e02');\r\n}\r\neleSelect.reportValidity();\r\n<\/pre>\n<p>\u6b64\u65f6\uff0c\u63d0\u793a\u6548\u679c\u5c31\u662f\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201908\/2019-08-18_204144.png\" width=\"696\" height=\"167\" alt=\"\u6539\u53d8\u63d0\u793a\u7684\u6587\u6848\u4fe1\u606f\" class=\"alignnone size-medium\" \/><\/p>\n<h4>validity\u5c5e\u6027\u4e0eValidityState\u5bf9\u8c61<\/h4>\n<p>\u6bcf\u4e00\u4e2a\u6807\u51c6\u7684\u8868\u5355\u63a7\u4ef6\uff0c\u4f8b\u5982\u8f93\u5165\u6846\uff0c\u4e0b\u62c9\u6846\u4ee5\u53ca\u5355\u590d\u9009\u6846\u90fd\u5185\u7f6e\u4e00\u4e2a<code>validity<\/code>\u5c5e\u6027\uff0c\u8fd9\u662f\u4e2a\u53ea\u8bfb\u5c5e\u6027\uff0c\u53ef\u4ee5\u8fd4\u56de\u5f53\u524d\u5143\u7d20\u5404\u79cd\u9a8c\u8bc1\u72b6\u6001\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>console.dir(document.querySelector('select').validity);<\/pre>\n<p>\u5728Chrome\u6d4f\u89c8\u5668\u4e0b\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u4e2aValidityState\u5bf9\u8c61\uff0c\u5305\u542b\u7684\u5c5e\u6027\u548c\u5c5e\u6027\u503c\u5982\u4e0b\uff1a<\/p>\n<pre>badInput: false\r\ncustomError: false\r\npatternMismatch: false\r\nrangeOverflow: false\r\nrangeUnderflow: false\r\nstepMismatch: false\r\ntooLong: false\r\ntooShort: false\r\ntypeMismatch: false\r\nvalid: false\r\nvalueMissing: true<\/pre>\n<p>\u63a7\u5236\u53f0\u8f93\u51fa\u622a\u56fe\u5982\u4e0b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201908\/2019-08-18_200146.png\" width=\"476\" height=\"275\" alt=\"\u5185\u7f6e\u7684\u9a8c\u8bc1\u72b6\u6001\u5bf9\u8c61\" class=\"alignnone size-medium\" \/><\/p>\n<p><strong>\u5176\u4e2d<\/strong><\/p>\n<dl>\n<dt><strong>badInput<\/strong><\/dt>\n<dd>\u53ea\u8bfb\u3002\u5c5e\u6027\u503c\u4e3a\u5e03\u5c14\u7c7b\u578b\u3002\u8f93\u5165\u6846\u91cc\u9762\u7684\u503c\u6d4f\u89c8\u5668\u6ca1\u529e\u6cd5\u8fdb\u884c\u8f6c\u6362\u3002\u4f8b\u5982<code>'number'<\/code>\u7c7b\u578b\u8f93\u5165\u6846\u91cc\u9762\u662f\u4e2d\u6587\u5b57\u7b26\uff08MDN\u4e0a\u8bf4\u6cd5\uff0c\u4f46\u662f\u81ea\u5df1\u6d4b\u8bd5\u65e0\u6cd5\u590d\u73b0\uff09\uff0c\u6b64\u65f6\u503c\u662f<code>true<\/code>\u3002\u6b64\u5c5e\u6027IE\u6d4f\u89c8\u5668\u5e76\u4e0d\u652f\u6301\u3002\u6b64\u5c5e\u6027\u5efa\u8bae\u4e86\u89e3\u5373\u53ef\u3002<\/dd>\n<dt><strong>customError<\/strong><\/dt>\n<dd>\u53ea\u8bfb\u3002\u5c5e\u6027\u503c\u4e3a\u5e03\u5c14\u7c7b\u578b\u3002\u5982\u679c\u5143\u7d20\u8c03\u7528<code>setCustomValidity()<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u4e86\u81ea\u5b9a\u4e49\u7684\u9a8c\u8bc1\u4fe1\u606f\u5219\u503c\u662f<code>true<\/code>\u3002<\/dd>\n<dt><strong>patternMismatch<\/strong><\/dt>\n<dd>\u53ea\u8bfb\u3002\u5c5e\u6027\u503c\u4e3a\u5e03\u5c14\u7c7b\u578b\u3002\u548c\u6307\u5b9a\u7684<code>pattern<\/code>\u6b63\u5219\u5c5e\u6027\u503c\u4e0d\u5339\u914d\u7684\u65f6\u5019\u503c\u662f<code>true<\/code>\u3002\u4f1a\u5339\u914d<code>:invalid<\/code>\u8fd9\u4e2aCSS\u4f2a\u7c7b\u3002<\/dd>\n<dt><strong>rangeOverflow<\/strong><\/dt>\n<dd>\u53ea\u8bfb\u3002\u5c5e\u6027\u503c\u4e3a\u5e03\u5c14\u7c7b\u578b\u3002\u8d85\u8fc7<code>max<\/code>\u5c5e\u6027\u8bbe\u7f6e\u7684\u503c\u7684\u65f6\u5019\u4f1a\u662f<code>true<\/code>\u3002\u540c\u65f6\u4f1a\u5339\u914dCSS <code>:invalid<\/code>\u548c<code>:out-of-range<\/code>\u4f2a\u7c7b\u3002<\/dd>\n<dt><strong>rangeUnderflow<\/strong><\/dt>\n<dd>\u53ea\u8bfb\u3002\u5c5e\u6027\u503c\u4e3a\u5e03\u5c14\u7c7b\u578b\u3002\u5c0f\u4e8e<code>min<\/code>\u5c5e\u6027\u8bbe\u7f6e\u7684\u503c\u7684\u65f6\u5019\u4f1a\u662f<code>true<\/code>\u3002\u540c\u65f6\u4f1a\u5339\u914dCSS <code>:invalid<\/code>\u548c<code>:out-of-range<\/code>\u4f2a\u7c7b\u3002<\/dd>\n<dt><strong>stepMismatch<\/strong><\/dt>\n<dd>\u53ea\u8bfb\u3002\u5c5e\u6027\u503c\u4e3a\u5e03\u5c14\u7c7b\u578b\u3002\u8f93\u5165\u6846\u5f53\u524d\u503c\u548c<code>step<\/code>\u5c5e\u6027\u503c\u4e0d\u5339\u914d\u7684\u65f6\u5019<code>stepMismatch<\/code>\u7684\u503c\u4f1a\u662f<code>true<\/code>\u3002\u540c\u65f6\u5143\u7d20\u4f1a\u5339\u914dCSS <code>:invalid<\/code>\u548c<code>:out-of-range<\/code>\u4f2a\u7c7b\u3002<\/dd>\n<dt><strong>tooLong<\/strong><\/dt>\n<dd>\u53ea\u8bfb\u3002\u5c5e\u6027\u503c\u4e3a\u5e03\u5c14\u7c7b\u578b\u3002\u8f93\u5165\u5185\u5bb9\u957f\u5ea6\u8d85\u51fa<code>maxlength<\/code>\u7684\u9650\u5236\u65f6\u5019\u4f1a\u662f<code>true<\/code>\u3002\u540c\u65f6\u4f1a\u5339\u914dCSS <code>:invalid<\/code>\u548c<code>:out-of-range<\/code>\u4f2a\u7c7b\u3002<\/dd>\n<dt><strong>tooShort<\/strong><\/dt>\n<dd>\u53ea\u8bfb\u3002\u5c5e\u6027\u503c\u4e3a\u5e03\u5c14\u7c7b\u578b\u3002\u8f93\u5165\u5185\u5bb9\u957f\u5ea6\u4e0d\u8db3<code>minlength<\/code>\u7684\u9650\u5236\u65f6\u5019\u4f1a\u662f<code>true<\/code>\u3002\u540c\u65f6\u4f1a\u5339\u914dCSS <code>:invalid<\/code>\u548c<code>:out-of-range<\/code>\u4f2a\u7c7b\u3002\u6b64\u5c5e\u6027IE\u6d4f\u89c8\u5668\u5e76\u4e0d\u652f\u6301\uff0c\u56e0\u4e3a\u4e0d\u652f\u6301<code>minlength<\/code>\u5c5e\u6027\u3002<\/dd>\n<dt><strong>typeMismatch<\/strong><\/dt>\n<dd>\u53ea\u8bfb\u3002\u5c5e\u6027\u503c\u4e3a\u5e03\u5c14\u7c7b\u578b\u3002\u8f93\u5165\u6846\u7684\u503c\u548c\u8f93\u5165\u6846\u7c7b\u578b\u4e0d\u5339\u914d\u7684\u65f6\u5019\u8be5\u5c5e\u6027\u503c\u4f1a\u662f<code>true<\/code>\u3002\u4f8b\u5982\u8f93\u5165\u6846type\u7c7b\u578b\u662femail\u6216\u8005url\u65f6\u5019\u3002\u5982\u679c\u503c\u662f<code>true<\/code>\uff0c\u4f1a\u5339\u914d<code>:invalid<\/code>\u8fd9\u4e2aCSS\u4f2a\u7c7b\u3002<\/dd>\n<dt><strong>valid<\/strong><\/dt>\n<dd>\u53ea\u8bfb\u3002\u5c5e\u6027\u503c\u4e3a\u5e03\u5c14\u7c7b\u578b\u3002\u5f53\u524d\u5143\u7d20\u662f\u5426\u5b8c\u5168\u9a8c\u8bc1\u901a\u8fc7\uff0c\u901a\u8fc7\u662f<code>true<\/code>\uff0c\u4f1a\u5339\u914d<code>:valid<\/code>\u8fd9\u4e2aCSS\u4f2a\u7c7b\uff1b\u4e0d\u901a\u8fc7\u662f<code>false<\/code>\uff0c\u4f1a\u5339\u914d<code>:invalid<\/code>\u8fd9\u4e2aCSS\u4f2a\u7c7b\u3002<\/dd>\n<dt><strong>valueMissing<\/strong><\/dt>\n<dd>\u53ea\u8bfb\u3002\u5c5e\u6027\u503c\u4e3a\u5e03\u5c14\u7c7b\u578b\u3002\u5982\u679c\u5143\u7d20\u8bbe\u7f6e\u4e86<code>required<\/code>\u5c5e\u6027\uff0c\u540c\u65f6\u8f93\u5165\u6846\u7684\u503c\u4e3a\u7a7a\uff0c\u5219\u8be5\u5c5e\u6027\u503c\u662f<code>true<\/code>\u3002\u5982\u679c\u503c\u662f<code>true<\/code>\uff0c\u4f1a\u5339\u914d<code>:invalid<\/code>\u8fd9\u4e2aCSS\u4f2a\u7c7b\u3002<\/dd>\n<\/dl>\n<p><strong>\u8865\u5145\u4e8e2021-07-30<\/strong><\/p>\n<h4>validationMessage\u5c5e\u6027<\/h4>\n<p>\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>var message = ele.validationMessage;<\/pre>\n<p>\u8868\u793a\uff0c\u5f53\u524d\u8f93\u5165\u6846\u5143\u7d20\u5982\u679c\u8981\u663e\u793a\u51fa\u9519\u63d0\u793a\uff0c\u51fa\u9519\u63d0\u793a\u7684\u6587\u5b57\u5185\u5bb9\u662f\u4ec0\u4e48\u3002<\/p>\n<p>\u4f8b\u5982\u4e00\u4e2a\u666e\u901a\u7684\u8f93\u5165\u6846\uff1a<\/p>\n<pre>&lt;input id=\"input\"&gt;<\/pre>\n<p><code>input.validationMessage<\/code>\u7684\u8fd4\u56de\u503c\u662f\u7a7a\u5b57\u7b26\u4e32<code>''<\/code>\u3002<\/p>\n<p>\u5982\u679c\u8f93\u5165\u6846\u8bbe\u7f6e\u4e86required\u5c5e\u6027\uff1a<\/p>\n<pre>&lt;input id=\"input\" required&gt;<\/pre>\n<p>\u5219<code>input.validationMessage<\/code>\u7684\u8fd4\u56de\u503c\u662f\uff1a&#8217;\u8bf7\u586b\u5199\u6b64\u5b57\u6bb5\u3002&#8217;<\/p>\n<p>\u5982\u679c\u8f93\u5165\u6846\u8bbe\u7f6e\u4e86<code>pattern<\/code>\u5c5e\u6027\uff0c\u540c\u65f6\u8f93\u5165\u6846\u91cc\u9762\u7684\u503c\u683c\u5f0f\u4e0d\u7b26\uff1a<\/p>\n<pre>&lt;input id=\"input\" required pattern=\"^\\d{6}$\"&gt;<\/pre>\n<p>\u5219<code>input.validationMessage<\/code>\u7684\u8fd4\u56de\u503c\u662f\uff1a&#8217;\u8bf7\u4e0e\u6240\u8bf7\u6c42\u7684\u683c\u5f0f\u4fdd\u6301\u4e00\u81f4\u3002&#8217;<\/p>\n<p>\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u5e38\u89c1\u5c31\u4e0d\u4e3e\u4f8b\u4e86\uff0c\u8fd0\u884c\u7ed3\u679c\u793a\u610f\u5982\u4e0b\u622a\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202107\/2021-07-30_132341.png\" width=\"256\" height=\"182\" alt=\"\u9a8c\u8bc1\u63d0\u793a\u4fe1\u606f\u8bf4\u660e\" class=\"alignnone size-medium\" \/><\/p>\n<h4>willValidate\u5c5e\u6027<\/h4>\n<p>\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>var result = ele.willValidate;<\/pre>\n<p>\u8868\u793a\u5143\u7d20\u80fd\u591f\u88ab\u9a8c\u8bc1\u3002<\/p>\n<p>\u8f93\u5165\u6846\u5143\u7d20\u90fd\u662f<code>true<\/code>\uff0c\u4f46\u662f\u5982\u679c\u8f93\u5165\u6846\u662f<code>disabled<\/code>\u7981\u7528\u7684\uff0c\u5219<code>result<\/code>\u662f<code>false<\/code>\u3002<\/p>\n<p>\u8fd9\u91cc\u8fd9\u5f20\u6d4b\u8bd5\u7ed3\u679c\u793a\u610f\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202107\/2021-07-30_133337.png\" width=\"468\" height=\"242\" alt=\"willValidate\u503c\u662ffalse\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e09\u3001IE9\u6d4f\u89c8\u5668\u7684polyfill<\/h3>\n<p>\u4ee5\u4e0a\u6240\u6709\u539f\u751f\u7684\u8868\u5355\u9a8c\u8bc1\u65b9\u6cd5\u548c\u5c5e\u6027IE9\u6d4f\u89c8\u5668\u90fd\u4e0d\u652f\u6301\uff0cIE10+\u6d4f\u89c8\u5668\u90fd\u652f\u6301\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201908\/2019-08-18_223358.png\" width=\"578\" height=\"484\" alt=\"\u8868\u5355\u539f\u751f\u9a8c\u8bc1\u65b9\u6cd5\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5982\u679c\u60f3\u8981IE9\u6d4f\u89c8\u5668\u4e5f\u652f\u6301\uff0c\u9700\u8981\u5199\u4e2apolyfill\uff0c\u6211\u5927\u6982\u641c\u7d22\u4e86\u4e0b\uff0c<code>checkValidity()<\/code>\u7b49\u51e0\u4e2a\u65b9\u6cd5\u5e76\u6ca1\u6709\u5408\u9002\u7684polyfill\uff0c\u5df2\u6709\u7684\u4e00\u4e9b\u9879\u76ee\u4e3a\u4e86\u517c\u5bb9IE8\uff0c\u66f4\u50cf\u662fshim\uff0c\u6ca1\u6709\u4e13\u95e8\u4e3aIE9\u6253\u9020\u4e86polyfill\u3002<\/p>\n<p>\u5012\u662fvalidity\u5c5e\u6027\u627e\u5230\u4e2a\u770b\u4e0a\u53bb\u6709\u4e9b\u9760\u8c31\u7684polyfill\u3002\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff08\u53ef\u4ee5\u53f3\u952e\u2192\u53e6\u5b58\u4e3a\uff09\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/201908\/validity-polyfill.js\" rel=\"noopener\" target=\"_blank\">validity-polyfill.js<\/a>\u3002\u8fd9\u4e2apolyfill\u51fa\u81ea<a href=\"https:\/\/css-tricks.com\/form-validation-part-3-validity-state-api-polyfill\/\" rel=\"noopener nofollow\" target=\"_blank\">\u8fd9\u91cc<\/a>\uff0c\u4e0d\u8fc7\u6211\u5e76\u6ca1\u6709\u4ed4\u7ec6\u9a8c\u8bc1\u8fc7\u5176\u6b63\u786e\u6027\u3002<\/p>\n<p>\u4e4b\u540e\u6211\u4f1a\u81ea\u5df1\u9488\u5bf9IE9\u6d4f\u89c8\u5668\u5427<code>checkValidity()<\/code>\u7b49\u51e0\u4e2a\u65b9\u6cd5\u7ed9\u8865\u4e0a\u3002<\/p>\n<h3>\u56db\u3001\u5199\u7740\u5199\u7740\u5c31\u6210\u4e86\u6587\u6863<\/h3>\n<p>\u672c\u6587\u4e0b\u7b14\u4e4b\u5904\u8fd8\u4ee5\u4e3a\u4f1a\u53d1\u73b0\u4e00\u4e9b\u597d\u73a9\u7684\u4e1c\u897f\uff0c\u4f8b\u5982\u81ea\u5b9a\u4e49\u8868\u5355\u9a8c\u8bc1\u903b\u8f91\uff0c\u540e\u6765\u53d1\u73b0\u81ea\u5df1\u60f3\u591a\u4e86\uff0c<code>setCustomValidity()<\/code>\u65b9\u6cd5\u5e76\u6ca1\u6709\u81ea\u5df1\u9884\u60f3\u7684\u90a3\u4e48\u5f3a\u5927\uff0c\u5c31\u662f\u4e2a\u6539\u53d8\u63d0\u793a\u6587\u5b57\u4fe1\u606f\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u6574\u7406\u6765\u6574\u7406\u53bb\uff0c\u6700\u540e\u5c31\u6210\u4e86\u4e00\u4e2a\u6bd4\u8f83\u8be6\u5c3d\u7684\u5173\u4e8e\u8868\u5355\u539f\u751f\u9a8c\u8bc1\u7684\u6587\u6863\u4e86\u3002<\/p>\n<p>\u4e4b\u540e\u5199\u9a8c\u8bc1\u7ec4\u4ef6\uff0c\u6211\u4f1a\u501f\u52a9\u8fd9\u4e9b\u539f\u751f\u7684\u5df2\u7ecf\u5185\u7f6e\u597d\u7684\u9a8c\u8bc1\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u53ef\u4ee5\u7701\u53bb\u4e0d\u5c11\u529f\u592b\uff0c\u4f1a\u5f88\u6709\u610f\u601d\u3002<\/p>\n<p>\u65e5\u540e\u7684\u7cbe\u5f69\u6f14\u7ece\u6e90\u81ea\u5e73\u65f6\u5e73\u6de1\u7684\u79ef\u7d2f\u3002<\/p>\n<p>\u5bf9\u4e8e\u5927\u90e8\u5206\u7684\u4eba\uff0c\u5bf9\u4e8e\u672c\u6587\u8fd9\u4e9bAPI\u662f\u65e0\u611f\u7684\uff0c\u60f3\u4e0d\u5230\u5982\u4f55\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528\uff0c\u89c9\u5f97\u5f88\u9e21\u808b\uff0c\u5b9e\u9645\u4e0a\u5927\u6709\u53ef\u4e3a\u7684\uff0c\u6211\u5df2\u7ecf\u60f3\u8981\u8dc3\u8dc3\u6b32\u8bd5\u4e86\uff0c\u563f\u563f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/1f913.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5185\u5bb9\u4f60\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u4ee5\u52a0\u5165\u6536\u85cf\u5939\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u800c\u4e14\u8fd8\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=8895\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=8895<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201908\/background-thumb.png\" width=\"161\" height=\"100\" class=\"alignright  size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u672c\u6587\u4ecb\u7ecdform\u8868\u5355\u5143\u7d20\u5185\u7f6e3\u4e2a\u9a8c\u8bc1\u65b9\u6cd5\u548c1\u4e2a\u5c5e\u6027\uff0c3\u4e2a\u65b9\u6cd5\u5206\u522b\u662fcheckValidity(), reportValidity(), setCustomValidity()\u65b9\u6cd5\uff0c1\u4e2a\u5c5e\u6027\u662fvalidity\u5c5e\u6027\u3002\u4e0d\u4e86\u89e3\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u8fdb\u6765\u4e86\u89e3\u4e00\u4e0b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[1390,827,825,1469,607,1388,546,1468,1470],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/8895"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=8895"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/8895\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=8895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=8895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=8895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}