<script type="text/javascript">
$(document).ready(function() {
jQuery.validator.addMethod(
"DepartmentCheck",
function(value, element) {
return this.optional(element) || (value.indexOf("11111111-1111-1111-1111-111111111111") == -1);
},
"请选择部门"
);
$("#form-employee-edit").validate({
rules: {
DepartmentId: {
DepartmentCheck: true
},
Name: {
required: true,
rangelength: [2, 50]
},
Post: "required",
Tel: "required"
//这里需要增加部门对比
},
messages: {
DepartmentId: {
DepartmentCheck: "请选择部门"
},
Name: {
required: "姓名不能为空",
rangelength: "姓名长度介于2到50个字符之间"
},
Post: "职位不能为空",
Tel: "办公电话不能为空"
}
});
//判断姓名是否重名
$("#Name").blur(function() { ValidateName(); });
});
function ValidateName() {
var name = $("#Name").val();
var actoinUrl = '<% = Url.Action("ValidateEmployeeName", "Employee") %>';
$.getJSON(actoinUrl, { "name": name }, function(json) {
$("#NameValidate").empty();
if (!json) {
$("#Name").after("<label id='NameValidate' class='error'>" + name + "已存在,姓名重名可以不改.</label>");
}
});
}
本文介绍了一个使用jQuery进行前端表单验证的案例,包括部门选择、姓名输入的有效性检查及姓名重复性的实时验证。
8万+

被折叠的 条评论
为什么被折叠?



