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

406 lines
24 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div id="sina_keyword_ad_area2" class="articalContent ">
<div>屏蔽右键 很酷&nbsp;<wbr></div>
<div>oncontextmenu=&quot;return false&quot; ondragstart=&quot;return false&quot;onselectstart=&quot;return false&quot;</div>
<div>加在body中</div>
<div><br>
</div>
<div><br>
</div>
<div><br>
</div>
<div>2.1 &nbsp;<wbr> &nbsp;<wbr> 表单项不能为空</div>
<div><br>
</div>
<div>&lt;script &nbsp;<wbr> &nbsp;<wbr>language=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div>function &nbsp;<wbr> &nbsp;<wbr>CheckForm()</div>
<div>{ &nbsp;<wbr></div>
<div>if &nbsp;<wbr> &nbsp;<wbr>(document.form.name.value.length &nbsp;<wbr>&nbsp;<wbr> == &nbsp;<wbr> &nbsp;<wbr> 0)&nbsp;<wbr> &nbsp;<wbr> { &nbsp;<wbr></div>
<div>alert(&quot;请输入您姓名!&quot;);</div>
<div>document.form.name.focus();</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>}</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> true;</div>
<div>}</div>
<div>--&gt;</div>
<div>&lt;/script&gt;</div>
<div><br>
</div>
<div>2.2 &nbsp;<wbr> &nbsp;<wbr> 比较两个表单项的&#20540;是否相同</div>
<div><br>
</div>
<div>&lt;script &nbsp;<wbr> &nbsp;<wbr>language=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div>function &nbsp;<wbr> &nbsp;<wbr>CheckForm()</div>
<div>if &nbsp;<wbr> &nbsp;<wbr>(document.form.PWD.value &nbsp;<wbr> &nbsp;<wbr> !=&nbsp;<wbr> &nbsp;<wbr>document.form.PWD_Again.value) &nbsp;<wbr>&nbsp;<wbr> { &nbsp;<wbr></div>
<div>alert(&quot;您两次输入的密码不一样!请重新输入.&quot;);</div>
<div>document.ADDUser.PWD.focus();</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>}</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> true;</div>
<div>}</div>
<div>--&gt;</div>
<div>&lt;/script&gt;</div>
<div><br>
</div>
<div>2.3 &nbsp;<wbr> &nbsp;<wbr>表单项只能为数字和&quot;_&quot;,用于电话/银行帐号验证上,可扩展到域名注册等</div>
<div><br>
</div>
<div>&lt;script &nbsp;<wbr> &nbsp;<wbr>language=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div>function &nbsp;<wbr> &nbsp;<wbr>isNumber(String)</div>
<div>{ &nbsp;<wbr></div>
<div>var &nbsp;<wbr> &nbsp;<wbr> Letters&nbsp;<wbr> &nbsp;<wbr> = &nbsp;<wbr>&nbsp;<wbr> &quot;1234567890-&quot;; &nbsp;<wbr>&nbsp;<wbr> //可以自己增加可输入&#20540;</div>
<div>var &nbsp;<wbr> &nbsp;<wbr> i;</div>
<div>var &nbsp;<wbr> &nbsp;<wbr> c;</div>
<div>if(String.charAt( &nbsp;<wbr> &nbsp;<wbr> 0&nbsp;<wbr> &nbsp;<wbr> )=='-')</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>if( &nbsp;<wbr> &nbsp;<wbr> String.charAt(&nbsp;<wbr> &nbsp;<wbr> String.length&nbsp;<wbr> &nbsp;<wbr> - &nbsp;<wbr>&nbsp;<wbr> 1 &nbsp;<wbr> &nbsp;<wbr> )&nbsp;<wbr> &nbsp;<wbr> == &nbsp;<wbr>&nbsp;<wbr> '-' &nbsp;<wbr> &nbsp;<wbr>)</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>for( &nbsp;<wbr> &nbsp;<wbr> i&nbsp;<wbr> &nbsp;<wbr> = &nbsp;<wbr>&nbsp;<wbr> 0; &nbsp;<wbr> &nbsp;<wbr> i&nbsp;<wbr> &nbsp;<wbr> &lt;&nbsp;<wbr> &nbsp;<wbr> String.length;&nbsp;<wbr> &nbsp;<wbr> i &nbsp;<wbr>&nbsp;<wbr> &#43;&#43; &nbsp;<wbr> &nbsp;<wbr> )</div>
<div>{ &nbsp;<wbr></div>
<div>c &nbsp;<wbr> &nbsp;<wbr> = &nbsp;<wbr>&nbsp;<wbr> String.charAt( &nbsp;<wbr>&nbsp;<wbr> i &nbsp;<wbr> &nbsp;<wbr> );</div>
<div>if &nbsp;<wbr> &nbsp;<wbr> (Letters.indexOf(&nbsp;<wbr> &nbsp;<wbr> c &nbsp;<wbr>&nbsp;<wbr> ) &nbsp;<wbr> &nbsp;<wbr>&lt; &nbsp;<wbr> &nbsp;<wbr> 0)</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>}</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> true;</div>
<div>}</div>
<div>function &nbsp;<wbr> &nbsp;<wbr>CheckForm()</div>
<div>{ &nbsp;<wbr></div>
<div>if(! &nbsp;<wbr> &nbsp;<wbr>isNumber(document.form.TEL.value)) &nbsp;<wbr>&nbsp;<wbr> { &nbsp;<wbr></div>
<div>alert(&quot;您的电话号码不合法!&quot;);</div>
<div>document.form.TEL.focus();</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>}</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> true;</div>
<div>}</div>
<div>--&gt;</div>
<div>&lt;/script&gt;</div>
<div><br>
</div>
<div><br>
</div>
<div>2.4 &nbsp;<wbr> &nbsp;<wbr> 表单项输入数&#20540;/长度限定</div>
<div><br>
</div>
<div>&lt;script &nbsp;<wbr> &nbsp;<wbr>language=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div>function &nbsp;<wbr> &nbsp;<wbr> CheckForm()&nbsp;<wbr></div>
<div>{ &nbsp;<wbr></div>
<div>if &nbsp;<wbr> &nbsp;<wbr>(document.form.count.value &nbsp;<wbr> &nbsp;<wbr>&gt; &nbsp;<wbr> &nbsp;<wbr> 100&nbsp;<wbr> &nbsp;<wbr> || &nbsp;<wbr>&nbsp;<wbr> document.form.count.value &nbsp;<wbr>&nbsp;<wbr> &lt; &nbsp;<wbr>&nbsp;<wbr> 1)</div>
<div>{ &nbsp;<wbr></div>
<div>alert(&quot;输入数&#20540;不能小于零大于100!&quot;);</div>
<div>document.form.count.focus();</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>}</div>
<div>if &nbsp;<wbr> &nbsp;<wbr>(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 &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>}</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> true;</div>
<div>}</div>
<div>//--&gt;</div>
<div>&lt;/script&gt;</div>
<div><br>
</div>
<div>2.5 &nbsp;<wbr> &nbsp;<wbr>中文/英文/数字/邮件地址合法性判断</div>
<div><br>
</div>
<div>&lt;SCRIPT &nbsp;<wbr> &nbsp;<wbr>LANGUAGE=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div><br>
</div>
<div>function &nbsp;<wbr> &nbsp;<wbr> isEnglish(name)&nbsp;<wbr> &nbsp;<wbr> //英文&#20540;检测</div>
<div>{ &nbsp;<wbr></div>
<div>if(name.length &nbsp;<wbr> &nbsp;<wbr> ==&nbsp;<wbr> &nbsp;<wbr> 0)</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>for(i &nbsp;<wbr> &nbsp;<wbr> =&nbsp;<wbr> &nbsp;<wbr> 0; &nbsp;<wbr>&nbsp;<wbr> i &nbsp;<wbr> &nbsp;<wbr>&lt; &nbsp;<wbr> &nbsp;<wbr> name.length;&nbsp;<wbr> &nbsp;<wbr> i&#43;&#43;) &nbsp;<wbr>&nbsp;<wbr> { &nbsp;<wbr></div>
<div>if(name.charCodeAt(i) &nbsp;<wbr> &nbsp;<wbr>&gt; &nbsp;<wbr> &nbsp;<wbr> 128)</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>}</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> true;</div>
<div>}</div>
<div><br>
</div>
<div>function &nbsp;<wbr> &nbsp;<wbr> isChinese(name)&nbsp;<wbr> &nbsp;<wbr> //中文&#20540;检测</div>
<div>{ &nbsp;<wbr></div>
<div>if(name.length &nbsp;<wbr> &nbsp;<wbr> ==&nbsp;<wbr> &nbsp;<wbr> 0)</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>for(i &nbsp;<wbr> &nbsp;<wbr> =&nbsp;<wbr> &nbsp;<wbr> 0; &nbsp;<wbr>&nbsp;<wbr> i &nbsp;<wbr> &nbsp;<wbr>&lt; &nbsp;<wbr> &nbsp;<wbr> name.length;&nbsp;<wbr> &nbsp;<wbr> i&#43;&#43;) &nbsp;<wbr>&nbsp;<wbr> { &nbsp;<wbr></div>
<div>if(name.charCodeAt(i) &nbsp;<wbr> &nbsp;<wbr>&gt; &nbsp;<wbr> &nbsp;<wbr> 128)</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> true;</div>
<div>}</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>}</div>
<div><br>
</div>
<div>function &nbsp;<wbr> &nbsp;<wbr> isMail(name)&nbsp;<wbr> &nbsp;<wbr> // &nbsp;<wbr>&nbsp;<wbr> E-mail&#20540;检测</div>
<div>{ &nbsp;<wbr></div>
<div>if(! &nbsp;<wbr> &nbsp;<wbr>isEnglish(name))</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>i &nbsp;<wbr> &nbsp;<wbr> = &nbsp;<wbr>&nbsp;<wbr> name.indexOf(&quot; &nbsp;<wbr>&nbsp;<wbr> at &nbsp;<wbr> &nbsp;<wbr>&quot;);</div>
<div>j &nbsp;<wbr> &nbsp;<wbr> = &nbsp;<wbr>&nbsp;<wbr> name &nbsp;<wbr> &nbsp;<wbr> dot&nbsp;<wbr> &nbsp;<wbr> lastIndexOf(&quot;&nbsp;<wbr> &nbsp;<wbr> at &nbsp;<wbr>&nbsp;<wbr> &quot;);</div>
<div>if(i &nbsp;<wbr> &nbsp;<wbr> ==&nbsp;<wbr> &nbsp;<wbr> -1)</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>if(i &nbsp;<wbr> &nbsp;<wbr> !=&nbsp;<wbr> &nbsp;<wbr> j)</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>if(i &nbsp;<wbr> &nbsp;<wbr> ==&nbsp;<wbr> &nbsp;<wbr> name &nbsp;<wbr>&nbsp;<wbr> dot &nbsp;<wbr> &nbsp;<wbr>length)</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> true;</div>
<div>}</div>
<div><br>
</div>
<div>function &nbsp;<wbr> &nbsp;<wbr> isNumber(name)&nbsp;<wbr> &nbsp;<wbr> //数&#20540;检测</div>
<div>{ &nbsp;<wbr></div>
<div>if(name.length &nbsp;<wbr> &nbsp;<wbr> ==&nbsp;<wbr> &nbsp;<wbr> 0)</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>for(i &nbsp;<wbr> &nbsp;<wbr> =&nbsp;<wbr> &nbsp;<wbr> 0; &nbsp;<wbr>&nbsp;<wbr> i &nbsp;<wbr> &nbsp;<wbr>&lt; &nbsp;<wbr> &nbsp;<wbr> name.length;&nbsp;<wbr> &nbsp;<wbr> i&#43;&#43;) &nbsp;<wbr>&nbsp;<wbr> { &nbsp;<wbr></div>
<div>if(name.charAt(i) &nbsp;<wbr> &nbsp;<wbr>&lt; &nbsp;<wbr> &nbsp;<wbr> &quot;0&quot;&nbsp;<wbr> &nbsp;<wbr> || &nbsp;<wbr>&nbsp;<wbr> name.charAt(i) &nbsp;<wbr>&nbsp;<wbr> &gt; &nbsp;<wbr>&nbsp;<wbr> &quot;9&quot;)</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>}</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> true;</div>
<div>}</div>
<div><br>
</div>
<div>function &nbsp;<wbr> &nbsp;<wbr>CheckForm()</div>
<div>{ &nbsp;<wbr></div>
<div>if(! &nbsp;<wbr> &nbsp;<wbr>isMail(form.Email.value)) &nbsp;<wbr> &nbsp;<wbr> {&nbsp;<wbr></div>
<div>alert(&quot;您的电子邮件不合法!&quot;);</div>
<div>form.Email.focus();</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>}</div>
<div>if(! &nbsp;<wbr> &nbsp;<wbr>isEnglish(form.name.value)) &nbsp;<wbr> &nbsp;<wbr> {&nbsp;<wbr></div>
<div>alert(&quot;英文名不合法!&quot;);</div>
<div>form.name.focus();</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>}</div>
<div>if(! &nbsp;<wbr> &nbsp;<wbr>isChinese(form.cnname.value)) &nbsp;<wbr> &nbsp;<wbr>{ &nbsp;<wbr></div>
<div>alert(&quot;中文名不合法!&quot;);</div>
<div>form.cnname.focus();</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>}</div>
<div>if(! &nbsp;<wbr> &nbsp;<wbr>isNumber(form.PublicZipCode.value)) &nbsp;<wbr>&nbsp;<wbr> { &nbsp;<wbr></div>
<div>alert(&quot;邮政编码不合法!&quot;);</div>
<div>form.PublicZipCode.focus();</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>}</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> true;</div>
<div>}</div>
<div>//--&gt;</div>
<div>&lt;/SCRIPT&gt;</div>
<div><br>
</div>
<div>2.6 &nbsp;<wbr> &nbsp;<wbr> 限定表单项不能输入的字符</div>
<div><br>
</div>
<div>&lt;script &nbsp;<wbr> &nbsp;<wbr>language=&quot;javascript&quot;&gt;</div>
<div>&lt;!--</div>
<div><br>
</div>
<div>function &nbsp;<wbr> &nbsp;<wbr>contain(str,charset)// &nbsp;<wbr> &nbsp;<wbr>字符串包含测试函数</div>
<div>{ &nbsp;<wbr></div>
<div>var &nbsp;<wbr> &nbsp;<wbr> 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 &nbsp;<wbr> &nbsp;<wbr> true;</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>}</div>
<div><br>
</div>
<div>function &nbsp;<wbr> &nbsp;<wbr>CheckForm()</div>
<div>{ &nbsp;<wbr></div>
<div>if &nbsp;<wbr> &nbsp;<wbr>((contain(document.form.NAME.value, &nbsp;<wbr>&nbsp;<wbr> &quot;%\(\)&gt;&lt;&quot;))&nbsp;<wbr> &nbsp;<wbr> || &nbsp;<wbr>&nbsp;<wbr> (contain(document.form.MESSAGE.value,&nbsp;<wbr> &nbsp;<wbr>&quot;%\(\)&gt;&lt;&quot;)))</div>
<div>{ &nbsp;<wbr></div>
<div>alert(&quot;输入了非法字符&quot;);</div>
<div>document.form.NAME.focus();</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> false;</div>
<div>}</div>
<div>return &nbsp;<wbr> &nbsp;<wbr> true;</div>
<div>}</div>
<div>//--&gt;</div>
<div>&lt;/script&gt;&nbsp;<wbr></div>
<div><br>
</div>
<div>1. 检查一段字符串是否全由数字组成 &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>--------------------------------------- &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr></div>
<div>&lt;scriptlanguage=&quot;Javascript&quot;&gt;&lt;!--&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr></div>
<div>function checkNum(str){return str.match(/\D/)==null}&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr></div>
<div>alert(checkNum(&quot;1232142141&quot;)) &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>alert(checkNum(&quot;123214214a1&quot;)) &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>// --&gt;&lt;/script&gt;&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr></div>
<div>2. 怎么判断是否是字符 &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr></div>
<div>--------------------------------------- &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr></div>
<div>if (/[^\x00-\xff]/g.test(s)) alert(&quot;含有汉字&quot;); &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr></div>
<div>else alert(&quot;全是字符&quot;); &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>3. 怎么判断是否含有汉字 &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr></div>
<div>--------------------------------------- &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr></div>
<div>if (escape(str).indexOf(&quot;%u&quot;)!=-1) alert(&quot;含有汉字&quot;);&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>else alert(&quot;全是字符&quot;); &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr></div>
<div>4. 邮箱&#26684;式验证 &nbsp;<wbr> &nbsp;<wbr></div>
<div>--------------------------------------- &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr></div>
<div>//函数名chkemail &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>//功能介绍检查是否为Email Address &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>//参数说明:要检查的字符串 &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>//返回&#20540;0不是 1&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>function chkemail(a) &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>{ var i=a.length; &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>var temp = a.indexOf('@'); &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>var tempd = a.indexOf('.'); &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>if (temp &gt; 1) { &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>if ((i-temp) &gt; 3){ &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>if ((i-tempd)&gt;0){ &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>return 1; &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>} &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr></div>
<div>} &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>} &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>return 0; &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>} &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr></div>
<div>5. 数字&#26684;式验证 &nbsp;<wbr> &nbsp;<wbr></div>
<div>--------------------------------------- &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr></div>
<div>//函数名fucCheckNUM &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>//功能介绍:检查是否为数字 &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>//参数说明:要检查的数字 &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>//返回&#20540;1为是数字0为不是数字 &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>function fucCheckNUM(NUM) &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>{ &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>var i,j,strTemp; &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>strTemp=&quot;0123456789&quot;; &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>if ( NUM.length== 0) &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>return 0 &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>for (i=0;i&lt;NUM.length;i&#43;&#43;) &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>{ &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>j=strTemp.indexOf(NUM.charAt(i)); &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>if (j==-1) &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>{ &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>//说明有字符不是数字 &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>return 0; &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>} &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>} &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>//说明是数字 &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>return 1; &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>} &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr></div>
<div>6. 电话号码&#26684;式验证 &nbsp;<wbr> &nbsp;<wbr></div>
<div>--------------------------------------- &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr></div>
<div>//函数名fucCheckTEL &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>//功能介绍:检查是否为电话号码 &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>//参数说明:要检查的字符串 &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>//返回&#20540;1为是合法0为不合法 &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>function fucCheckTEL(TEL) &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>{ &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>var i,j,strTemp; &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>strTemp=&quot;0123456789-()# &quot;; &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>for (i=0;i&lt;TEL.length;i&#43;&#43;) &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>{ &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>j=strTemp.indexOf(TEL.charAt(i)); &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>if (j==-1) &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>{ &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>//说明有字符不合法 &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>return 0; &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>} &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>} &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>//说明合法 &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>return 1; &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>} &nbsp;<wbr> &nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>7. 判断输入是否为中文的函数 &nbsp;<wbr>&nbsp;<wbr></div>
<div>--------------------------------------- &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr></div>
<div>function ischinese(s){&nbsp;<wbr>&nbsp;<wbr></div>
<div>var ret=true; &nbsp;<wbr>&nbsp;<wbr></div>
<div>for(var i=0;i&lt;s.length;i&#43;&#43;)&nbsp;<wbr>&nbsp;<wbr></div>
<div>ret=ret &amp;&amp;(s.charCodeAt(i)&gt;=10000);&nbsp;<wbr>&nbsp;<wbr></div>
<div>return ret; &nbsp;<wbr>&nbsp;<wbr></div>
<div>} &nbsp;<wbr> &nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>8. 综合的判断用户输入的合法性的函数 &nbsp;<wbr></div>
<div>--------------------------------------- &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr></div>
<div>&lt;script language=&quot;javascript&quot;&gt;&nbsp;<wbr></div>
<div>//限制输入字符的位数开始 &nbsp;<wbr></div>
<div>//m是用户输入n是要限制的位数 &nbsp;<wbr></div>
<div>function issmall(m,n) &nbsp;<wbr></div>
<div>{ &nbsp;<wbr></div>
<div>if ((m&lt;n) &amp;&amp;(m&gt;0)) &nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> {&nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>return(false); &nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> }&nbsp;<wbr></div>
<div>else &nbsp;<wbr></div>
<div>{return(true);} &nbsp;<wbr></div>
<div>} &nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr></div>
<div>9. 判断密码是否输入一致 &nbsp;<wbr></div>
<div>--------------------------------------- &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr></div>
<div>function issame(str1,str2) &nbsp;<wbr></div>
<div>{ &nbsp;<wbr></div>
<div>if (str1==str2) &nbsp;<wbr></div>
<div>{return(true);} &nbsp;<wbr></div>
<div>else &nbsp;<wbr></div>
<div>{return(false);} &nbsp;<wbr></div>
<div>} &nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr></div>
<div>10. 判断用户名是否为数字字母下滑线&nbsp;<wbr></div>
<div>--------------------------------------- &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr></div>
<div>function notchinese(str){&nbsp;<wbr></div>
<div>var reg=/[^A-Za-z0-9_]/g&nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> if (reg.test(str)){&nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> return (false);&nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> }else{&nbsp;<wbr></div>
<div>return(true); &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> }&nbsp;<wbr></div>
<div>}&nbsp;<wbr></div>
<div><br>
</div>
<div>11. form文本域的通用校验函数</div>
<div>--------------------------------------- &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr></div>
<div>作用检测所有必须非空的input文本比如姓名账号邮件地址等等。</div>
<div>该校验现在只针对文本域如果要针对form里面的其他域对象可以改变判断条件。</div>
<div><br>
</div>
<div>使用方法在要检测的文本域中加入title文字。文字是在提示信息你要提示给用户的该字段的中文名。比如要检测用户名</div>
<div>html如下&lt;input name=&quot;txt_1&quot;title=&quot;姓名&quot;&gt;,当然最好用可视化工具比如dreamweaver什么的来编辑域。</div>
<div>如果要检测数字类型数据的话再把域的id统一为sz.</div>
<div>javascript判断日期类型比较麻烦所以就没有做日期类型校验的程序了.高手可以补充。</div>
<div><br>
</div>
<div>程序比较草,只是提供一个思路。抛砖引玉! </div>
<div>哦,对了,函数调用方法:&lt; form &nbsp;<wbr>&nbsp;<wbr> onsubmit=&quot;returndovalidate()&quot;&gt;</div>
<div><br>
</div>
<div>function dovalidate()</div>
<div>{</div>
<div>fm=document.forms[0] //只检测一个form,如果是多个可以改变判断条件</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> for(i=0;i&lt;fm.length;i&#43;&#43;)</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> { &nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> //检测判断条件,根据类型不同可以修改</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> if(fm.tagName.toUpperCase()==&quot;INPUT&quot;&amp;&amp;fm.type.toUpperCase()==&quot;TEXT&quot;&amp;&amp; (fm.title!=&quot;&quot;))</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>if(fm.value=&quot;/blog/=&quot;&quot;)//</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>{</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>str_warn1=fm.title&#43;&quot;不能为空!&quot;;</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>alert(str_warn1);</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>fm.focus();</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>return false; &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>}</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>if(fm.id.toUpperCase()==&quot;SZ&quot;)//数字校验</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr>{</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>if(isNaN(fm.value))</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> { str_warn2=fm.title&#43;&quot;&#26684;式不对&quot;;</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> alert(str_warn2);</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> fm.focus();</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>return false;</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>}</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> }</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> }</div>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>&nbsp;<wbr> return true;</div>
<div>}</div>
<div><br>
</div>
</div>