{"id":844,"date":"2022-01-18T23:08:14","date_gmt":"2022-01-18T15:08:14","guid":{"rendered":"http:\/\/www.henrycome.com\/wordpress\/?p=844"},"modified":"2022-01-19T12:43:32","modified_gmt":"2022-01-19T04:43:32","slug":"20220118230813","status":"publish","type":"post","link":"http:\/\/www.henrycome.com\/wordpress\/202201182308\/844","title":{"rendered":"\u3010JS\u57fa\u7840\u3011\u8868\u5355\u9a8c\u8bc1"},"content":{"rendered":"<blockquote>\n<p>JavaScript \u8868\u5355 | \u83dc\u9e1f\u6559\u7a0b<br \/>\n<a href=\"https:\/\/www.runoob.com\/js\/js-validation.html\">https:\/\/www.runoob.com\/js\/js-validation.html<\/a><br \/>\nJavaScript \u8868\u5355\u9a8c\u8bc1 | \u83dc\u9e1f\u6559\u7a0b<br \/>\n<a href=\"https:\/\/www.runoob.com\/js\/js-form-validation.html\">https:\/\/www.runoob.com\/js\/js-form-validation.html<\/a><br \/>\nJavaScript \u9a8c\u8bc1 API | \u83dc\u9e1f\u6559\u7a0b<br \/>\n<a href=\"https:\/\/www.runoob.com\/js\/js-validation-api.html\">https:\/\/www.runoob.com\/js\/js-validation-api.html<\/a><\/p>\n<\/blockquote>\n<p>\u5f15\u6587\u8bf4\u660e\uff1a\u7b2c\u4e00\u7bc7\u5176\u5b9e\u662f\u8868\u5355\u9a8c\u8bc12\u7c7b\u65b9\u5f0f\u7684\u7b80\u4ecb\uff0c\u540d\u5b57\u53eb\u201c\u8868\u5355\u201d\u5176\u5b9e\u4e0d\u592a\u5408\u7406\uff1b\u7b2c\u4e8c\u7bc7\u662fJS\u81ea\u5b9a\u4e49\u5224\u65ad\u65b9\u5f0f\uff1b\u7b2c\u4e09\u7bc7\u662fHTML5\u65b0\u589e\u7684\u7ea6\u675f\u9a8c\u8bc1\u65b9\u5f0f\u3002<\/p>\n<h2>\u5143\u7d20\u81ea\u5e26\u7ea6\u675f\u5c5e\u6027\u7684\u4f7f\u7528<\/h2>\n<p>\u8868\u5355\u5177\u4f53\u5e94\u8be5\u662finput\u5143\u7d20\u6709\u5bf9\u8f93\u5165\u503c\u7684\u7ea6\u675f\u6027\u7684\u5143\u7d20\uff1adisabled\u3001max\u3001min\u3001pattern\u3001type\u3001required\u3002<\/p>\n<p>\u5982\u679c\u8868\u5355\u5143\u7d20\u6709\u7ea6\u675f\u6027\u5c5e\u6027\u88ab\u8bbe\u7f6e\uff0c\u90a3\u4e48\u5c31\u6309\u7167\u8fd9\u4e2a\u5bf9\u8c61\u7684checkValidity()\u65b9\u6cd5\u6765\u5224\u65ad\uff1a<\/p>\n<p>checkValidity()\u65b9\u6cd5\uff1a\u7b26\u5408true\uff0c\u4e0d\u7b26\u5408false\uff0c<\/p>\n<pre><code class=\"language-javascript\">\u5bf9\u8c61.checkValidity()== true \/\/\u8bf4\u660e\u7b26\u5408\uff0c\n\u5bf9\u8c61.checkValidity()== false \/\/\u4e0d\u7b26\u5408<\/code><\/pre>\n<p>\u4e0a\u9762checkValidity()\u65b9\u6cd5\u4e0d\u591f\u5177\u4f53\uff0c\u7528if\u518d\u53bb\u5224\u65ad\u5177\u4f53\u60c5\u51b5\u6bd4\u8f83\u9ebb\u70e6\u3002\u8fd8\u597d\u6709\u73b0\u6210\u7684validity\u5c5e\u6027\uff08\u503c\u4e5f\u662ftrue\u548cfalse\uff09\u63d0\u4f9b\u5177\u4f53\u7684\u5224\u65ad\uff0c\u5e76\u4e14\u6709\u5bf9\u5e94\u7684validationMessage\u5c5e\u6027\u503c\uff08string\u7c7b\u578b\u7684\u9ed8\u8ba4\u63d0\u793a\u4fe1\u606f\uff09\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-js\">\u5bf9\u8c61.validity.rangeOverflow == true\/\/\u4e3a\u771f\u65f6\uff0c\u8868\u660e\u5927\u4e8emax\u8bbe\u5b9a\u7684\u6700\u5927\u503c\n\/\/\u8fd9\u65f6\u5bf9\u5e94\u7684ValidationMessage\u503c\n\u5bf9\u8c61.validationMessage\/\/\u503c\uff1a\u503c\u5fc5\u987b\u5c0f\u4e8e\u6216\u7b49\u4e8e 300\u3002\uff08\u6ce8\uff1a300\u662fHTML\u5143\u7d20max\u5c5e\u6027\u8bbe\u5b9a\u503c\uff09<\/code><\/pre>\n<p>validity\u5c5e\u6027\u548cvalidationMessage\u7684\u9ed8\u8ba4\u503c\uff08\u6839\u636e\u7cfb\u7edf\u8bed\u8a00\u800c\u4e0d\u540c\uff09\uff0c\u5217\u8868\u5982\u4e0b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>HTML\u5143\u7d20\u7ea6\u675f\u5c5e\u6027<\/th>\n<th>validity\u5c5e\u6027<\/th>\n<th>validationMessage\u4e2d\u6587\u503c<\/th>\n<th>validationMessage\u82f1\u6587\u503c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>required\uff1a<\/td>\n<td>valueMissing<\/td>\n<td>\u8bf7\u586b\u5199\u6b64\u5b57\u6bb5\u3002<\/td>\n<td>Please fill out this field.<\/td>\n<\/tr>\n<tr>\n<td>type=&quot;number&quot;\uff1a<\/td>\n<td>typeMismatch<\/td>\n<td>number\u65f6\u5176\u4ed6\u5b57\u7b26\u65e0\u6cd5\u8f93\u5165\u6d4b\u4e0d\u4e86\uff0cemail\u65f6\u9519\u8bef\u4fe1\u606f\u4e3a\uff1a<\/td>\n<td>Please include an &#8216;@&#8217; in the email address. &#8216;default&#8217; is missing an &#8216;@&#8217;.<\/td>\n<\/tr>\n<tr>\n<td>min=&quot;100&quot;\uff1a<\/td>\n<td>rangeUnderflow<\/td>\n<td>\u503c\u5fc5\u987b\u5927\u4e8e\u6216\u7b49\u4e8e 100\u3002<\/td>\n<td>Value must be greater than or equal to 100.<\/td>\n<\/tr>\n<tr>\n<td>max=&quot;300&quot;\uff1a<\/td>\n<td>rangeOverflow<\/td>\n<td>\u503c\u5fc5\u987b\u5c0f\u4e8e\u6216\u7b49\u4e8e 300\u3002<\/td>\n<td>Value must be less than or equal to 300.<\/td>\n<\/tr>\n<tr>\n<td>step=&quot;3&quot;\uff1a<\/td>\n<td>stepMismatch<\/td>\n<td>\u9002\u7528\u4e8enumber\u3001range\u3001date\u3001datetime\u3001datetime-local\u3001month\u3001time \u548c week<\/td>\n<td>Please enter a valid value. The two nearest valid values are 100 and 103.<\/td>\n<\/tr>\n<tr>\n<td>pattern=&quot;[A-Za-z]{3}&quot;\uff1a<\/td>\n<td>patternMismatch<\/td>\n<td>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/td>\n<td>Please match the requested format.<\/td>\n<\/tr>\n<tr>\n<td>maxlenth=&quot;300&quot;\uff1a<\/td>\n<td>tooLong<\/td>\n<td>type\u662f\u6587\u672c\u65f6\u53ef\u7528\uff08\u6682\u5b9a\uff09<\/td>\n<td>\u8d85\u51fa\u957f\u5ea6\u65e0\u6cd5\u8f93\u5165\uff0c\u6682\u672a\u6d4b\u8bd5tooLong\u548cMessage\u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td>\u5982\u679c\u90fd\u7b26\u5408\uff1a<\/td>\n<td>valid<\/td>\n<td>\u7a7a\u5b57\u7b26<\/td>\n<td>\u6548\u679c\u7b49\u540c&#8221;<\/td>\n<\/tr>\n<tr>\n<td>\u8fd9\u4e2a\u662f\u7279\u4f8b\uff1a<\/td>\n<td>customError<\/td>\n<td>\u81ea\u5b9a\u4e49\u9519\u8bef\u63d0\u793a\u540e\uff0ccustomError\u503c\u4e00\u76f4\u662ffalse<\/td>\n<td>\u4e0b\u9762\u4f1a\u8bb2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8865\u8bb0\uff1a\u5982\u679ctype\u662fnumber\uff0cmax\u6216min\u3001step\u90fd\u8bbe\u7f6e\uff0c\u5982\u679c\u90fd\u6709\u9519\uff0c\u90a3\u4e48\u4f18\u5148\u663e\u793amax\u6216min\u7684\u9519\u8bef\u63d0\u793a\u3002\u5176\u4ed6\u7684\u4f18\u5148\u7ea7\u6682\u672a\u6d4b\u8bd5\u3002<\/p>\n<p>\u5982\u679c\u60f3\u81ea\u5b9a\u4e49\u9519\u8bef\u63d0\u793a\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528setCustomValidity()\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-js\">if(rangeOverflow){\nsetCustomValidity(&#039;\u8d85\u8fc7\u8bb8\u613f\u6570\u91cf\uff0c\u60f3\u5f97\u592a\u7f8e\uff01&#039;)\n}<\/code><\/pre>\n<p>\u60f3\u67e5\u770b\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u53ef\u4ee5\u5e94\u7528\u201c\u7ea6\u675f\u9a8c\u8bc1\u201d\uff0c\u53ef\u4ee5\u67e5\u770b\u5176willValidate\u5c5e\u6027\u3002\u4ee5\u4e0b\u5b9a\u4e49\u548c\u793a\u4f8b\u6765\u81ea\uff1a<\/p>\n<blockquote>\n<p>HTMLObjectElement.willValidate &#8211; Web APIs | MDN<br \/>\n<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/HTMLObjectElement\/willValidate\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/HTMLObjectElement\/willValidate<\/a><\/p>\n<\/blockquote>\n<p>The willValidate read-only property of the HTMLObjectElement interface returns a boolean value that indicates whether the element is a candidate for constraint validation. Always false for HTMLObjectElement objects.<\/p>\n<pre><code class=\"language-js\">var Boolean = HTMLObjectElement.willValidate;<\/code><\/pre>\n<p>\u90a3\u4e48\uff0c\u6211\u611f\u89c9\u633a\u5947\u602a\uff0c\u8fd9\u91cc\u4f3c\u4e4e\u5f88\u590d\u6742\uff0c\u6709\u91cd\u590d\u4f30\u8ba1\u662f\u7ed9\u60f3\u81ea\u5df1\u5199\u9519\u8bef\u63d0\u793a\u7684\u4eba\u63d0\u4f9b\u65b9\u4fbf\u5427\uff0c\u514d\u53bb\u81ea\u5df1\u5199\u4ee3\u7801\u5224\u65ad\u60c5\u51b5\uff0c\u5c31\u901a\u8fc7validity.\u5c5e\u6027\u7684\u65b9\u5f0f\u7ed9\u6982\u62ec\u4e86\u3002<\/p>\n<pre><code class=\"language-js\">\/\/\u7ed9setCustomValidity()\u53c2\u6570\u4e3a&#039;&#039;,\u662f\u89c4\u5b9a\u7684\u4e00\u4e2a\u53d6\u6d88\u81ea\u5b9a\u4e49\u7684\u65b9\u5f0f\uff1b\n\/\/\u4e4b\u6240\u4ee5\u5148\u5f00\u5934\u53d6\u6d88\u81ea\u5b9a\u4e49\uff0c\u662f\u56e0\u4e3a\u70b9\u51fb\u7b2c\u4e8c\u6b21\u6309\u94ae\u7684\u65f6\u5019\uff0ccheckValidity()\u603b\u4f1a\u7ed9\u51fafalse\u503c\uff1b\n\/\/checkValidity()\u4e4b\u524d\u4f7f\u7528setCustomValidity(&#039;XXX&#039;)\uff0c\u4f1a\u5e72\u6270checkValidity()\u5224\u65ad\uff0c\u603b\u4f1a\u8fd4\u56defalse\uff1b\u7b2c\u4e8c\u6b21\u70b9\u51fb\u6309\u94ae\u91cd\u590d\u6267\u884c\u51fd\u6570\uff0c\u5c31\u76f8\u5f53\u4e8e\u5728\u7b2c\u4e8c\u6b21checkValidity()\u4e4b\u524d\u4f7f\u7528\u4e86setCustomValidity()\uff0c\u9664\u975e\u5173\u95ed\u7f51\u9875\n\nobj.setCustomValidity(&#039;&#039;)\nif (obj.checkValidity()){\n    alert(&#039;\u8bb8\u613f\u6570\u91cfOK\uff0c\u9a6c\u4e0a\u5b9e\u73b0\uff01&#039;)\n    else {\n        if(obj.validity.rangeOverflow){\n        obj.setCustomValidity(&#039;\u8d85\u8fc7\u8bb8\u613f\u6570\u91cf\uff0c\u60f3\u5f97\u592a\u7f8e\uff01&#039;)}\n        else if(obj.validity.valueMissing){\n        obj.setCustomValidity(&#039;\u6570\u91cf\u4e3a0\uff0c\u4e3a\u5565\u4e0d\u8bb8\u613f\u5462\uff1f&#039;)}\n        \/\/.............\n        alert(obj.validationMessage)\n}\n<\/code><\/pre>\n<h2>JS\u81ea\u5b9a\u4e49\u7ea6\u675f<\/h2>\n<p>\u901a\u8fc7\u83b7\u53d6\u8868\u5355\u5143\u7d20\u7684\u503c\uff0c\u901a\u8fc7JS\u5224\u65ad\u8f93\u5165\u503c\u662f\u5426\u7b26\u5408\u8981\u6c42\u3002<br \/>\n\u4e0b\u9762\u7684\u7f51\u7ad9\u4e2d\u6709\u5fc5\u586b\u548cEmail\u683c\u5f0f\u9a8c\u8bc12\u4e2a\u4f8b\u5b50\u3002<br \/>\nJavaScript \u8868\u5355\u9a8c\u8bc1 | \u83dc\u9e1f\u6559\u7a0b<br \/>\n<a href=\"https:\/\/www.runoob.com\/js\/js-form-validation.html\">https:\/\/www.runoob.com\/js\/js-form-validation.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u8868\u5355 | \u83dc\u9e1f\u6559\u7a0b https:\/\/www.runoob.com\/js\/js-valid [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49,21],"tags":[],"_links":{"self":[{"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/posts\/844"}],"collection":[{"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/comments?post=844"}],"version-history":[{"count":11,"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/posts\/844\/revisions"}],"predecessor-version":[{"id":855,"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/posts\/844\/revisions\/855"}],"wp:attachment":[{"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/media?parent=844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/categories?post=844"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.henrycome.com\/wordpress\/wp-json\/wp\/v2\/tags?post=844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}