$.extend($.validator.messages, {
required: "这是必填字段",
remote: "请修正此字段",
email: "请输入有效的电子邮件地址",
url: "请输入有效的网址",
date: "请输入有效的日期",
dateISO: "请输入有效的日期 (YYYY-MM-DD)",
number: "请输入有效的数字",
digits: "只能输入数字",
creditcard: "请输入有效的信用卡号码",
equalTo: "你的输入不相同",
extension: "请输入有效的后缀",
maxlength: $.validator.format("最多可以输入 {0} 个字符"),
minlength: $.validator.format("最少要输入 {0} 个字符"),
rangelength: $.validator.format("请输入长度在 {0} 到 {1} 之间的字符串"),
range: $.validator.format("请输入范围在 {0} 到 {1} 之间的数值"),
max: $.validator.format("请输入不大于 {0} 的数值"),
min: $.validator.format("请输入不小于 {0} 的数值")
});
$.validator.addMethod("checkPhone", function (value, element, params) {
var checkPhone = /^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$/;
return this.optional( element ) || ( checkPhone.test(value) );
}, "Please enter the right phone number.");
messages: {
userName: {
required: "请输入用户名",
minlength: "用户名长度不能少于{0}个字符",
maxlength: "用户名长度不能超过{0}个字符"
},
password: {
required: "请输入密码",
minlength: "密码长度不能小于{0}个字母"
},
confirm_password: {
required: "请输入密码",
minlength: "密码长度不能小于{0}个字母",
equalTo: "两次密码输入不一致"
},
address: {
required: "请选择地址"
},
email: {
required: "请输入邮箱",
email: "邮箱格式不正确"
},
hobby: {
required: "请选择爱好",
minlength: "至少选择{0}项爱好"
},
sex: {
required: "请选择性别"
},
file: {
required: "请上传附件",
accept: "只支持jpg,jpeg,png或gif后缀的图片"
},
stype: {
required: "请选择订阅方式"
}
}
- 后端校验
- remote属性用于实现后端验证,后端接口返回Boolean类型的值即可。
remot: {
url: "/makelocks/validate/username",
type: "get",
dataType: "json",
data: {
userName: function() {
return $("input[name='userName']").val();
}
}
}
- 调整错误信息的显示位置
- 当校验不通过时,默认的错误信息会显示在校验元素的后面,但当检验元素为radio或checkbox时,错误信息的显示位置不当,需进行如下调整
errorPlacement: function(error, element) { //指定错误信息的显示位置
if (element.is(':radio') || (element.is(':checkbox') { //如果元素为radio或checkbox
var eid = element.attr('name'); //获取元素的name属性值
error.appendTo(element.parent().parent()); //将错误信息添加到当前元素的父类元素后面(根据实际页面位置进行调整)
} else {
error.insertAfter(element);
}
}
- 示例
<body>
<form class="form">
<div class="form-group">
<label>用户名</label>
<input type="text" class="form-control" name="userName" placeholder="用户名">
</div>
<div class="form-group">
<label>密码</label>
<input type="password" class="form-control" name="password" placeholder="密码">
</div>
<div class="form-group">
<label>确认密码</label>
<input type="password" class="form-control" name="confirm_password" placeholder="请再次输入密码">
</div>
<div class="form-group">
<label>手机号</label>
<input type="text" class="form-control" name="phone" placeholder="手机号">
</div>
<div class="form-group">
<label>邮箱</label>
<input type="text" class="form-control" name="email" placeholder="邮箱">
</div>
<div class="form-group">
<label>地址</label>
<select class="form-control" name="address">
<option value="">- 请选择 -</option>
<option value="1">福州</option>
<option value="2">厦门</option>
<option value="3">泉州</option>
</select>
</div>
<div class="form-group">
<label>爱好</label>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby" value="1">
<label class="form-check-label">游泳</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby" value="2">
<label class="form-check-label">唱歌</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby" value="3">
<label class="form-check-label">睡觉</label>
</div>
</div>
<div class="form-group">
<label>性别</label>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="sex" value="1">
<label class="form-check-label">男</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="sex" value="2">
<label class="form-check-label">女</label>
</div>
</div>
<div class="form-group">
<label>上传图片</label>
<input type="file" class="form-control-file" name="file">
</div>
<div class="form-group">
<div class="form-check form-check-inline">
<input type="checkbox" class="form-check-input" name="subscribe">
<label class="form-check-label">订阅</label>
</div>
</div>
<div class="form-group">
<label>订阅方式</label>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="stype" value="1">
<label class="form-check-label">RSS</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="stype" value="2">
<label class="form-check-label">邮箱订阅</label>
</div>
</div>
<button id="submit" class="btn btn-primary">Submit</button>
<button id="reset" class="btn btn-danger">Reset</button>
</form>
</body>
<script data-th-inline="javascript">
$.validator.addMethod("checkPhone", function (value, element) {
var checkPhone = /^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$/;
return this.optional( element ) || ( checkPhone.test(value) );
}, "请输入正确的手机号码!");
$(function () {
validateRules();
$("#submit").on("click", function () {
var validator = $(".form").validate();
var flag = validator.form();
if (flag) {
$.ajax({
url: "/makelocks/validate/register",
type: "post",
data: {
userName: $("input[name='userName']").val().trim()
},
dataType: "json",
success: function (r) {
if (r.code === 0) {
window.location.href = "/makelocks/index";
console.log("success:" + r.msg);
}
}
});
}
});
$("#reset").on("click", function () {
var validator = $(".form").validate();
validator.resetForm();
});
});
function validateRules() {
$(".form").validate({
rules: {
userName: {
required: true,
minlength: 6,
maxlength: 30
},
password: {
required: true,
minlength: 6
},
confirm_password: {
required: true,
minlength: 6,
equalTo: "[name='password']"
},
phone:{
checkPhone: true
},
email: {
required: true,
email: true
},
address: {
required: true
},
hobby: {
required: true,
minlength: 2
},
sex: {
required: true
},
file: {
required: true,
accept: "image/jpg,image/jpeg,image/png,image/gif"
},
stype: {
required: "[name='subscribe']:checked"
}
},
messages: {
userName: {
required: "请输入用户名",
minlength: "用户名长度不能少于{0}个字符",
maxlength: "用户名长度不能超过{0}个字符"
},
password: {
required: "请输入密码",
minlength: "密码长度不能小于{0}个字母"
},
confirm_password: {
required: "请输入密码",
minlength: "密码长度不能小于{0}个字母",
equalTo: "两次密码输入不一致"
},
address: {
required: "请选择地址"
},
email: {
required: "请输入邮箱",
email: "邮箱格式不正确"
},
hobby: {
required: "请选择爱好",
minlength: "至少选择{0}项爱好"
},
sex: {
required: "请选择性别"
},
file: {
required: "请上传附件",
accept: "只支持jpg,jpeg,png或gif后缀的图片"
},
stype: {
required: "请选择订阅方式"
}
},
errorPlacement: function (error, element) {
if (element.is(':radio') || element.is(':checkbox')) {
var eid = element.attr('name');
error.appendTo(element.parent().parent());
} else {
error.insertAfter(element);
}
}
});
}
</script>