js检查表单- -
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
:javascript 表单检查 自定义属性 正则表达式
部分代码参考了一位兄弟的代码,但没记住名字,十分抱歉,如果这位兄弟看到本文,请与我联系!!!
回回写表单,回回要写不同的检查JS,很麻烦,后来写了通用的检查函数,很粗糙,但比较实用,以后再好好改改:
包含页: CheckForm.js
代码如下:
//************************************************
//规则检查排序
function RegCheck(objs)
<wbr>{<br><wbr>var str = objs.checktype;<br><wbr>switch (str)<br><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr>case "cn" :<wbr><wbr><wbr>//要检查的表单控件的输入类型必须为中文<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return CnWordRegCheck(objs);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>break;<br><wbr><wbr><wbr><wbr><wbr><wbr>case "idnum" :<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return IdCardRegCheck(objs); //要检查的表单控件的输入类型必须为身份证号<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>break;<br><wbr><wbr><wbr>case "num" :<wbr><wbr><wbr>//要检查的表单控件的输入类型必须为数字<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return NumRegCheck(objs);<br><wbr><wbr><wbr><wbr>break;<br><wbr><wbr><wbr>case "mail" :<wbr><wbr><wbr>//要检查的表单控件的输入类型必须为EMAIL<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return EmailRegCheck(objs);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>break;<br><wbr><wbr><wbr>case "txt" :<wbr><wbr>//要检查的表单控件的输入类型必须为字符串<br><wbr><wbr><wbr><wbr>return SpecialWordRegCheck(objs);<br><wbr><wbr><wbr><wbr>break;<br><wbr><wbr><wbr>case "notes" :<br><wbr><wbr><wbr><wbr>return true; //要检查的表单控件的输入类型必须为什么都可以<br><wbr><wbr><wbr><wbr>break;<br><wbr><wbr>}<br><wbr>}<br> //************************************************<br> //检查电话号码<br> function NumRegCheck(obj)<br><wbr>{<br><wbr>var uplimit = obj.checkrule.split(",")[0];<br><wbr>var downlimit = obj.checkrule.split(",")[1];<br><wbr>var reg = "";<br><wbr>if (downlimit == null)<br><wbr><wbr>{<br><wbr><wbr><wbr><wbr>reg = eval_r("/^[0-9]{"+uplimit+"}$/");<br><wbr><wbr>}<br><wbr>else<br><wbr><wbr>{<br><wbr><wbr><wbr><wbr>reg = eval_r("/^[0-9]{"+uplimit+","+downlimit+"}$/");<br><wbr><wbr>}<br><wbr>var str = obj.value;<br><wbr>var flag = reg.test(str);<br><wbr>return flag;<br><wbr>}<br> //************************************************<br> //检查身份证号<br> function IdCardRegCheck(obj)<br><wbr>{<br><wbr>var str = obj.value;<br><wbr>var reg = /^([0-9]{15}|[0-9]{18})$/;<br><wbr>var flag = reg.test(str);<br><wbr>return flag;<br><wbr>}<br> //************************************************<br> //检查EMAIL<br> function EmailRegCheck(obj)<br><wbr>{<br><wbr>var str = obj.value;<br><wbr>var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;<br><wbr>var flag = reg.test(str);<br><wbr>return flag;<br><wbr>}<br> //***************************************<br> //检查中文输入<br> function CnWordRegCheck(obj)<br><wbr>{<br><wbr>var str = obj.value;<br><wbr>var reg=/^[\u4e00-\u9fa5](\s*[\u4e00-\u9fa5])*$/;<br><wbr>var flag = reg.test(str);<br><wbr>//alert(flag);<br><wbr>return flag;<br><wbr>}<br> //***************************************<br> //检查特殊字符<br> function SpecialWordRegCheck(obj)<br><wbr>{<br><wbr>var reg= /[(\/)(<a href="file://%29%28%27%29%28/" style="text-decoration:none"><)(>)]/g">\\)(')(")(<)(>)]/g</a>;<br><wbr>var str = obj.value;<br><wbr>var flag= reg.test(str);<br><wbr>flag = !flag;<br><wbr>return flag;<br><wbr>}<br> //************************************************<br> //检查主引导函数<br> function CheckForm(obj)<br><wbr>{<br><wbr>var myform = eval_r("document."+obj.name);<br><wbr><br><wbr>for (i=0;i<myform.elements.length;i++)<br><wbr><wbr>{<br><wbr><wbr><wbr>var formvalue = myform.elements[i].value;<br><wbr><wbr><wbr>//内容非空检查,长度检查<br><wbr><wbr><wbr>if ((myform.elements[i].value == "")||(myform.elements[i].value.length>myform.elements[i].maxlength))<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr>alert("您忘了填写"+myform.elements[i].cnname+"!"+"或者您填写的信息不符合规范!");<br><wbr><wbr><wbr><wbr><wbr>myform.elements[i].focus();<br><wbr><wbr><wbr><wbr><wbr>return false;<br><wbr><wbr><wbr><wbr><wbr>break;<br><wbr><wbr><wbr><wbr>}<br><wbr><br><wbr><wbr><wbr><wbr>if (myform.elements[i].value == 0)<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr>alert("您忘了选择"+myform.elements[i].cnname+"!");<br><wbr><wbr><wbr><wbr><wbr>myform.elements[i].focus();<br><wbr><wbr><wbr><wbr><wbr>return false;<br><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>