juqeryvalidate验证 自由触发验证


对于juqery的验证框架 实在是不想用 但是没办法项目组要用统一的验证方式对于jquery的验证最常见的写法就是 如下:

$("#addressForm").validate({
rules: {
'memberAddress.name':{
required: true,
minlength:1,
maxlength:20
},
'memberAddress.address':{
required: true,
minlength:1,
maxlength:128
},
'memberAddress.postcode':{
required: true,
minlength:6,
maxlength:6,
number:true
},
'memberAddress.zoneCode':{
minlength:3,
maxlength:4,
number:true
},
'memberAddress.tel':{
minlength:7,
maxlength:8,
number:true
},
'memberAddress.mobile':{
minlength:11,
maxlength:11,
number:true
}
},

messages: {
'memberAddress.name': {
required: '请输联系人姓名',
minlength: '请输联系人姓名',
maxlength: '联系人姓名的最大长度为20'
},
'memberAddress.address':{
required:'请输入联系地址',
minlength:'请输入联系地址',
maxlength:'联系地址最大长度为128'
},
'memberAddress.postcode':{
required:'请输入邮编',
minlength:'请输入正确的邮编',
maxlength:'请输入正确的邮编',
number:'请输入正确的邮编'
},
'memberAddress.zoneCode':{
minlength:'请输入正确的区号',
maxlength:'请输入正确的区号',
number:'请输入正确的区号'
},
'memberAddress.tel':{
minlength:'请输入正确的电话',
maxlength:'请输入正确的电话',
number:'请输入正确的电话'
},
'memberAddress.mobile':{
minlength:'请输入正确的手机号',
maxlength:'请输入正确的手机号',
number:'请输入正确的手机号'
}

},
errorElement: "span",
errorClass: "error",
success:"checked",
submitHandler : function() {//控制是否提交,可以不写
return checkInfo();
}

});
但是假设我的表单是ajax提交的的 哪怎么办呢

可以这样写:
validate=$("#addressForm").validate({
rules: {
'memberAddress.name':{
required: true,
minlength:1,
maxlength:20
},
'memberAddress.address':{
required: true,
minlength:1,
maxlength:128
},
'memberAddress.postcode':{
required: true,
minlength:6,
maxlength:6,
number:true
},
'memberAddress.zoneCode':{
minlength:3,
maxlength:4,
number:true
},
'memberAddress.tel':{
minlength:7,
maxlength:8,
number:true
},
'memberAddress.mobile':{
minlength:11,
maxlength:11,
number:true
}
},

messages: {
'memberAddress.name': {
required: '请输联系人姓名',
minlength: '请输联系人姓名',
maxlength: '联系人姓名的最大长度为20'
},
'memberAddress.address':{
required:'请输入联系地址',
minlength:'请输入联系地址',
maxlength:'联系地址最大长度为128'
},
'memberAddress.postcode':{
required:'请输入邮编',
minlength:'请输入正确的邮编',
maxlength:'请输入正确的邮编',
number:'请输入正确的邮编'
},
'memberAddress.zoneCode':{
minlength:'请输入正确的区号',
maxlength:'请输入正确的区号',
number:'请输入正确的区号'
},
'memberAddress.tel':{
minlength:'请输入正确的电话',
maxlength:'请输入正确的电话',
number:'请输入正确的电话'
},
'memberAddress.mobile':{
minlength:'请输入正确的手机号',
maxlength:'请输入正确的手机号',
number:'请输入正确的手机号'
}

},
errorElement: "span",
errorClass: "error",
success:"checked",
submitHandler : function() {//控制是否提交,可以不写
return checkInfo();
}

});


然后 在你提交前触发的function中
function submitform(){
var f=validate.form();
if(f){}else{}
}
好了这样就行了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值