forked from JavaScriptHub/uvalidator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.uvalidator.js
424 lines (369 loc) · 10.9 KB
/
jquery.uvalidator.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
/* eslint-disable */
/*jslint browser: true, white: true*/
(function ($) {
'use strict';
var defaults,
events,
validatorManager;
/**
* Default settings
* @property defaults
* @type Object
*/
defaults = {
validationEvents: {
/**
* Validate on focus out event
* @property {Boolean} validationEvents.focusout
* @default false
*/
focusout: false,
/**
* Validate on focus in event
* @property {Boolean} validationEvents.focusin
* @default false
*/
focusin: false,
/**
* Validate on change event
* @property {Boolean} validationEvents.change
* @default true
*/
change: true,
/**
* Validate on keyup event
* @property {Boolean} validationEvents.keyup
* @default keyup
*/
keyup: true,
/**
* Validate on click event
* @property {Boolean} validationEvents.click
* @default true
*/
click: false,
/**
* Validate on form submit event
* @property {Boolean} validationEvents.submit
* @default true
*/
submit: true
}
};
events = {
FIELD_VALID: 'fieldValid',
FIELD_INVALID: 'fieldInvalid',
FORM_VALID: 'formValid',
FORM_INVALID: 'formInvalid',
FINISH_FORM_VALIDATION: 'finishFormValidation',
START_FORM_VALIDATION: 'startFormValidation',
FINISH_FIELD_VALIDATION: 'finishFieldValidation',
START_FIELD_VALIDATION: 'startFieldValidation'
};
validatorManager = (function () {
var validators = [],
validatorsByName = {},
groupValidators = [],
groupValidatorsByName = {};
return {
createValidator: function createValidator(selector, name, fn, isGroup) {
return {
fn: fn,
name: name,
selector: selector,
isGroup: isGroup
};
},
getValidatorByIndex: function getValidatorByIndex(index, isGroup) {
return isGroup ? groupValidators[index] : validators[index];
},
/**
* @method getValidatorIndexByName
* @param {String} name The index of the validator
* @private
* @return {Integer} Returns the index of the validator with the name. If
* the validator not found, returns -1
*/
getValidatorIndexByName: function getValidatorIndexByName(name, isGroup) {
var index = -1,
byName = isGroup ? groupValidatorsByName : validatorsByName;
if (typeof byName[name] === 'number') {
index = byName[name];
}
return index;
},
/**
* @method getValidatorByName
* @param {String} name Name of the validator
* @private
* @return {Validator,Null} Returns the validator object if it's found or
* null if no such validator
*/
getValidatorByName: function getValidatorByName(name, isGroup) {
var index = validatorManager.getValidatorIndexByName(name, isGroup),
validator = null;
if (index > -1) {
validator = isGroup ? groupValidators[index] : validators[index];
}
return validator;
},
addValidatorMethod: function addValidatorMethod(selector, name, fn) {
var currentIndex, index, validatorObject;
currentIndex = validatorManager.getValidatorIndexByName(name, false);
// overwrites the existing method
index = currentIndex > -1 ? currentIndex : validators.length;
validatorObject = validatorManager.createValidator(selector, name, fn, false);
validatorsByName[name] = index;
validators[index] = validatorObject;
},
addValidatorGroupMethod: function (selector, name, fn) {
var currentIndex, index, validatorObject;
currentIndex = validatorManager.getValidatorIndexByName(name, true);
// overwrites the existing method
index = currentIndex > -1 ? currentIndex : groupValidators.length;
validatorObject = validatorManager.createValidator(selector, name, fn, true);
groupValidatorsByName[name] = index;
groupValidators[index] = validatorObject;
},
len: function (isGroup) {
return isGroup ? groupValidators.length : validators.length;
}
};
}());
function getFieldValue(field) {
return field.is(':checkbox,:radio') ? field.is(':checked') : field.val();
}
function getGroupItemsForField(field) {
var group, form;
group = field.attr('data-validator-group');
form = field[0].form ? $(field[0].form) : field.closest('form');
return form.find('[data-validator-group="' + group + '"]:not(:disabled)');
}
function validateWith(value, field, name, callback, isGroup, form) {
var validator = validatorManager.getValidatorByName(name, isGroup);
if (validator) {
validator.fn.call(form, value, field, callback);
} else {
callback(true);
}
}
function validateField(field, callback, isGroup, form) {
var vl, value, isValid, index, validated, callbackCalled;
field = $(field);
index = 0;
validated = 0;
isValid = true;
callbackCalled = false;
vl = validatorManager.len(isGroup);
if (!isGroup) {
value = getFieldValue(field);
}
function finishValidation(validator) {
var result = {
isValid: isValid,
isGroup: isGroup,
validator: validator.name,
field: field
};
callbackCalled = true;
field.trigger(events.FINISH_FIELD_VALIDATION, field);
// if not matched any validator then in the end the
// index will be eq to vl, so we must call the
// callback by hand
callback(result, field);
}
function onValidate(valid, validator, next) {
if (callbackCalled) {
return;
}
index += 1;
validated += 1;
isValid = isValid && valid;
if (validated < vl && isValid) {
// call next validation
next();
} else {
finishValidation(validator);
}
}
function getOnValidate(validator, validate) {
return function (valid) {
onValidate(valid, validator, validate);
};
}
function validate() {
var validator;
while (index < vl && isValid) {
validator = validatorManager.getValidatorByIndex(index, isGroup);
if (field.is(validator.selector)) {
validator.fn.call(form, value, field, getOnValidate(validator, validate));
} else {
validated += 1;
if (validated >= vl) {
finishValidation(validator);
}
}
index += 1;
}
}
field.trigger(events.START_FIELD_VALIDATION, field);
validate();
}
function triggerFieldEvents(result, field) {
if (result.isValid) {
field.trigger(events.FIELD_VALID, result);
} else {
field.trigger(events.FIELD_INVALID, result);
}
}
/**
* Helper to decide if the validation should run a certain event. It's good
* to handle exceptions, like we don't want to validate on keyup if the
* user just pressed the TAB button.
* @method isAllowedEventValidation
* @private
* @param {Event} e
*/
function isAllowedEventValidation(e) {
var output = true,
which;
if (e.type === 'keyup') {
which = e.which;
// on keyup event don't allow to validate if user presses these
// buttons
// I would use switch/case but jsperf says it's the slowest way to do the comparisons.
// 9: tab, 16: shift, 17: ctrl, 18: alt
if (which === 9 || which === 16 || which === 17 || which === 18) {
output = false;
}
}
return output;
}
function validateForm(form) {
var validationResults, isFormValid, fields, groupFields, groups, groupsLen;
fields = form.find(':input').not('.skip-validation,:disabled,:hidden');
groupFields = fields.filter('[data-validator-group]');
fields = fields.not('[data-validator-group],:button,:submit');
groups = {};
groupsLen = 0;
groupFields.each(function () {
var that = $(this), // caching
group = that.attr('data-validator-group');
if (!groups[group]) {
groups[group] = getGroupItemsForField(that);
groupsLen += 1;
}
});
validationResults = [];
isFormValid = true;
function onValidate(result, field) {
isFormValid = isFormValid && result.isValid;
validationResults.push({
field: field,
isValid: result.isValid,
validator: result.validator,
isGroup: false
});
if (result.isValid) {
field.trigger(events.FIELD_VALID, result);
} else {
field.trigger(events.FIELD_INVALID, result);
}
if (validationResults.length >= (fields.length + groupsLen)) {
form.trigger(events.FINISH_FORM_VALIDATION, form);
if (isFormValid) {
form.trigger(events.FORM_VALID, {results: validationResults});
} else {
/*jslint unparam: true*/
form.trigger(events.FORM_INVALID, {
results: validationResults,
errors: $.makeArray($(validationResults).filter(function (index, item) {
return !item.isValid;
}))
});
/*jslint unparam: false*/
}
}
}
form.trigger(events.START_FORM_VALIDATION, form);
if (!fields.length && !groups.length) {
form.trigger(events.FIELD_VALID, {results: validationResults});
form.trigger(events.FORM_VALID, {results: validationResults});
return;
}
fields.each(function () {
validateField(this, onValidate, false, form);
});
/*jslint unparam: true*/
$.each(groups, function (group, items) {
validateField(items, onValidate, true, form);
});
/*jslint unparam: false*/
}
function bindDelegation(form, settings) {
form = $(form);
form.attr('novalidate', 'novalidate');
$.each(settings.validationEvents, function (name, value) {
if (value) {
form.delegate(':input:not(:button,:submit,[data-validator-group],.skip-validation)',
name, function (e) {
// don't validate on tab keyup
if (!isAllowedEventValidation(e)) {
return;
}
validateField(e.target, triggerFieldEvents, false, form);
});
form.delegate(':input[data-validator-group]', name, function (e) {
// don't validate on tab keyup
if (!isAllowedEventValidation(e)) {
return;
}
var target = $(e.target),
elements;
elements = getGroupItemsForField(target);
validateField(elements, triggerFieldEvents, true, form);
});
}
});
form.submit(function (submitEvent) {
submitEvent.preventDefault();
validateForm(form);
});
}
function UValidator(options) {
var settings = $.extend({}, defaults, options);
return this.each(function () {
bindDelegation(this, settings);
});
}
$.uvalidator = {
addMethod: validatorManager.addValidatorMethod,
addGroupMethod: validatorManager.addValidatorGroupMethod,
events: events,
fieldIsValid: function (field, callback, conf, form) {
var isGroup;
field = $(field);
conf = $.extend({}, defaults, conf);
isGroup = field.is('[data-validator-group]');
if (isGroup) {
field = getGroupItemsForField(field);
}
validateField(field, callback, isGroup, form);
},
validateField: function (field, callback, conf, form) {
$.uvalidator.fieldIsValid(field, function (result, field) {
triggerFieldEvents(result, field);
return callback && callback(result, field);
}, conf, form);
},
validateWith: function (field, method, callback, form) {
var isGroup = field.is('[data-validator-group]');
validateWith(getFieldValue(field), field, method, callback, isGroup, form);
},
validate: function (form) {
validateForm($(form));
}
};
$.fn.uvalidator = UValidator;
}(window.jQuery));
/* eslint-enable */