-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.js
465 lines (394 loc) · 11.8 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/**
* @license
* Copyright (c) 2015 Sasha Bichkov <[email protected]>
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* UMD pattern which defines a jQuery plugin that works
* with AMD and browser globals
* https://github.com/umdjs/umd/blob/master/jqueryPlugin.js
*/
;(function (factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(jQuery);
}
}(function ($) {
'use strict';
var PLUGIN_NAME = 'validator';
var defaults = {
autoClear: true
};
var callbacks;
var filters = {
callback: function(opt, val, $el) {
return callbacks[opt](val, $el);
},
chars: function(val) {
return /\w*/.test(val);
},
color: function(val) {
return /^(?:.{7}|.{4})?$/.test(val) && /^(#[A-Fa-f\d]+)?$/.test(val);
},
/**
* I took the regular expression from here,
* but I changed it since $(input[type="date"]).val()
* returns the date in "yyyy-mm-dd" format.
* http://www.regular-expressions.info/dates.html
*/
date: function(val) {
return /^(19|20)\d\d[-\s\/\.](0[1-9]|1[012])[-\s\/\.](0[1-9]|[12][0-9]|3[01])$/.test(val);
},
digits: function(val) {
return /^(\d+)?$/.test(val);
},
/**
* Jeffrey E.F. Friedl
* Mastering Regular Expressions, 3rd Edition
*/
eraseTags: function(val) {
return val.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi, '');
},
equal: function(opt, val) {
return $(opt).val() === val;
},
/**
* About all file extensions you can read here:
* http://fileinfo.com/filetypes/common
*/
image: function(val) {
return /[^\s]+(\.(jpe?g|png|gif|bmp))$/.test(val);
},
login: function(val) {
return /^([A-Za-z\d_\.]+)?$/.test(val);
},
max: function(opt, val) {
return opt >= val.length
},
email: function(val) {
return /(\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}\b)?/i.test(val);
},
min: function(opt, val) {
return opt <= val.length || val.length === 0;
},
onlyRu: function(val) {
return /^([а-яА-ЯёЁйЙ\s]+)?$/.test(val);
},
onlyEng: function(val) {
return /^([a-zA-Z\s]+)?$/.test(val);
},
phone: function(val) {
return /^(\+?\d{1,3}?[- .]?\(?(?:\d{2,3})\)?[- .]?\d\d\d[- .]?\d\d\d\d)?$/.test(val);
},
phoneOrMail: function(val) {
return (/^(\+?\d{1,3}?[- .]?\(?(?:\d{2,3})\)?[- .]?\d\d\d[- .]?\d\d\d\d)?$/.test(val)) ||
(/^([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})?$/i.test(val));
},
required: function(val) {
return !!val;
},
time: function(val) {
return /^((((2[0-3]|[01]?\d):([0-5]\d))|(24:00)))?$/.test(val);
},
url: function(val) {
return /^((?:(https?|ftp):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?)?$/.test(val);
},
/**
* https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
* http://htmlpurifier.org/live/smoketests/xssAttacks.php
*/
xss: function(val) {
var htmlEscapes = {
'&' : '&apm;',
'<' : '<',
'>' : '>',
'"' : '"',
"'" : '''
};
return val.replace(/[&<>"']/g, function(match) {
return htmlEscapes[match];
});
}
};
var Validator = function (container, options) {
if (options) {
this.options = $.extend({}, defaults, options);
}
callbacks = $.extend({}, callbacks, this.options.callbacks);
this.container = container;
this._defaults = defaults;
this.init();
};
Validator.prototype.init = function() {
if (this.options) {
this._getElementsAndFilters();
this._getRules();
this._checkRules();
this._setEvents();
this._setFormSubmit();
}
};
Validator.prototype._getElementsAndFilters = function() {
var rules = this.rules = [];
var selectors = this.selectors = [];
var filters = this.options.filters;
for (var selector in filters) {
if (selector && typeof selector === 'string') {
selectors.push(selector);
} else {
throw new TypeError('Element name is empty or not a string!');
}
var rule = filters[selector];
if (rule && typeof selector === 'string') {
rules.push(rule);
} else {
throw new TypeError('Filter is empty or not a string!');
}
}
};
Validator.prototype._getRules = function() {
var context = this;
var rules = this.rules;
$.each(rules, function(i, value) {
rules[i] = context._proccessRules(rules[i]);
});
};
/**
* This is a function for proccessing filters
* Input: 'onlyEng|required|max:20'
* Output: ['onlyEng', 'required', {filter:'max', val:'20'}]
* @param {(object|string)}
* @returns ...
*/
Validator.prototype._proccessRules = function(rule) {
var rules = rule.replace(/\s+/g, '').split('|');
$.each(rules, function(j, value) {
if (value.match(/:/)) {
var dataRule = value.split(':');
rules.splice(
j, 1,
{
filter: dataRule[0],
val: dataRule[1]
}
);
}
});
return rules;
};
/**
* Find user filters in filters array
*/
Validator.prototype._checkRules = function() {
$.each(this.rules, function(i, value) {
var j = value.length;
var data, filter;
while ( j-- ) {
data = value[j];
filter = typeof data === 'object' ? data.filter : data;
if (!(filter in filters)) {
throw new Error('The filter "' + filter + '" not found');
}
}
});
};
/**
* Set events on all(!) input elements without
* checkbox and radio button
*/
Validator.prototype._setEvents = function() {
var events = this.options.events;
var messages = this.options.messages;
var check = this.check.bind(this);
var selectors = this.selectors;
var rules = this.rules;
if (events) {
$.each(events, function(event, status) {
if (status) {
$.each(selectors, function(i, el) {
var $el = $(el);
var type = $el.attr('type');
if ( type !== 'checkbox' && type !== 'radio' ) {
$el.on(event, function() {
var message = messages ? messages[i] : null;
check(el, rules[i], message, i);
});
}
});
}
});
}
};
Validator.prototype._setFormSubmit = function() {
var self = this;
$(this.container).on('submit', function(e) {
var setter = self.options.setter;
if (setter) self.options = $.extend(self.options, setter());
var opt = self.options;
var ajax = opt.ajax;
var after = opt.after;
var before = opt.before;
var messages = opt.messages;
var autoClear = opt.autoClear;
var check = self.check.bind(self);
var selectors = self.selectors;
var resetStatus = self._resetStatus.bind(self);
var rules = self.rules;
var afterArg;
if (before) before();
var data, url;
var errors = 0;
$.each(selectors, function(i, el) {
var message = messages ? messages[i] : null;
var result = check(el, rules[i], message, i);
if (typeof result === 'boolean' && !result) {
errors++;
} else {
afterArg = result;
}
});
if (errors) return false;
if (ajax) {
e.preventDefault();
data = $(this).serialize();
url = $(this).attr('action');
$.ajax({
type: 'post',
url: url,
data: data,
success: function() { ajax.success(); },
error: function() { ajax.error(); },
complete: function() { if (after) after(afterArg); }
});
} else {
return true;
}
if (autoClear) {
$(this).find('input').each(function(i, input) {
var $input = $(input);
resetStatus($input);
$input.val('');
});
if (messages) {
messages.forEach(function(message) {
$(message.el).empty();
});
}
}
});
};
/**
* Remove styles from input element
* @param {jQuery} $el - this is an $(input) element
*/
Validator.prototype._resetStatus = function($el) {
if ($el.hasClass('invalid')) $el.removeClass('invalid');
if ($el.hasClass('valid')) $el.removeClass('valid');
};
Validator.prototype.check = function(el, rules, message, ij) {
var $el = $(el);
var j = rules.length;
var matching = false;
var $msg, type, rule, value, regExp, data;
var status, errIndex;
var currentRule;
this._resetStatus($el);
value = $el.val();
/**
* Check checkbox and radio buttons
*/
type = $el.attr('type');
if (type === 'radio' || type === 'checkbox') {
var checked = false;
$.each($el, function(i, el) {
if (el.checked) {
checked = true;
$el = $(el);
}
});
if (!checked) value = '';
}
while ( j-- ) {
rule = rules[j];
if (typeof rule === 'object') {
regExp = filters[rule.filter];
matching = regExp(rule.val, value, $el);
currentRule = rule.filter;
} else {
regExp = filters[rule];
matching = regExp(value);
currentRule = rule;
}
errIndex = j;
if (!matching) break;
}
$msg = this._getMessageContainer(ij);
status = matching ? 'valid' : 'invalid';
data = {
el: $el,
container: $msg,
message: message,
status: status,
errIndex: errIndex,
errRule: currentRule
};
this._printMessage(data);
return matching;
};
Validator.prototype._getMessageContainer = function(i) {
var messages = this.options.messages;
return messages ? $(messages[i].el) : null;
};
Validator.prototype._printMessage = function(options) {
var opt = options;
var $el = opt.el;
var $msg = opt.container;
var status = opt.status;
var message = opt.message;
var errRule = opt.errRule;
var errIndex = opt.errIndex;
var validation = status === 'invalid' ? 'valid' : 'invalid';
var msgstat = message ? message[status] : '';
var text;
if (typeof msgstat === 'object') {
var obj = $.grep(msgstat, function(filterMessage) {
return filterMessage['filter'] === errRule;
});
text = obj[0].text;
} else if (typeof msgstat === 'function') {
msgstat();
} else {
text = msgstat;
}
$el.addClass(status);
if ($msg) {
$msg.removeClass(validation)
.addClass(status)
.html(text);
}
};
$.fn[PLUGIN_NAME] = function (options) {
return this.each(function() {
if (!$.data(this, 'plugin_' + PLUGIN_NAME)) {
$.data(this, 'plugin_' + PLUGIN_NAME,
new Validator(this, options));
}
});
}
}));