jquery validate 动态增加删除验证规则

页面加载完成初始化form validate 

[javascript] view plain copy
print ?
  1. $("#user_regForm").validate({  
  2.             errorPlacement: function(error, element){  
  3.                 if(element.attr("id")=="province"||element.attr("id")=="city"||element.attr("id")=="area"){  
  4.                     var error_td = element.parent().parent('dd').next();  
  5.                 }else{  
  6.                     var error_td = element.parent('dd').next();  
  7.                 }  
  8.                 error_td.html("");  
  9.                 error_td.append(error);  
  10.                 element.addClass("user_regNok");  
  11.             },  
  12.             error:function(label,element){  
  13.                 element.addClass("user_regNok");  
  14.             },  
  15.             success       : function(label,element){  
  16.                 label.addClass('reg_validate_right').text('');  
  17.                 element.removeClass("user_regNok");  
  18.             },  
  19.             submitHandler:function(form){  
  20.                 console.info("submit:"+$(form).serializeArray());  
  21.                 form.submit();    
  22.             },  
  23.             onkeyup: false,  
  24.             rules : {  
  25.                 username:{  
  26.                     required:true,  
  27.                     alipay:true,  
  28.                     remote   : {  
  29.                         url :'index.php?mod=ajax&act=check_user',  
  30.                         type:'post',  
  31.                         data:{  
  32.                             username : function(){  
  33.                                 return $('#username').val();  
  34.                             }  
  35.                         }  
  36.                     }  
  37.                 },  
  38.                 password : {  
  39.                     required : true,  
  40.                     rangelength:[6,20]  
  41.                 },  
  42.                 password_confirm : {  
  43.                     required : true,  
  44.                     equalTo  : '#password'  
  45.                 },  
  46.                 captcha:{  
  47.                     required : true,  
  48.                     maxlength:4,  
  49.                     remote   : {  
  50.                         url :'index.php?mod=ajax&act=check_captcha',  
  51.                         type:'post'  
  52.                     }  
  53.                 }  
  54.             },  
  55.             messages : {  
  56.                 username:{  
  57.                     alipay:"输入电子邮箱或手机号码",  
  58.                     remote:"用户名已存在"  
  59.                 },  
  60.                 password  : {  
  61.                     required : '您必须提供一个密码',  
  62.                     rangelength: '密码长度应在6-20个字符之间'  
  63.                 },  
  64.                 password_confirm : {  
  65.                     required : '您必须再次确认您的密码',  
  66.                     equalTo  : '两次输入的密码不一致'  
  67.                 },  
  68.                 captcha:{  
  69.                     required : "请输入验证码",  
  70.                     maxlength:"输入4位验证码",  
  71.                     remote:"验证码错误"  
  72.                 }  
  73.             }  
  74.         });  


后期比如像增加某些表单的验证规则


[javascript] view plain copy
print ?
  1. var add_user_validate=function(){  
  2.         console.info("add_user_validate");  
  3.         remove_user_validate();  
  4.         $("#province").rules("add",{required:true,min:0,messages:{min:"请选择省份"}});  
  5.         $("#city").rules("add",{required:true,min:0,messages:{min:"请选择城市"}});  
  6.         $("#area").rules("add",{required:true,min:0,messages:{ min:"请选择区域"}});  
  7.         $("#sjhztype").rules("add",{required:true,min:0,messages:{ min:"请选择合作类型"}});  
  8.         $("#realname").rules("add",{required:true});  
  9.         $("#tel").rules("add",{required:true,mobile:0});  
  10.         $("#shopname").rules("add",{required:true});  
  11.     }  

后来又因为什么原因不详以上元素需要验证


可以删除规则


[javascript] view plain copy
print ?
  1. var remove_user_validate=function(){  
  2.         console.info("remove_user_validate");  
  3.         $("#province").rules("remove");  
  4.         $("#city").rules("remove");  
  5.         $("#area").rules("remove");  
  6.         $("#sjhztype").rules("remove");  
  7.         $("#realname").rules("remove");  
  8.         $("#tel").rules("remove");  
  9.         $("#shopname").rules("remove");  
  10.     }  

你可能感兴趣的:(jquery)