-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator.js
216 lines (190 loc) · 6.7 KB
/
validator.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
// bootstrap3-validator.js
// 黄雪良, https://github.com/hxlniada/bootstrap3-validator
// Licensed under the MIT license.
$.fn.extend({
validator: function(options) {
var checkGroup = [];
this.find('[data-validation]').each(function() {
var $inputGroup = $(this),
validation = $inputGroup.data('validation'),
target = $inputGroup.data('target'),
$element = $inputGroup.find('[name=' + target + ']');
if ($element.prop('disabled') || $element.prop('readonly')) {
return;
}
$element.on('change blur', function() {
clearTimeout($element.data('validator-timer'));
$.validator.checkValid(this, validation, $inputGroup);
});
$element.on('keyup', function() {
var _this = this, timer;
clearTimeout($element.data('validator-timer'));
timer = setTimeout(function() {
$.validator.checkValid(_this, validation, $inputGroup);
}, $.validator.delay);
$element.data('validator-timer', timer);
});
checkGroup.push({
inputGroup: $inputGroup,
element: $element,
validation: validation
});
});
this.data('checkGroup', checkGroup);
},
checkValid: function() {
var checkGroup = this.data('checkGroup'),
validateResult,
firstInvalid;
for (var i = 0; i < checkGroup.length; i++) {
validateResult = $.validator.checkValid(checkGroup[i].element, checkGroup[i].validation, checkGroup[i].inputGroup, true);
if (!validateResult && (typeof firstInvalid === 'undefined')) {
firstInvalid = i;
}
}
if (typeof firstInvalid === 'number') {
checkGroup[firstInvalid].element.focus();
return false;
} else {
return true;
}
}
});
$.extend({
validator: {
delay: 500,
extend: function(options) {
$.extend(true, this, options);
},
validRender: function($element, $inputGroup) {
$inputGroup.removeClass('has-error').addClass('has-success').find('.help-block').text('');
},
invalidRender: function($element, $inputGroup, message) {
$inputGroup.removeClass('has-success').addClass('has-error').find('.help-block').text(message);
},
remoteHanlder: function(value, data, $element, $inputGroup) {//远程校验比较特殊
if (data === -1) {
$element.data('remote', 'error');
$.validator.invalidRender($element, $inputGroup, $.validator.messages.remote.error);
return;
}
if (data.msg === 'valid_success') {
$element.data('remote', 'success');
$.validator.validRender($element, $inputGroup);
return;
} else {
$element.data('remoteInvalids')[value] = $.validator.messages.remote.fail;
$element.data('remote', 'fail');
$.validator.invalidRender($element, $inputGroup, $.validator.messages.remote.fail);
}
},
rules: {
remote: function(value, url, $element, $inputGroup, _check) {
if (!$element.data('remoteInvalids')) {
$element.data('remoteInvalids', {});
}
//如果检测失败,直接返回失败的内容
if (value in ($element.data('remoteInvalids'))) {
$.validator.remoteHanlder.call(this, value, false, $element, $inputGroup);
return $element.data('remoteInvalids')[value];
}
if (_check) {
return true;
}
var data = {};
data[$element.attr('name')] = value;
$.get(url, $.extend(data, {_: +new Date()}), null, 'json')
.done(function(data) {
$.validator.remoteHanlder.call(this, value, data, $element, $inputGroup);
})
.fail(function() {
$.validator.remoteHanlder.call(this, value, -1, $element, $inputGroup);
});
$element.data('remote', 'pending');
return $.validator.messages.remote.pending;
},
required: function(value) {
return !!value.length ? true : $.validator.messages['required'];
},
mobile: function(value) {
return /^1[1-9]\d{9}$/.test(value) ? true : $.validator.messages['mobile'];
},
email: function(value) {
return /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(value) ? true: $.validator.messages['email'];
},
url: function(value) {
return /(https?|ftp|mms):\/\/([A-z0-9]+[_\-]?[A-z0-9]+\.)*[A-z0-9]+\-?[A-z0-9]+\.[A-z]{2,}(\/.*)*\/?/.test(value) ? true : $.validator.messages['url'];
},
webUrl: function(value) {
return /(https?):\/\/([A-z0-9]+[_\-]?[A-z0-9]+\.)*[A-z0-9]+\-?[A-z0-9]+\.[A-z]{2,}(\/.*)*\/?/.test(value) ? true : $.validator.messages['webUrl'];
},
maxLength: function(value, maxLength) {
maxLength = parseInt(maxLength, 10);
return value.length > maxLength ? $.validator.messages.maxLength.replace('{{1}}', maxLength) : true;
},
minLength: function(value, minLength) {
minLength = parseInt(minLength, 10);
return value.length < minLength ? $.validator.messages.minLength.replace('{{1}}', minLength) : true;
},
range: function(value, range) {
var min = parseInt(range.split(',')[0], 10),
max = parseInt(range.split(',')[1], 10);
return (value.length <= max && value.length >= min) ? $.validator.messages.range.replace('{{1}}', min).replace('{{2}}', max) : true;
}
},
messages: {
remote: {
pending: '数据远程验证中……',
error: '数据发送失败',
fail: '该数据不合法'
},
required: '此处不能为空',
mobile: '不是合法的手机号码',
email: '不是合法的邮箱格式',
maxLength: '长度不能超过{{1}}',
minLength: '长度不能小于{{1}}',
url: '不是合法的url格式',
webUrl: '不是合法的(http/https)url格式',
range: '长度必须不小于{{1}}不超过{{2}}'
},
//check表示是否是主动检测
checkValid: function(element, validation, $inputGroup, _check) {
var $element = $(element),
value = $.trim($element.val()),
i, ruleName, params, invalidValues, message,
rules = validation.split('|');
//如果检测到非法数据中存在该数据,就不需要再检测了
invalidValues = $element.data('invalids') || [];
if ($element.data('remote') === 'pending') {
return false;
}
for (i = 0; i < rules.length; i++) {
ruleName = rules[i].split('::')[0];
params = rules[i].split('::')[1];
//获取检测后的结果
message = $.validator.rules[ruleName](value, params, $element, $inputGroup, _check);
if (typeof message === 'string') {
if (ruleName !== 'remote') {
invalidValues.push(value);
}
$element.data('invalids', invalidValues);
$.validator.invalidRender($element, $inputGroup, message);
return false;
}
}
$.validator.validRender($element, $inputGroup);
return true;
},
hasValue: function(arr, value) {
if (Array.prototype.indexOf) {
return !!~arr.indexOf(value);
}
for (var i = 0; i < arr.length; i++) {
if (arr[i] == value) {
return true;
}
}
return false;
}
}
});