鄙人最近搞的JS验证类(采用策略模式,极易扩展)

代码如下

// JavaScript Document var checkObj={/**数据验证类**/ checkFunc:{},//验证数据的函数对象集合 errorMessage:[],//错误消息 rightMessage:[],//正确消息 showInfoFunc:{},//显示消息的函数 checkConfig:{},//验证配置 check:function(data){//验证 var i, msg, checkType, checker, result_ok; for(i in data){ /*if(data[i].hasOwnProperty(i)){*/ checkType=this.checkConfig[i];//获得相应的验证规则函数名称 checker=this.checkFunc[checkType];//获得相应的验证函数 if(!checkType){; continue;//若验证规则不存在则不做任何处理跳出当前遍历进行下一步遍历 } if(!checker){ throw new Error('请指定'+checkType+'验证函数');//若验证函数不存在,抛出异常 } //}//hasOwnProperty result_ok=checker.validate(data[i]);//对单个进行验证 if(!result_ok){ //alert(data[i]+'错误'); this.errorMessage[i]=i+checker.instructions; if(this.showInfoFunc[i]){//如果定义的显示状态的函数,则调用该函数 //alert('有函数'); this.showInfoFunc[i](this.errorMessage[i]); } }else{ //alert(data[i]+'正确'); this.rightMessage[i]=i+'填写正确'; if(this.showInfoFunc[i]){//如果定义的显示状态的函数,则调用该函数 //alert(this.showInfoFunc[data[i]]); this.showInfoFunc[i](this.rightMessage[i]); } } }//for in },//check hasErrors:function(){//检查是否有错误 var _count=0; for(var i in this.errorMessage){ _count+=1; } return _count!==0; } } /*******检查是否为空*******/ checkObj.checkFunc.isEmpty={ validate:function(val){ return val!=''; }, instructions:'传入的值不能为空!'//错误消息 } /*******检查是否选择非默认值******/ checkObj.checkFunc.isDefault={ validate:function(val){ var _default='default'; return val!=_default; }, instructions:'请选择值!'//错误消息 } /*****检查电子邮件*****/ checkObj.checkFunc.isEmail={ validate:function(val){ var _reg=/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/; return !_reg.test(val); }, instructions:'请填写正确的格式!'//错误消息 } /**************调用示例****************/ /*var data={ fullName:$('.content').find('#fullNameTr').find('input').val(), tel:$('.content').find('#telTr').find('input').val(), email:$('.content').find('#emailTr').find('input').val(), address:$('.content').find('#addressTr').find('input').val(), send:$('.content').find('#sendTr').find('radio').val(), sendArea:$('.content').find('#countryTr').find('select[name=district]').val() }*/ var checkTest=function(){ //alert('test'); //alert(checkObj.checkFunc.isEmail.instructions); var data={ fullName:'曹某人', tel:'152', email:'914616@qq.com', address:'湖北武汉', send:'申通', sendArea:'武汉' } /*************配置***************/ checkObj.checkConfig={ fullName:'isEmpty', tel:'isEmpty', email:'isEmail', address:'isEmpty', send:'isEmpty', sendArea:'isDefault' } /*************t提示函数************/ checkObj.showInfoFunc={ tel:function(msg){ alert(msg); }, email:function(msg){ alert(msg); }, address:function(msg){ alert(msg); }, send:function(msg){ alert(msg); }, sendArea:function(msg){ alert(msg); } } /**************调用检查***************/ checkObj.check(data); /*************确认是否有错误*************/ //alert(checkObj.rightMessage.lengths); if(checkObj.hasErrors()){ alert('有错误'); return; } }


还有不足之处就是hasOwnProperty这个东西,一加上下面就checker就成undefined了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值