对于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{}
}
好了这样就行了
juqeryvalidate验证 自由触发验证
最新推荐文章于 2020-04-01 15:43:20 发布