-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputCheck.js
310 lines (309 loc) · 8.99 KB
/
InputCheck.js
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
(function (window) {
var InputCheck = function (target, config) {
if ($('#' + target).length == 0 || !$('#' + target).is('input'))
return;
this.trigger = 'blur';
this.required = true;
this.blankmsg = '不能为空!';
this.error = target + '-err';
this.pass = target + '-pass';
this.loading = target + '-loading';
this.errorIcon = target + 'err-icon';
this.passIcon = target + 'pass-icon';
$.extend(this, config);
delete config;
this.target = $('#' + target);
this.error = $('#' + this.error);
this.pass = $('#' + this.pass);
this.loading = $('#' + this.loading);
this.errorIcon = $('#' + this.errorIcon);
this.passIcon = $('#' + this.passIcon);
if (null == this.validators) {
this.validators = new Array();
}
if (this.required) {
this.validators.splice(0, 0, {
validator : 'blank',
msg : this.blankmsg,
template : this.defaultTemplate
});
}
this.bindEvent();
};
InputCheck.prototype = {
bindEvent : function () {
var self = this;
this.target.bind(this.trigger, function (e) {
self.check();
});
},
check : function () {
if (this.target.length == 0 || !this.target.is('input'))
return true;
if ($.isFunction(this.beforeValidate)) {
this.beforeValidate();
}
var value = $.trim(this.target.val());
var re = true;
var self = this;
var ajaxValidators = new Array();
$.each(this.validators, function (i, v) {
if ($.isFunction(v.beforeValidate)) {
v.beforeValidate.call(self);
}
if ('ajax' == v.type && null != v.url) {
ajaxValidators.push(v);
} else if (($.isFunction(v.validator) && !v.validator(value, self.target)) ||
($.isFunction(window.$inputcheck.utils._validators[v.validator]) &&
!window.$inputcheck.utils._validators[v.validator](value))) {
self.showMSG(v.msg, v.template);
re = false;
if ($.isFunction(v.afterValidate)) {
v.afterValidate.call(self, re);
}
return false;
}
if ('ajax' != v.type && $.isFunction(v.afterValidate)) {
v.afterValidate.call(self, re);
}
});
if (re && ajaxValidators.length > 0) {
var index = 0;
function exeValidate(ajaxValidators) {
ajaxValidator = ajaxValidators[index];
index++;
self.showLoading();
var parameters = {};
parameters[ajaxValidator.parameterName] = value;
$.getJSON(ajaxValidator.url, parameters, function (data) {
if ($.isFunction(ajaxValidator.validator)) {
var re = ajaxValidator.validator(data);
if ($.isFunction(ajaxValidator.afterValidate)) {
ajaxValidator.afterValidate.call(self, re);
}
if (!re) {
self.showMSG(ajaxValidator.msg, ajaxValidator.template);
} else {
self.showPass();
if (index < ajaxValidators.length) {
exeValidate(ajaxValidators);
}
}
}
});
}
exeValidate(ajaxValidators);
} else if (re) {
this.showPass();
}
if ($.isFunction(this.afterValidate)) {
this.afterValidate(re);
}
return re;
},
showMSG : function (m, t) {
this.pass.hide();
this.passIcon.hide();
this.loading.hide();
this.error.html(this.message(m, t)).show();
this.errorIcon.show();
},
showPass : function () {
this.error.hide();
this.errorIcon.hide();
this.loading.hide();
this.pass.show();
this.passIcon.show();
},
showLoading : function () {
this.error.hide();
this.errorIcon.hide();
this.pass.hide();
this.passIcon.hide();
this.loading.show();
},
message : function (m, t) {
if (null == m) {
m = '错误信息未定义';
}
if (null != t) {
return t.replace('{msg}', m);
} else if (null != this.defaultTemplate) {
return this.defaultTemplate.replace('{msg}', m);
} else {
return m;
}
}
};
var utils = {
_validators : {
blank : function (v) {
return '' != v;
},
email : function (v) {
var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
return reg.test(v);
},
mobile : function (v) {
var reg = /^1[3-8]{1}\d{9}$/g;
return reg.test(v);
},
phone : function (v) {
var reg = /^(\d{3,4}-?)?\d{7,8}([\-转]\d{3,5})?$/g;
return reg.test(v);
},
number : function (v) {
return this.integer(v) || this.float(v);
},
integer : function (v) {
var reg = /^-?(0|[1-9]\d*)$/;
return reg.test(v);
},
float : function (v) {
var reg = /^-?(0|[1-9]\d*)\.\d+$/;
return reg.test(v);
},
url : function (v) {
var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@
+ "(([0-9]{1,3}.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
+ "|" // 允许IP和DOMAIN(域名)
+ "([0-9a-z_!~*'()-]+.)*" // 域名- www.
+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]." // 二级域名
+ "[a-z]{2,6})" // first level domain- .com or .museum
+ "(:[0-9]{1,4})?" // 端口- :80
+ "((/?)|" // a slash isn't required if there is no file name
+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
var re = new RegExp(strRegex);
return re.test(v);
},
ID : function (v) {
return IdCardValidate(v);
}
}
};
var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1]; // 加权因子
var ValideCode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2]; // 身份证验证位值.10代表X
function IdCardValidate(idCard) {
idCard = trim(idCard.replace(/ /g, ""));
if (idCard.length == 15) {
return isValidityBrithBy15IdCard(idCard);
} else if (idCard.length == 18) {
var a_idCard = idCard.split(""); // 得到身份证数组
if (isValidityBrithBy18IdCard(idCard) && isTrueValidateCodeBy18IdCard(a_idCard)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* 判断身份证号码为18位时最后的验证位是否正确
* @param a_idCard 身份证号码数组
* @return
*/
function isTrueValidateCodeBy18IdCard(a_idCard) {
var sum = 0; // 声明加权求和变量
if (a_idCard[17].toLowerCase() == 'x') {
a_idCard[17] = 10; // 将最后位为x的验证码替换为10方便后续操作
}
for (var i = 0; i < 17; i++) {
sum += Wi[i] * a_idCard[i]; // 加权求和
}
valCodePosition = sum % 11; // 得到验证码所位置
if (a_idCard[17] == ValideCode[valCodePosition]) {
return true;
} else {
return false;
}
}
/**
* 通过身份证判断是男是女
* @param idCard 15/18位身份证号码
* @return 'female'-女、'male'-男
*/
function maleOrFemalByIdCard(idCard) {
idCard = trim(idCard.replace(/ /g, "")); // 对身份证号码做处理。包括字符间有空格。
if (idCard.length == 15) {
if (idCard.substring(14, 15) % 2 == 0) {
return 'female';
} else {
return 'male';
}
} else if (idCard.length == 18) {
if (idCard.substring(14, 17) % 2 == 0) {
return 'female';
} else {
return 'male';
}
} else {
return null;
}
}
/**
* 验证18位数身份证号码中的生日是否是有效生日
* @param idCard 18位书身份证字符串
* @return
*/
function isValidityBrithBy18IdCard(idCard18) {
var year = idCard18.substring(6, 10);
var month = idCard18.substring(10, 12);
var day = idCard18.substring(12, 14);
var temp_date = new Date(year, parseFloat(month) - 1, parseFloat(day));
// 这里用getFullYear()获取年份,避免千年虫问题
if (temp_date.getFullYear() != parseFloat(year)
|| temp_date.getMonth() != parseFloat(month) - 1
|| temp_date.getDate() != parseFloat(day)) {
return false;
} else {
return true;
}
}
/**
* 验证15位数身份证号码中的生日是否是有效生日
* @param idCard15 15位书身份证字符串
* @return
*/
function isValidityBrithBy15IdCard(idCard15) {
var year = idCard15.substring(6, 8);
var month = idCard15.substring(8, 10);
var day = idCard15.substring(10, 12);
var temp_date = new Date(year, parseFloat(month) - 1, parseFloat(day));
// 对于老身份证中的你年龄则不需考虑千年虫问题而使用getYear()方法
if (temp_date.getYear() != parseFloat(year)
|| temp_date.getMonth() != parseFloat(month) - 1
|| temp_date.getDate() != parseFloat(day)) {
return false;
} else {
return true;
}
}
//去掉字符串头尾空格
function trim(str) {
return str.replace(/(^\s*)|(\s*$)/g, "");
}
window.$inputcheck = {};
window.$inputcheck.utils = utils;
window.InputCheck = InputCheck;
})(window);
$.fn.extend({
InputCheck : function (config) {
this.each(function (i, e) {
$(e).data('_checker', new InputCheck($(e).attr('id'), config));
});
},
check : function (shortCut) {
var re = true;
this.each(function (i, e) {
if (!$(e).data('_checker').check()) {
re = false;
if (shortCut) {
return false;
}
}
});
return re;
}
});