jquery.validate.extend.js
4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*this is basic form validation using for validation person's basic information author:Clara Guo data:2017/07/20*/
$(document).ready(function(){
$.validator.setDefaults({
submitHandler: function(form) {
form.submit();
}
});
//手机号码验证身份证正则合并:(^\d{15}$)|(^\d{17}([0-9]|X)$)
jQuery.validator.addMethod("isPhone",function(value,element){
var length = value.length;
var phone=/^1[3|4|5|7|8][0-9]\d{8}$/;
return this.optional(element)||(length == 11 && phone.test(value));
},"请填写正确的11位手机号");
//电话号码验证
jQuery.validator.addMethod("isTel",function(value,element){
var tel = /^(0\d{2,3}-)?\d{7,8}$/g;//区号3,4位,号码7,8位
return this.optional(element) || (tel.test(value));
},"请填写正确的座机号码");
//姓名校验
jQuery.validator.addMethod("isName",function(value,element){
var name=/^[\u4e00-\u9fa5]{2,6}$/;
return this.optional(element) || (name.test(value));
},"姓名只能用汉字,长度2-4位");
//校验用户名
jQuery.validator.addMethod("isUserName",function(value,element){
var userName=/^[a-zA-Z0-9]{2,13}$/;
return this.optional(element) || (userName).test(value);
},'请输入数字或者字母,不包含特殊字符');
//校验身份证
jQuery.validator.addMethod("isIdentity",function(value,element){
var id= /^(\d{15}$|^\d{18}$|^\d{17}(\d|X))$/;
return this.optional(element) || (id.test(value));
},"请输入正确的15或18位身份证号,末尾为大写X");
//校验出生日期
jQuery.validator.addMethod("isBirth",function(value,element){
var birth = /^(19|20)\d{2}-(1[0-2]|0?[1-9])-(0?[1-9]|[1-2][0-9]|3[0-1])$/;
return this.optional(element) || (birth).test(value);
},"出生日期格式示例2000-01-01");
//校验新旧密码是否相同
jQuery.validator.addMethod("isdiff",function(){
var p1=$("#pwdOld").val();
var p2=$("#pwdNew").val();
if(p1==p2){
return false;
}else{
return true;
}
});
//校验新密码和确认密码是否相同
jQuery.validator.addMethod("issame",function(){
var p3=$("#confirm_password").val();
var p4=$("#pwdNew").val();
if(p3==p4){
return true;
}else{
return false;
}
});
//校验基础信息表单
$("#basicInfoForm").validate({
errorElement:'span',
errorClass:'help-block error-mes',
rules:{
name:{
required:true,
isName:true
},
sex:"required",
birth:"required",
mobile:{
required:true,
isPhone:true
},
email:{
required:true,
email:true
}
},
messages:{
name:{
required:"请输入中文姓名",
isName:"姓名只能为汉字"
},
sex:{
required:"请输入性别"
},
birth:{
required:"请输入出生年月"
},
mobile:{
required:"请输入手机号",
isPhone:"请填写正确的11位手机号"
},
email:{
required:"请输入邮箱",
email:"请填写正确的邮箱格式"
}
},
errorPlacement:function(error,element){
element.next().remove();
element.closest('.gg-formGroup').append(error);
},
highlight:function(element){
$(element).closest('.gg-formGroup').addClass('has-error has-feedback');
},
success:function(label){
var el = label.closest('.gg-formGroup').find("input");
el.next().remove();
label.closest('.gg-formGroup').removeClass('has-error').addClass("has-feedback has-success");
label.remove();
},
submitHandler:function(form){
alert("保存成功!");
}
});
//校验修改密码表单
$("#modifyPwd").validate({
onfocusout: function(element) { $(element).valid()},
debug:false, //表示校验通过后是否直接提交表单
onkeyup:false, //表示按键松开时候监听验证
rules:{
pwdOld:{
required:true,
minlength:6
},
pwdNew:{
required:true,
minlength:6,
isdiff:true,
//issame:true,
},
confirm_password:{
required:true,
minlength:6,
issame:true,
}
},
messages:{
pwdOld : {
required:'必填',
minlength:$.validator.format('密码长度要大于6')
},
pwdNew:{
required:'必填',
minlength:$.validator.format('密码长度要大于6'),
isdiff:'原密码与新密码不能重复',
},
confirm_password:{
required:'必填',
minlength:$.validator.format('密码长度要大于6'),
issame:'新密码要与确认新密码一致',
}
},
errorElement:"mes",
errorClass:"gg-star",
errorPlacement: function(error, element)
{
element.closest('.gg-formGroup').append(error);
}
});
});