jquery.validate.extend.js
9.08 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
jQuery.extend(jQuery.validator.messages, {
required: "必选字段",
remote: "请修正该字段",
email: "请输入正确格式的电子邮件",
url: "请输入合法的网址",
date: "请输入合法的日期",
dateISO: "请输入合法的日期 (ISO).",
number: "请输入合法的数字",
digits: "只能输入整数",
creditcard: "请输入合法的信用卡号",
equalTo: "请再次输入相同的值",
accept: "请输入拥有合法后缀名的字符串",
money: "请输入正确的金额格式",
maxlength: jQuery.validator.format("请输入一个长度最多是 {0} 的字符串"),
minlength: jQuery.validator.format("请输入一个长度最少是 {0} 的字符串"),
rangelength: jQuery.validator.format("请输入一个长度介于 {0} 和 {1} 之间的字符串"),
range: jQuery.validator.format("请输入一个介于 {0} 和 {1} 之间的值"),
max: jQuery.validator.format("请输入一个最大为 {0} 的值"),
min: jQuery.validator.format("请输入一个最小为 {0} 的值"),
notIllegalCharacter:"请输入正确的内容",
isPhone:"请输入正确的电话号码",
isMultiPhone:"请输入正确的电话号码",
isChar:"请填写正确的中文, 字母或者数字"
});
$.validator.setDefaults({focusInvalid:false, focusCleanup:true});
$.validator.setDefaults({
errorPlacement: function (error, element) {
try {
$(element).wrap("<i style='position: relative;font-style:normal;display: inline-block;'></i>");
element.after(error);
element.addClass();
var Labtop =element.height();
error.css("top", Labtop + 7);
// var Lableft =0;
// error.css("left", Lableft);
// if(Labtop < "28"){
// error.css("top", "28px");
// }else{
// error.css("top", Labtop);
// };
} catch(e) {
$(element).focus();
}
}
});
//增加身份证验证
function isIdCardNo(num) {
num = num.toUpperCase();
var factorArr = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
var parityBit = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2");
var varArray = new Array();
var intValue;
var lngProduct = 0;
var intCheckDigit;
var intStrLen = num.length;
var idNumber = num;
// initialize
if ((intStrLen != 15) && (intStrLen != 18)) {
return false;
}
// check and set value
for (i = 0; i < intStrLen; i++) {
varArray[i] = idNumber.charAt(i);
if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) {
return false;
} else if (i < 17) {
varArray[i] = varArray[i] * factorArr[i];
}
}
if (intStrLen == 18) {
//check date
var date8 = idNumber.substring(6, 14);
if (isDate8(date8) == false) {
return false;
}
// calculate the sum of the products
for (i = 0; i < 17; i++) {
lngProduct = lngProduct + varArray[i];
}
// calculate the check digit
intCheckDigit = parityBit[lngProduct % 11];
// check last digit
if (varArray[17] != intCheckDigit) {
return false;
}
}
else { //length is 15
//check date
var date6 = idNumber.substring(6, 12);
if (isDate6(date6) == false) {
return false;
}
}
return true;
}
function isDate6(sDate) {
if (!/^[0-9]{6}$/.test(sDate)) {
return false;
}
var myDate = new Date();
var nowyear = myDate.getFullYear();
var year, month, day;
year = sDate.substring(0, 4);
month = sDate.substring(4, 6);
console.info(nowyear + "--22-" + year);
if (year > nowyear)return false
if (year < 1700 || year > 2500) return false
if (month < 1 || month > 12) return false
return true
}
function isDate8(sDate) {
if (!/^[0-9]{8}$/.test(sDate)) {
return false;
}
var myDate = new Date();
var nowyear = myDate.getFullYear();
var year, month, day;
year = sDate.substring(0, 4);
month = sDate.substring(4, 6);
day = sDate.substring(6, 8);
var iaMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
console.info(nowyear + "---" + year);
if (year > nowyear)return false
if (year < 1700 || year > 2500) return false
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1] = 29;
if (month < 1 || month > 12) return false
if (day < 1 || day > iaMonthDays[month - 1]) return false
return true
}
//身份证验证
jQuery.validator.addMethod("isIdCardNo", function (value, element) {
return this.optional(element) || isIdCardNo(value);
}, "请正确输入您的身份证号码");
//电话号码
jQuery.validator.addMethod("isMobile", function (value, element) {
var length = value.length;
return this.optional(element) || length == 11 && /^1[3-8]\d{9}$/.test(value);
}, "请填写正确的手机号码");
//短信验证码
jQuery.validator.addMethod("isVerifCode", function (value, element) {
var length = value.length;
return this.optional(element) || length == 6 && /^\d{6}$/.test(value);
}, "请填写正确的验证码");
//验证是否是企业
jQuery.validator.addMethod("isCompany", function (value, element) {
var length = value.length;
return this.optional(element) || /^[\u4E00-\u9FA5]+$/.test(value);
}, "请填写正确的企业名称");
/**
* 验证是中文, 字母, 数字
*/
jQuery.validator.addMethod("isChar", function (value, element) {
var length = value.length;
return this.optional(element) || /^[\u4E00-\u9FA50-9a-zA-Z]+$/.test(value);
}, "请填写正确的中文, 字母或者数字!");
//验证真实姓名
jQuery.validator.addMethod("isRealName", function (value, element) {
var length = value.length;
return this.optional(element) || /^[\u4E00-\u9FA5+.]{2,10}$/.test(value) && !/^[\u4E00-\u9FA5+.]{2,10}[.]$/.test(value) && !/^[.][\u4E00-\u9FA5+.]{2,10}$/.test(value)
}, "请填写正确的名字");
//验证钱
jQuery.validator.addMethod("money", function (value, element) {
var money = /^([1-9][0-9]+|[0-9])(\.\d{1,2})?$/
return money.test(value);
}, "请输入正确的金额");
//验证电话号码
jQuery.validator.addMethod("isPhone", function (value, element) {
var phone = /((^\d{11}$)|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/
return phone.test(value);
}, "请输入正确的电话号码");
//验证多个电话号码
jQuery.validator.addMethod("isMultiPhone", function (value, element) {
var phone = /^(((\d{11})|((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})))([;、;,、,]?))+$/
return phone.test(value);
}, "请输入正确的电话号码");
// 联系电话号码验证
jQuery.validator.addMethod("isPlane", function(value, element) {
var tel = /^(\d{3,4}-)\d{7,9}$/g;
return this.optional(element) || (tel.test(value));
}, "请正确填写您的联系电话。");
// 联系电话(手机/皆可)验证
jQuery.validator.addMethod("isTel", function(value,element) {
var length = value.length;
var mobile = /^1[3-8]\d{9}$/ ;
console.info(length);
var tel = /^(\d{3,4}-)\d{7,9}$/g;
return this.optional(element) || tel.test(value) || (length==11 && mobile.test(value));
}, "请正确填写您的联系方式");
//不是非法字符
jQuery.validator.addMethod("notIllegalCharacter", function (value, element) {
var char = /^[^><\/\\;&]*$/
return char.test(value);
}, "请输入正确的内容");
//验证是否是工商注册号码
jQuery.validator.addMethod("isBusinessReg", function (value, element) {
return this.optional(element) || reg(value);
}, "请填写正确的工商注册号码");
function reg(value) {
if(!/^\d{15}$/.test(value)){
return false;
}
var ss = value.substring(0, 14);
var p;
var max;
for (var j = 1; j <= 14; j++) {
if (j == 1) {
p = 10;
}
var a1 = ss[j - 1];
var s1 = parseInt(p % 11) + parseInt(a1);
var s2;
if (s1 % 10 == 0) {
s2 = 10;
} else {
s2 = s1 % 10;
}
p = s2 * 2;
if (j == 14) {
var yu = parseInt(p%11);
amax =11-yu;
}
}
var v = value.substring(14, 15);
if (v == amax) {
return true;
}
return false;
}
jQuery.validator.addMethod("diliMethod", function(value, element, param){
var me = param;
// var t = eval(me + "('"+value+"',"+element.id+")");
var t = window[me].call(this, value, element);
if(t != true) {
$.validator.messages["diliMethod"] = t;
return false;
}
return true;
});
jQuery.validator.addMethod("diliPattern", function(value, element, param){
var params = param.split("/,");
var pa = eval(params[0]+"/");
var msg = params[1];
var t = pa.test(value);
if(t == false) {
$.validator.messages["diliPattern"] = msg !== undefined ? msg : $.validator.messages[msg];
}
return t;
});