https://jqueryvalidation.org/documentation/
remote 使用
$("#form-admin-add").validate({
rules: {
userName: {
required: true,
minlength: 2,
maxlength: 16,
remote: {
type: "post",
url: "/checkUserName",
// data 默认使用的userName
data: {
userName: function() {
return $("#userName").val();
}
},
dataType: "html",
dataFilter: function(data, type) {
var data = JSON.parse(data);
if (data.status == 400)
return false;
else
return true;
}
}
},
password: {
required: true,
},
password2: {
required: true,
equalTo: "#password"
},
sex: {
required: true,
},
phone: {
required: true,
isPhone: true,
},
email: {
required: true,
email: true,
},
roleId: {
required: true,
},
},
success: function(label) {
//正确时的样式
label.text(" ").addClass("success");
},
messages: {
userName: {
required: "请输入用户名,3-16个字符(字母、数字、下划线),注册后不能更改",
minlength: "用户名长度不能小于3个字符",
maxlength: "用户名长度不能大于16个字符",
remote: "用户名不可用"
}
},
onkeyup: false,
focusCleanup: true,
success: "valid"
});
自定义方法
jQuery.validator.addMethod("checkname",
function(value, element) {
var resultval = false;
var name = $("input[name='Name']").val();
jQuery.ajax({
type: 'post',
async: false,
url: '/checkname'
dataType: 'json',
success: function(result) {
if (result.code == 0) {
resultval = false;
} else {
resultval = true;
}
},
error: function() {}
});
return resultval;
}, "您的名称重复,请重新输入");
//使用
$("#searchForm").validate({
rules: {
materialCode: {
checkname: true
}
}
})