jquery validate addMethod 自定义验证方法
jqueryvalidateaddMethod
//验证邮政编码 jQuery.validator.addMethod( "checkPost",function(value,element){ var pattern =/^[0-9]{6}$/; if(value !=''){if(!pattern.exec(value)){return false;}}; return true; } , "请输入有效的邮政编码"); //添加转换小写功能 jQuery.validator.addMethod("toLowerCase", function(value, element) { value = $.trim(String(value));//去空 if(value==""){element.value = ""; return this.optional(element)|| true;} if(value.toLowerCase()!=value) element.value = value.toLowerCase(); return this.optional(element)|| true; }, ""); //验证手机 jQuery.validator.addMethod( "checkMobile",function(value,element){ var reg0 = /^13\d{5,9}$/; var reg1 = /^15\d{5,9}$/; var reg2 = /^189\d{4,8}$/; var reg3 = /^0\d{10,11}$/; var my = false; if (reg0.test(value))my=true; if (reg1.test(value))my=true; if (reg2.test(value))my=true; if (reg3.test(value))my=true; if(value!=''){if(!my){return false;}}; return true; } , " 请输入有效的手机号码); //验证密码 6-18位由字符数字和特殊符号组成 排除空格.. jQuery.validator.addMethod("checkPassword",function(value, element) { var myreg = /^[^\s]{6,18}$/; if (value != '') {if (!myreg.test(value)) {return false;}}; return true;}, "请输入有效密码!"); //验证邮箱 jQuery.validator.addMethod( "checkEmail",function(value,element){ var myreg = /^[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]*)*@[a-zA-Z0-9\-]+([\.][a-zA-Z0-9\-]+)+$/; if(value !=''){if(!myreg.test(value)){return false;}}; return true; } , " 请输入有效的E_mail"); //验证固定电话 jQuery.validator.addMethod( "checkTel",function(value,element){ var pattern =/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/; if(value!=''){if(!pattern.exec(value)){return false;}}; return true; } , "请输入有效的固定电话"); //验证名称是否重复 jQuery.validator.addMethod( "checkName",function(value,element){ var returnMsg=true; jQuery.ajax({type:"get",url:"url", async:false,cache:false,data:{ toinName:value,method:"get"},dataType:"html",scriptCharset:"UTF-8",success:function(msg){ if(msg=="1"){ returnMsg=false; } }}); return returnMsg; } , "此名称已经被占用!请您更换其它名称"); $("#addForm").validate({ errorPlacement:function (error, element){ if(element.attr("type")=="checkbox"||element.attr("type")=="radio"){ error.appendTo(element.parent()); }else{ error.appendTo(element.parent()); } }, rules: { toinCompanyName:{ required: true, checkCompanyName:true },usinName:{ required: true },usinSex:{ required: true },usinZw:{ required: true },toinCatalogId:{ required: true },usinCounty:{ required: true },usinQicq:{ number: true },toinCompanyIntroduction:{ maxlength:400 },usinEmail:{ checkEmail:true , toLowerCase:true },usinTel:{ checkTel : true },usinMobile:{ checkMobile : true },usinPost :{ checkPost : true } }, messages: { .... } }); 获取html加入 class <input id="D_NUMBER" name="D_NUMBER" style="width:100px;" class="checkPost" type="text"/>
相关推荐
tztzyzyz 2020-07-20
EdwardSiCong 2020-11-23
85477104 2020-11-17
hhanbj 2020-11-17
81427005 2020-11-11
seoppt 2020-09-13
honeyth 2020-09-13
WRITEFORSHARE 2020-09-13
84483065 2020-09-11
momode 2020-09-11
85477104 2020-08-15
83510998 2020-08-08
82550495 2020-08-03
tthappyer 2020-08-03
84901334 2020-07-28
tthappyer 2020-07-25
TONIYH 2020-07-22
83510998 2020-07-18
81463166 2020-07-17