cnblogs/dcrenl/js验证表单大全3.html
2024-09-24 12:43:01 +08:00

406 lines
13 KiB
HTML

<div id="sina_keyword_ad_area2" class="articalContent ">
<div><br>
</div>
<div>2 &gt;表单提交验证类&nbsp;<wbr></div>
<div><br>
</div>
<div><br>
</div>
<div>2.1 表单项不能为空</div>
<div><br>
</div>
<div>&lt;scriptlanguage=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div>function CheckForm()</div>
<div>{&nbsp;<wbr></div>
<div>if (document.form.name.value.length == 0){&nbsp;<wbr></div>
<div>alert(&quot;请输入您姓名!&quot;);</div>
<div>document.form.name.focus();</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div>--&gt;</div>
<div>&lt;/script&gt;</div>
<div><br>
</div>
<div>2.2 比较两个表单项的&#20540;是否相同</div>
<div><br>
</div>
<div>&lt;scriptlanguage=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div>function CheckForm()</div>
<div>if (document.form.PWD.value != document.form.PWD_Again.value){&nbsp;<wbr></div>
<div>alert(&quot;您两次输入的密码不一样!请重新输入.&quot;);</div>
<div>document.ADDUser.PWD.focus();</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div>--&gt;</div>
<div>&lt;/script&gt;</div>
<div><br>
</div>
<div>2.3 表单项只能为数字和&quot;_&quot;,用于电话/银行帐号验证上,可扩展到域名注册等</div>
<div><br>
</div>
<div>&lt;scriptlanguage=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div>function isNumber(String)</div>
<div>{&nbsp;<wbr></div>
<div>var Letters = &quot;1234567890-&quot;; //可以自己增加可输入&#20540;</div>
<div>var i;</div>
<div>var c;</div>
<div>if(String.charAt( 0 )=='-')</div>
<div>return false;</div>
<div>if( String.charAt( String.length - 1 ) == '-' )</div>
<div>return false;</div>
<div>for( i = 0; i &lt; String.length; i &#43;&#43; )</div>
<div>{&nbsp;<wbr></div>
<div>c = String.charAt( i );</div>
<div>if (Letters.indexOf( c ) &lt; 0)</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div>function CheckForm()</div>
<div>{&nbsp;<wbr></div>
<div>if(! isNumber(document.form.TEL.value)){&nbsp;<wbr></div>
<div>alert(&quot;您的电话号码不合法!&quot;);</div>
<div>document.form.TEL.focus();</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div>--&gt;</div>
<div>&lt;/script&gt;</div>
<div><br>
</div>
<div><br>
</div>
<div>2.4 表单项输入数&#20540;/长度限定</div>
<div><br>
</div>
<div>&lt;scriptlanguage=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div>function CheckForm()&nbsp;<wbr></div>
<div>{&nbsp;<wbr></div>
<div>if (document.form.count.value &gt; 100 ||document.form.count.value &lt; 1)</div>
<div>{&nbsp;<wbr></div>
<div>alert(&quot;输入数&#20540;不能小于零大于100!&quot;);</div>
<div>document.form.count.focus();</div>
<div>return false;</div>
<div>}</div>
<div>if(document.form.MESSAGE.value.length&lt;10)</div>
<div>{&nbsp;<wbr></div>
<div>alert(&quot;输入文字小于10!&quot;);</div>
<div>document.form.MESSAGE.focus();</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div>//--&gt;</div>
<div>&lt;/script&gt;</div>
<div><br>
</div>
<div>2.5 中文/英文/数字/邮件地址合法性判断</div>
<div><br>
</div>
<div>&lt;SCRIPTLANGUAGE=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div><br>
</div>
<div>function isEnglish(name) //英文&#20540;检测</div>
<div>{&nbsp;<wbr></div>
<div>if(name.length == 0)</div>
<div>return false;</div>
<div>for(i = 0; i &lt; name.length; i&#43;&#43;){&nbsp;<wbr></div>
<div>if(name.charCodeAt(i) &gt; 128)</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div><br>
</div>
<div>function isChinese(name) //中文&#20540;检测</div>
<div>{&nbsp;<wbr></div>
<div>if(name.length == 0)</div>
<div>return false;</div>
<div>for(i = 0; i &lt; name.length; i&#43;&#43;){&nbsp;<wbr></div>
<div>if(name.charCodeAt(i) &gt; 128)</div>
<div>return true;</div>
<div>}</div>
<div>return false;</div>
<div>}</div>
<div><br>
</div>
<div>function isMail(name) // E-mail&#20540;检测</div>
<div>{&nbsp;<wbr></div>
<div>if(! isEnglish(name))</div>
<div>return false;</div>
<div>i = name.indexOf(&quot; at &quot;);</div>
<div>j = name dot lastIndexOf(&quot; at &quot;);</div>
<div>if(i == -1)</div>
<div>return false;</div>
<div>if(i != j)</div>
<div>return false;</div>
<div>if(i == name dot length)</div>
<div>return false;</div>
<div>return true;</div>
<div>}</div>
<div><br>
</div>
<div>function isNumber(name) //数&#20540;检测</div>
<div>{&nbsp;<wbr></div>
<div>if(name.length == 0)</div>
<div>return false;</div>
<div>for(i = 0; i &lt; name.length; i&#43;&#43;){&nbsp;<wbr></div>
<div>if(name.charAt(i) &lt; &quot;0&quot; || name.charAt(i)&gt; &quot;9&quot;)</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div><br>
</div>
<div>function CheckForm()</div>
<div>{&nbsp;<wbr></div>
<div>if(! isMail(form.Email.value)) {&nbsp;<wbr></div>
<div>alert(&quot;您的电子邮件不合法!&quot;);</div>
<div>form.Email.focus();</div>
<div>return false;</div>
<div>}</div>
<div>if(! isEnglish(form.name.value)) {&nbsp;<wbr></div>
<div>alert(&quot;英文名不合法!&quot;);</div>
<div>form.name.focus();</div>
<div>return false;</div>
<div>}</div>
<div>if(! isChinese(form.cnname.value)) {&nbsp;<wbr></div>
<div>alert(&quot;中文名不合法!&quot;);</div>
<div>form.cnname.focus();</div>
<div>return false;</div>
<div>}</div>
<div>if(! isNumber(form.PublicZipCode.value)){&nbsp;<wbr></div>
<div>alert(&quot;邮政编码不合法!&quot;);</div>
<div>form.PublicZipCode.focus();</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div>//--&gt;</div>
<div>&lt;/SCRIPT&gt;</div>
<div><br>
</div>
<div>2.6 限定表单项不能输入的字符</div>
<div><br>
</div>
<div>&lt;scriptlanguage=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div><br>
</div>
<div>function contain(str,charset)// 字符串包含测试函数</div>
<div>{&nbsp;<wbr></div>
<div>var i;</div>
<div>for(i=0;i&lt;charset.length;i&#43;&#43;)</div>
<div>if(str.indexOf(charset.charAt(i))&gt;=0)</div>
<div>return true;</div>
<div>return false;</div>
<div>}</div>
<div><br>
</div>
<div>function CheckForm()</div>
<div>{&nbsp;<wbr></div>
<div>if ((contain(document.form.NAME.value,&quot;%\(\)&gt;&lt;&quot;)) ||(contain(document.form.MESSAGE.value,&quot;%\(\)&gt;&lt;&quot;)))</div>
<div>{&nbsp;<wbr></div>
<div>alert(&quot;输入了非法字符&quot;);</div>
<div>document.form.NAME.focus();</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div>//--&gt;</div>
<div>&lt;/script&gt;</div>
<div><br>
</div>
<div>.1 表单项不能为空</div>
<div><br>
</div>
<div>&lt;scriptlanguage=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div>function CheckForm()</div>
<div>{&nbsp;<wbr></div>
<div>if (document.form.name.value.length == 0){&nbsp;<wbr></div>
<div>alert(&quot;请输入您姓名!&quot;);</div>
<div>document.form.name.focus();</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div>--&gt;</div>
<div>&lt;/script&gt;</div>
<div>2.2 比较两个表单项的&#20540;是否相同</div>
<div>&lt;scriptlanguage=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div>function CheckForm()</div>
<div>if (document.form.PWD.value != document.form.PWD_Again.value){&nbsp;<wbr></div>
<div>alert(&quot;您两次输入的密码不一样!请重新输入.&quot;);</div>
<div>document.ADDUser.PWD.focus();</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div>--&gt;</div>
<div>&lt;/script&gt;</div>
<div>2.3 表单项只能为数字和&quot;_&quot;,用于电话/银行帐号验证上,可扩展到域名注册等</div>
<div>&lt;scriptlanguage=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div>function isNumber(String)</div>
<div>{&nbsp;<wbr></div>
<div>var Letters = &quot;1234567890-&quot;; //可以自己增加可输入&#20540;</div>
<div>var i;</div>
<div>var c;</div>
<div>if(String.charAt( 0 )=='-')</div>
<div>return false;</div>
<div>if( String.charAt( String.length - 1 ) == '-' )</div>
<div>return false;</div>
<div>for( i = 0; i &lt; String.length; i &#43;&#43; )</div>
<div>{&nbsp;<wbr></div>
<div>c = String.charAt( i );</div>
<div>if (Letters.indexOf( c ) &lt; 0)</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div>function CheckForm()</div>
<div>{&nbsp;<wbr></div>
<div>if(! isNumber(document.form.TEL.value)){&nbsp;<wbr></div>
<div>alert(&quot;您的电话号码不合法!&quot;);</div>
<div>document.form.TEL.focus();</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div>--&gt;</div>
<div>&lt;/script&gt;</div>
<div><br>
</div>
<div>2.4 表单项输入数&#20540;/长度限定</div>
<div>&lt;scriptlanguage=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div>function CheckForm()&nbsp;<wbr></div>
<div>{&nbsp;<wbr></div>
<div>if (document.form.count.value &gt; 100 ||document.form.count.value &lt; 1)</div>
<div>{&nbsp;<wbr></div>
<div>alert(&quot;输入数&#20540;不能小于零大于100!&quot;);</div>
<div>document.form.count.focus();</div>
<div>return false;</div>
<div>}</div>
<div>if(document.form.MESSAGE.value.length&lt;10)</div>
<div>{&nbsp;<wbr></div>
<div>alert(&quot;输入文字小于10!&quot;);</div>
<div>document.form.MESSAGE.focus();</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div>//--&gt;</div>
<div>&lt;/script&gt;</div>
<div>2.5 中文/英文/数字/邮件地址合法性判断</div>
<div>&lt;SCRIPTLANGUAGE=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div>function isEnglish(name) //英文&#20540;检测</div>
<div>{&nbsp;<wbr></div>
<div>if(name.length == 0)</div>
<div>return false;</div>
<div>for(i = 0; i &lt; name.length; i&#43;&#43;){&nbsp;<wbr></div>
<div>if(name.charCodeAt(i) &gt; 128)</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div>function isChinese(name) //中文&#20540;检测</div>
<div>{&nbsp;<wbr></div>
<div>if(name.length == 0)</div>
<div>return false;</div>
<div>for(i = 0; i &lt; name.length; i&#43;&#43;){&nbsp;<wbr></div>
<div>if(name.charCodeAt(i) &gt; 128)</div>
<div>return true;</div>
<div>}</div>
<div>return false;</div>
<div>}</div>
<div>function isMail(name) // E-mail&#20540;检测</div>
<div>{&nbsp;<wbr></div>
<div>if(! isEnglish(name))</div>
<div>return false;</div>
<div>i = name.indexOf(&quot; at &quot;);</div>
<div>j = name dot lastIndexOf(&quot; at &quot;);</div>
<div>if(i == -1)</div>
<div>return false;</div>
<div>if(i != j)</div>
<div>return false;</div>
<div>if(i == name dot length)</div>
<div>return false;</div>
<div>return true;</div>
<div>}</div>
<div>function isNumber(name) //数&#20540;检测</div>
<div>{&nbsp;<wbr></div>
<div>if(name.length == 0)</div>
<div>return false;</div>
<div>for(i = 0; i &lt; name.length; i&#43;&#43;){&nbsp;<wbr></div>
<div>if(name.charAt(i) &lt; &quot;0&quot; || name.charAt(i)&gt; &quot;9&quot;)</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div>function CheckForm()</div>
<div>{&nbsp;<wbr></div>
<div>if(! isMail(form.Email.value)) {&nbsp;<wbr></div>
<div>alert(&quot;您的电子邮件不合法!&quot;);</div>
<div>form.Email.focus();</div>
<div>return false;</div>
<div>}</div>
<div>if(! isEnglish(form.name.value)) {&nbsp;<wbr></div>
<div>alert(&quot;英文名不合法!&quot;);</div>
<div>form.name.focus();</div>
<div>return false;</div>
<div>}</div>
<div>if(! isChinese(form.cnname.value)) {&nbsp;<wbr></div>
<div>alert(&quot;中文名不合法!&quot;);</div>
<div>form.cnname.focus();</div>
<div>return false;</div>
<div>}</div>
<div>if(! isNumber(form.PublicZipCode.value)){&nbsp;<wbr></div>
<div>alert(&quot;邮政编码不合法!&quot;);</div>
<div>form.PublicZipCode.focus();</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div>//--&gt;</div>
<div>&lt;/SCRIPT&gt;</div>
<div>2.6 限定表单项不能输入的字符</div>
<div>&lt;scriptlanguage=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div>function contain(str,charset)// 字符串包含测试函数</div>
<div>{&nbsp;<wbr></div>
<div>var i;</div>
<div>for(i=0;i&lt;charset.length;i&#43;&#43;)</div>
<div>if(str.indexOf(charset.charAt(i))&gt;=0)</div>
<div>return true;</div>
<div>return false;</div>
<div>}</div>
<div>function CheckForm()</div>
<div>{&nbsp;<wbr></div>
<div>if ((contain(document.form.NAME.value,&quot;%\(\)&gt;&lt;&quot;)) ||(contain(document.form.MESSAGE.value,&quot;%\(\)&gt;&lt;&quot;)))</div>
<div>{&nbsp;<wbr></div>
<div>alert(&quot;输入了非法字符&quot;);</div>
<div>document.form.NAME.focus();</div>
<div>return false;</div>
<div>}</div>
<div>return true;</div>
<div>}</div>
<div>//--&gt;</div>
<div>&lt;/script&gt;</div>
</div>