答复: 看看我的js表单验证程序,怎样使它更具重用性

关于客户端的验证,这里给出一个通用的,有兴趣的朋友可以研究一下: [code] 表单验证类 Validator v1.01 <style> body,td{font:normal 12px Verdana;color:#333333} input,textarea,select,td{font:normal 12px Verdana;color:#333333;border:1px solid #999999;background:#ffffff} table{border-collapse:collapse;} td{padding:3px} input{height:20;} textarea{width:80%;height:50px;overfmin:auto;} form{display:inline} </style>
真实姓名:
英文名:
主页:
密码:
重复:
信箱:
信箱:
QQ:
身份证:
年龄:
年龄1:
电话:
手机:
生日:
邮政编码:
邮政编码:
操作系统:选择您所用的操作系统Win98Win2kWinXP
所在省份:广东陕西浙江江西
爱好:运动上网听音乐看书
自我介绍:中文是一个字
自传:中文是两个字节t
<script> /************************************************* Validator v1.01 code by 我佛山人 wfsr@cunite.com http://www.cunite.com *************************************************/ Validator = { Require : /.+/, Email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, Phone : /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/, Mobile : /^((\(\d{3}\))|(\d{3}\-))?13\d{9}$/, Url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/, IdCard : /^\d{15}(\d{2}[A-Za-z0-9])?$/, Currency : /^\d+(\.\d+)?$/, Number : /^\d+$/, Zip : /^[1-9]\d{5}$/, QQ : /^[1-9]\d{4,8}$/, Integer : /^[-\+]?\d+$/, Double : /^[-\+]?\d+(\.\d+)?$/, English : /^[A-Za-z]+$/, Chinese : /^[\u0391-\uFFE5]+$/, UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/, IsSafe : function(str){return !this.UnSafe.test(str);}, SafeString : "this.IsSafe(value)", Limit : "this.limit(value.length,getAttribute('min'), getAttribute('max'))", LimitB : "this.limit(this.LenB(value), getAttribute('min'), getAttribute('max'))", Date : "this.IsDate(value, getAttribute('min'), getAttribute('format'))", Repeat : "value == document.getElementsByName(getAttribute('to'))[0].value", Range : "getAttribute('min') < value && value < getAttribute('max')", Compare : "this.compare(value,getAttribute('operator'),getAttribute('to'))", Custom : "this.Exec(value, getAttribute('regexp'))", Group : "this.MustChecked(getAttribute('name'), getAttribute('min'), getAttribute('max'))", ErrorItem : [document.forms[0]], ErrorMessage : ["以下原因导致提交失败:\t\t\t\t"], Validate : function(theForm, mode){ var obj = theForm || event.srcElement; var count = obj.elements.length; this.ErrorMessage.length = 1; this.ErrorItem.length = 1; this.ErrorItem[0] = obj; for(var i=0;i<count;i++){ with(obj.elements[i]){="" var="" _datatype="getAttribute(&quot;dataType&quot;);" if(typeof(_datatype)="=" "object"="" ||="" typeof(this[_datatype])="=" "undefined")="" continue;="" this.clearstate(obj.elements[i]);="" if(getattribute("require")="=" "false"="" &&="" value="=" "")="" continue;="" switch(_datatype){="" case="" "date"="" :="" case="" "repeat"="" :="" case="" "range"="" :="" case="" "compare"="" :="" case="" "custom"="" :="" case="" "group"="" :="" case="" "limit"="" :="" case="" "limitb"="" :="" case="" "safestring"="" :="" if(!eval(this[_datatype]))="" {="" this.adderror(i,="" getattribute("msg"));="" }="" break;="" default="" :="" if(!this[_datatype].test(value)){="" this.adderror(i,="" getattribute("msg"));="" }="" break;="" }="" }="" }="" if(this.errormessage.length=""> 1){ mode = mode || 1; var errCount = this.ErrorItem.length; switch(mode){ case 2 : for(var i=1;i<errcount;i++) this.erroritem[i].style.color="red" ;="" case="" 1="" :="" alert(this.errormessage.join("\n"));="" this.erroritem[1].focus();="" break;="" case="" 3="" :="" for(var="" i="1;i&lt;errCount;i++){" try{="" var="" span="document.createElement(&quot;SPAN&quot;);" span.id="__ErrorMessagePanel" ;="" span.style.color="red" ;="" this.erroritem[i].parentnode.appendchild(span);="" span.innerhtml="this.ErrorMessage[i].replace(/\d+:/,&quot;*&quot;);" }="" catch(e){alert(e.description);}="" }="" this.erroritem[1].focus();="" break;="" default="" :="" alert(this.errormessage.join("\n"));="" break;="" }="" return="" false;="" }="" return="" true;="" },="" limit="" :="" function(len,min,="" max){="" min="min" ||="" 0;="" max="max" ||="" number.max_value;="" return="" min="" <="len" &&="" len="" <="max;" },="" lenb="" :="" function(str){="" return="" str.replace(="" [^\x00-\xff]="" g,"**").length;="" },="" clearstate="" :="" function(elem){="" with(elem){="" if(style.color="=" "red")="" style.color="" ;="" var="" lastnode="parentNode.childNodes[parentNode.childNodes.length-1];" if(lastnode.id="=" "__errormessagepanel")="" parentnode.removechild(lastnode);="" }="" },="" adderror="" :="" function(index,="" str){="" this.erroritem[this.erroritem.length]="this.ErrorItem[0].elements[index];" this.errormessage[this.errormessage.length]="this.ErrorMessage.length" +="" ":"="" +="" str;="" },="" exec="" :="" function(op,="" reg){="" return="" new="" regexp(reg,"g").test(op);="" },="" compare="" :="" function(op1,operator,op2){="" switch="" (operator)="" {="" case="" "notequal":="" return="" (op1="" !="op2);" case="" "greaterthan":="" return="" (op1=""> op2); case "GreaterThanEqual": return (op1 >= op2); case "LessThan": return (op1 < op2); case "LessThanEqual": return (op1 <= op2); default: return (op1 == op2); } }, MustChecked : function(name, min, max){ var groups = document.getElementsByName(name); var hasChecked = 0; min = min || 1; max = max || groups.length; for(var i=groups.length-1;i>=0;i--) if(groups[i].checked) hasChecked++; return min <= hasChecked && hasChecked <= max; }, IsDate : function(op, formatString){ formatString = formatString || "ymd"; var m, year, month, day; switch(formatString){ case "ymd" : m = op.match(new RegExp("^((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})$")); if(m == null ) return false; day = m[6]; month = m[5]--; year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10)); break; case "dmy" : m = op.match(new RegExp("^(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))$")); if(m == null ) return false; day = m[1]; month = m[3]--; year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10)); break; default : break; } if(!parseInt(month)) return false; month = month==12 ?0:month; var date = new Date(year, month, day); return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()); function GetFullYear(y){return ((y<30 ? "20" : "19") + y)|0;} } } </script> [/code]
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页