{"id":37,"date":"2007-07-24T18:20:42","date_gmt":"2007-07-24T22:20:42","guid":{"rendered":"http:\/\/prglab.com\/blog\/js-validation"},"modified":"2007-08-25T14:44:11","modified_gmt":"2007-08-25T18:44:11","slug":"js-validation","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/js-tutorial\/js-validation","title":{"rendered":"JavaScript \u8868\u5355\u9a8c\u8bc1(Form Validation)"},"content":{"rendered":"<p class=\"intro\">JavaScript \u53ef\u4ee5\u88ab\u7528\u6765\u5728\u5411\u670d\u52a1\u5668\u53d1\u9001\u5185\u5bb9\u524d\u9a8c\u8bc1HTML\u8868\u5355\u4e2d\u8f93\u5165\u7684\u4fe1\u606f\u662f\u5426\u7b26\u5408\u8981\u6c42\u3002<\/p>\n<hr \/>\n<h2>JavaScript \u8868\u5355\u9a8c\u8bc1<\/h2>\n<p>\u5728\u5411\u670d\u52a1\u5668\u53d1\u9001\u4fe1\u606f\u524d\uff0c\u53ef\u4ee5\u4f7f\u7528JavaScript\u6765\u9a8c\u8bc1\u4e00\u4e2aHTML \u8868\u5355(form)\u4e2d\u8f93\u5165\u7684\u4fe1\u606f\u662f\u5426\u5408\u6cd5\u3002\u901a\u5e38\u4f7f\u7528JavaScript\u9a8c\u8bc1\u7684\u8868\u5355\u6570\u636e\u53ef\u4ee5\u662f\uff1a<\/p>\n<ul>\n<li>\u5fc5\u586b\u6570\u636e\u662f\u5426\u4e3a\u7a7a?<\/li>\n<li>\u7528\u6237\u8f93\u5165\u7684e-mail\u5730\u5740\u662f\u5426\u5408\u6cd5?<\/li>\n<li>\u7528\u6237\u8f93\u5165\u7684\u65e5\u671f\u662f\u5426\u6709\u6548?<\/li>\n<li>\u4e00\u4e2a\u6570\u503c\u57df\u662f\u5426\u8f93\u5165\u4e86\u975e\u6570\u503c\u6587\u672c?<\/li>\n<\/ul>\n<hr \/>\n<h2>\u68c0\u67e5\u5fc5\u586b\u6570\u636e<\/h2>\n<p>\u4e0b\u9762\u7684\u51fd\u6570\u68c0\u67e5\u4e00\u4e2a\u5fc5\u586b\u6570\u636e\u57df\u662f\u5426\u4e3a\u7a7a\u3002\u5982\u679c\u4e3a\u7a7a\uff0c\u5219\u663e\u793a\u4e00\u4e2a\u8b66\u793a\u7a97\u53e3\u548c\u8b66\u793a\u4fe1\u606f\uff0c\u5e76\u8fd4\u56defalse\u503c\u3002\u5426\u5219\u76f4\u63a5\u8fd4\u56detrue  (\u610f\u5473\u7740\u6570\u636e\u6b63\u786e):<\/p>\n<table class=\"ex\" id=\"table7\" border=\"1\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td>\nfunction validate_required(field,alerttxt)<br \/>\n{<br \/>\nwith (field)<br \/>\n{<br \/>\nif (value==null||value==&#8221;&#8221;)<br \/>\n  {alert(alerttxt);return false}<br \/>\nelse {return true}<br \/>\n}<br \/>\n}\n<\/td>\n<\/tr>\n<\/table>\n<p>\u5b8c\u6574\u7684\u811a\u672c\u8fde\u540cHTML\u8868\u5355\u53ef\u4ee5\u50cf\u4e0b\u9762\u4f8b\u4e2d\u6240\u793a\uff1a<\/p>\n<table class=\"ex\" id=\"table8\" border=\"1\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td>\n&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\nfunction validate_required(field,alerttxt)<br \/>\n{<br \/>\nwith (field)<br \/>\n{<br \/>\nif (value==null||value==&#8221;&#8221;)<br \/>\n  {alert(alerttxt);return false}<br \/>\nelse {return true}<br \/>\n}<br \/>\n}<br \/>\nfunction validate_form(thisform)<br \/>\n{<br \/>\nwith (thisform)<br \/>\n{<br \/>\nif (validate_required(email,&#8221;Email \u5730\u5740\u5fc5\u987b\u586b\u5199!&#8221;)==false)<br \/>\n  {email.focus();return false}<br \/>\n}<br \/>\n}<br \/>\n&lt;\/script&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;form action=&#8221;submitpage.htm&#8221;<br \/>\nonsubmit=&#8221;return validate_form(this)&#8221;<br \/>\nmethod=&#8221;post&#8221;&gt;<br \/>\nEmail: &lt;input type=&#8221;text&#8221; name=&#8221;email&#8221; size=&#8221;30&#8243;&gt;<br \/>\n&lt;input type=&#8221;submit&#8221; value=&#8221;Submit&#8221;&gt;<br \/>\n&lt;\/form&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;\n<\/td>\n<\/tr>\n<\/table>\n<hr \/>\n<h2>E-mail \u9a8c\u8bc1<\/h2>\n<p>\u4e0b\u9762\u7684\u51fd\u6570\u53ef\u4ee5\u68c0\u67e5\u8f93\u5165\u7684email\u5730\u5740\u7684\u4e00\u822c\u683c\u5f0f\uff0c\u4e5f\u5c31\u662f\u8bf4\u8f93\u5165\u7684\u6570\u636e\u5fc5\u987b\u5305\u542b\u6700\u5c11\u4e00\u4e2a@\u7b26\u53f7\u548c\u4e00\u4e2a\u70b9 (.)\uff0c\u800c\u4e14 @ \u7b26\u53f7\u5fc5\u987b\u4e0d\u662femail\u5730\u5740\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff0c\u800c\u4e14\u6700\u540e\u4e00\u4e2a\u70b9(.)\u5fc5\u987b\u5728@\u540e\u9762\u81f3\u5c11\u4e00\u4e2a\u5b57\u7b26\u4ee5\u540e:<\/p>\n<table class=\"ex\" id=\"table1\" border=\"1\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td>\nfunction validate_email(field,alerttxt)<br \/>\n{<br \/>\nwith (field)<br \/>\n{<br \/>\napos=value.indexOf(&#8220;@&#8221;)<br \/>\ndotpos=value.lastIndexOf(&#8220;.&#8221;)<br \/>\nif (apos&lt;1||dotpos-apos&lt;2)<br \/>\n  {alert(alerttxt);return false}<br \/>\nelse {return true}<br \/>\n}<br \/>\n}\n<\/td>\n<\/tr>\n<\/table>\n<p>\u5b8c\u6574\u7684\u811a\u672c\u8fde\u540cHTML\u8868\u5355\u53ef\u4ee5\u50cf\u4e0b\u9762\u4f8b\u4e2d\u6240\u793a\uff1a<\/p>\n<table class=\"ex\" id=\"table2\" border=\"1\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td>\n&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\nfunction validate_email(field,alerttxt)<br \/>\n{<br \/>\nwith (field)<br \/>\n{<br \/>\napos=value.indexOf(&#8220;@&#8221;)<br \/>\ndotpos=value.lastIndexOf(&#8220;.&#8221;)<br \/>\nif (apos&lt;1||dotpos-apos&lt;2)<br \/>\n  {alert(alerttxt);return false}<br \/>\nelse {return true}<br \/>\n}<br \/>\n}<br \/>\nfunction validate_form(thisform)<br \/>\n{<br \/>\nwith (thisform)<br \/>\n{<br \/>\nif (validate_email(email,&#8221;\u8bf7\u8f93\u5165\u6709\u6548\u7684email\u5730\u5740!&#8221;)==false)<br \/>\n  {email.focus();return false}<br \/>\n}<br \/>\n}<br \/>\n&lt;\/script&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;form action=&#8221;submitpage.htm&#8221;<br \/>\nonsubmit=&#8221;return validate_form(this);&#8221;<br \/>\nmethod=&#8221;post&#8221;&gt;<br \/>\nEmail: &lt;input type=&#8221;text&#8221; name=&#8221;email&#8221; size=&#8221;30&#8243;&gt;<br \/>\n&lt;input type=&#8221;submit&#8221; value=&#8221;Submit&#8221;&gt;<br \/>\n&lt;\/form&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;\n<\/td>\n<\/tr>\n<\/table>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u53ef\u4ee5\u88ab\u7528\u6765\u5728\u5411\u670d\u52a1\u5668\u53d1\u9001\u5185\u5bb9\u524d\u9a8c\u8bc1HTML\u8868\u5355\u4e2d\u8f93\u5165\u7684\u4fe1\u606f\u662f\u5426\u7b26\u5408\u8981\u6c42\u3002 JavaScript \u8868\u5355\u9a8c\u8bc1 \u5728\u5411\u670d\u52a1\u5668\u53d1\u9001\u4fe1\u606f\u524d\uff0c\u53ef\u4ee5\u4f7f\u7528JavaScript\u6765\u9a8c\u8bc1\u4e00\u4e2aHTML \u8868\u5355(form)\u4e2d\u8f93\u5165\u7684\u4fe1\u606f\u662f\u5426\u5408\u6cd5\u3002\u901a\u5e38\u4f7f\u7528JavaScript\u9a8c\u8bc1\u7684\u8868\u5355\u6570\u636e\u53ef\u4ee5\u662f\uff1a \u5fc5\u586b\u6570\u636e\u662f\u5426\u4e3a\u7a7a? \u7528\u6237\u8f93\u5165\u7684e-mail\u5730\u5740\u662f\u5426\u5408\u6cd5? \u7528\u6237\u8f93\u5165\u7684\u65e5\u671f\u662f\u5426\u6709\u6548? \u4e00\u4e2a\u6570\u503c\u57df\u662f\u5426\u8f93\u5165\u4e86\u975e\u6570\u503c\u6587\u672c? \u68c0\u67e5\u5fc5\u586b\u6570\u636e \u4e0b\u9762\u7684\u51fd\u6570\u68c0\u67e5\u4e00\u4e2a\u5fc5\u586b\u6570\u636e\u57df\u662f\u5426\u4e3a\u7a7a\u3002\u5982\u679c\u4e3a\u7a7a\uff0c\u5219\u663e\u793a\u4e00\u4e2a\u8b66\u793a\u7a97\u53e3\u548c\u8b66\u793a\u4fe1\u606f\uff0c\u5e76\u8fd4\u56defalse\u503c\u3002\u5426\u5219\u76f4\u63a5\u8fd4\u56detrue (\u610f\u5473\u7740\u6570\u636e\u6b63\u786e): function validate_required(field,alerttxt) { with (field) { if (value==null||value==&#8221;&#8221;) {alert(alerttxt);return false} else {return true} } } \u5b8c\u6574\u7684\u811a\u672c\u8fde\u540cHTML\u8868\u5355\u53ef\u4ee5\u50cf\u4e0b\u9762\u4f8b\u4e2d\u6240\u793a\uff1a &lt;html&gt; &lt;head&gt; &lt;script type=&#8221;text\/javascript&#8221;&gt; function validate_required(field,alerttxt) { with (field) { if (value==null||value==&#8221;&#8221;) {alert(alerttxt);return false} else {return true} } } function validate_form(thisform) { with (thisform) { if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3,"menu_order":27,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/37"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=37"}],"version-history":[{"count":0,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/37\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/3"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}