我的代码如下
//登录表单验证
$(document).ready(function(){
$("#submitForm").validate({
rules: {
company: {
required: true,
minlength: 2,
maxlength: 10
},
user: {
required: true,
minlength: 2,
maxlength: 10
},
phone: {
required: true,
isPhone: true
}
},
messages: {
company: {
required: '请输入公司名称!',
minlength: '公司名称不能小于2个字符!',
maxlength: '公司名称不能超过10个字符!'
},
user: {
required: '请输入联系人姓名!',
minlength: '公司名称不能小于2个字符!',
maxlength: '公司名称不能超过10个字符!'
},
phone: {
required: '请输入联系电话!',
isPhone: '请输入一个有效的联系电话!'
}
},
submitHandler: function (form) { //通过验证,运行函数
console.log($(form).serialize()); //form表单序列号
},
groups:{
login:"company user phone"
},
errorPlacement:function(error,element){
error.appendTo("#error_tip");
},
errorElement: "em", //用来创建错误提示信息标签
});
});
// 联系电话(手机/电话皆可)验证
jQuery.validator.addMethod("isPhone", function(value,element) {
var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
var tel = /^\d{3,4}-?\d{7,9}$/;
return this.optional(element) || (tel.test(value) || mobile.test(value));
});
ruibin
荼酒
pardon110