-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtiny-validator.js
More file actions
557 lines (491 loc) · 20 KB
/
tiny-validator.js
File metadata and controls
557 lines (491 loc) · 20 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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
Validator
Author: Forlooper
To Do:
Usage:
- Insert js files to page
- get validator object
- do any config
- Add rules
- add placeholder for notifications
*/
/* START: VALIDATOR CLASS */
Validator = function() {}
Validator.allowCommasInNumbers = true;
Validator.rules = {
'required': {
text: '{label} is required.',
logic: function(value) {
return (value == null || value == '' ? false : true);
}
},
'email': {
text: '{label} must be a valid email address.',
logic: /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i
},
'integer': {
text: '{label} must be an integer.',
logic: /^[\-]?\d+$/
},
'decimal': {
text: '{label} must be a decimal',
logic: /^[\-]?[\d]+(\.\d*)?$/
},
'positive': {
text: '{label} must be positive',
logic: /^[\d]+(\.\d*)?$/
},
'negative': {
text: '{label} must be negative',
logic: /^\-[\d]+(\.\d*)?$/
},
'date': {
text: '{label} must be a date of format dd/mm/yyyy',
logic: /^(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/](19|20)\d\d$/
},
'min': {
text: '{label} must be at least {param1}',
logic: function(value, param1) {
value = Validator.number(value);
if (value == null)
return false;
return (value >= param1 ? true : false);
}
},
'max': {
text: '{label} must be at most {param1}',
logic: function(value, param1) {
value = Validator.number(value);
if (value == null)
return false;
return (value <= param1 ? true : false);
}
},
'range': {
text: '{label} must be between {param1} and {param2}',
logic: function(value, param1, param2) {
value = Validator.number(value);
if (value == null)
return false;
return (value >= param1 && value <= param2 ? true : false);
}
},
'minSize': {
text: '{label} must have at least {param1} character(s)',
logic: function(value, param1) {
value += '';
return (value.length >= param1 ? true : false);
}
},
'maxSize': {
text: '{label} must have at most {param1} character(s)',
logic: function(value, param1) {
value += '';
return (value.length <= param1 ? true : false);
}
},
'minDate': {
text: '{label} must be {param1} or later',
logic: function(value, param1) {
var vDate = Validator.parseDate(value);
var pDate = Validator.parseDate(param1);
if (isNaN(vDate) || isNaN(pDate))
return false;
return (vDate - pDate >= 0 ? true : false);
}
},
'maxDate': {
text: '{label} must be {param1} or later',
logic: function(value, param1) {
var vDate = Validator.parseDate(value);
var pDate = Validator.parseDate(param1);
if (isNaN(vDate) || isNaN(pDate))
return false;
return (vDate - pDate <= 0 ? true : false);
}
},
'decimalPlaces': {
text: '{label} must have {param1} place(s)',
logic: function(value) {
//check if value is a number, round to x places
//if value is not a number, return false
}
},
'url': {
text: '{label} must be a valid URL',
logic: /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i
},
'phone': {
text: '{label} must be a valid phone number',
logic: /^[+]?[\d]{6}$/
}
} //rules
/**
* Converts a number string to a Number type
* @param {{}} parmas - parameters
* @example new Validator({form: $('#myForm'));
*/
Validator.Engine = function(params) {
params = Lib.setDefaultParams(params, {
'$form': null,
'$notificationArea': $('body'),
'skipDisabledControl': true,
'submitHandler': null
})
this.$form = params.$form; //required, so we can handle submit event
this.$notificationArea = params.$notificationArea; //if not supplied, we send display notifications at top of the page
this.skipDisabledControls = params.skipDisabledControls;
this.submitHandler = params.submitHandler;
this.errorFieldCssClass = 'tiny-validator_errorField';
this.errorMessageCssClass = 'tiny-validator_errorMsg';
this.registry = [];
this.failedRegistry = [];
if (this.submitHandler == null) {
var thisValidator = this;
this.$form.submit(function() {
if (thisValidator.validate()) {
return true;
}
else {
return false;
}
});
}
else
this.$form.submit(params['submitHandler']);
}
/**
* Converts a number string to a Number type
* @param {String} value - the number string
* @example Validator.number('123,000');
*/
Validator.number = function(value) {
var properNum;
//remove any commas that exists
value += ''; //make it a string
if (Validator.allowCommasInNumbers) {
properNum = value.replace(/,/g, '');
}
if (properNum == '') //isNaN('') or isNaN(' ') or isNaN(' '), etc, == false (strange indeed!)
return null;
if (isNaN(properNum))
return null;
return Number(properNum);
}
/**
* Converts a date string into a Date object
* @param {String} dateStr - a date string
* @example Validator.parseDate('31/10/2012');
*/
Validator.parseDate = function(dateStr) {
var parts = dateStr.split("/");
// new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
var d = new Date(Number(parts[2]), Number(parts[1])-1, Number(parts[0]), 0, 0, 0, 0); // months are 0-based
return Date.parse(d);
}
/**
* Converts a Date object into a string of format dd/mm/yyyy
* @param {Date} date - Date object
* @example Validator.date2Str(new Date());
*/
Validator.date2Str = function(date) {
return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();
}
/**
* Define rules for validations here.
* Keys:
* - text => the error message. {label} will be replaced by the control label
* - logic => the logic that's run to return if the value being validated is valid or not; logic can be either a regular expression or an inline function returning true or false
*/
Validator.Engine.prototype = {
/**
* Add a remove rule from a control
* @param {String} $ctrl - the control ID of the field
* @param {String} rule - the rule to add to this field
*/
remove: function($ctrl, rule) {
for (var i=0; i < this.registry.length; i++) {
if ($ctrl.prop('id') == this.registry[i].$ctrl.prop('id') && rule == this.registry[i].rule) {
this.registry.splice(i, 1);
return true;
}
}
return false; //didn't remove anything
},
/**
* Add a validation rule to a text field
* @param {String} $ctrl - the control ID of the field
* @param {String} label - the label of the control
* @param {String} rule - the rule to add to this field
* @param {[]} params - an array of string params that will be passed to the rule (min: 0 parmas, max: 3 params)
* @example see overview
*/
textField: function($ctrl, label, rule, params) {
if (typeof params == 'undefined' || params == null) {
params = [];
}
this.registry.push({
type: 'text',
$ctrl: $ctrl,
label: label,
rule: rule,
params: params
});
},
/**
* Add a validation rule to a select field
* Only the 'required' rule is supported if select control allows multiple selections
* @param {String} $ctrl - the control ID of the field
* @param {String} label - the label of the control
* @param {String} rule - the rule to add to this field
* @param {[]} params - an array of string params that will be passed to the rule (min: 0 parmas, max: 3 params)
* @example see overview
*/
selectField: function($ctrl, label, rule, params) {
if (typeof params == 'undefined' || params == null) {
params = [];
}
this.registry.push({
type: 'select',
$ctrl: $ctrl,
label: label,
rule: rule,
params: params
});
},
/**
* Add a validation rule to a checkbox group
* Only the 'required' rule is supported for checkboxes
* @param {String} $ctrl - the control ID of the field
* @param {String} label - the label of the control
* @param {String} rule - the rule to add to this field
* @param {[]} params - an array of string params that will be passed to the rule (min: 0 parmas, max: 3 params)
* @example see overview
*/
checkboxes: function($ctrl, label, rule, params) {
if (typeof params == 'undefined' || params == null) {
params = [];
}
this.registry.push({
type: 'checkbox',
$ctrl: $ctrl,
label: label,
rule: rule,
params: params
});
},
/**
* Add a validation rule to a radio button group
* @param {String} $ctrl - the control ID of the field
* @param {String} label - the label of the control
* @param {String} rule - the rule to add to this field
* @param {[]} params - an array of string params that will be passed to the rule (min: 0 parmas, max: 3 params)
* @example see overview
*/
radioButtons: function($ctrl, label, rule, params) {
if (typeof params == 'undefined' || params == null) {
params = [];
}
this.registry.push({
type: 'radio',
$ctrl: $ctrl,
label: label,
rule: rule,
params: params
});
},
/**
* Creates and displays an error message
* @param {String} msg - the error message
* @example Validator.errorMessage("Title is required");
*/
errorMessage: function(msg) {
View.notify(msg, {'$notificationArea': this.$notificationArea, 'addClass': this.errorMessageCssClass});
},
/**
* The validation engine. Designed to be called by the system.
* @example Validator.validate();
*/
validate: function() {
//clear the failed registry
this.failedRegistry = [];
//clear the error messages too
$('.' + this.errorMessageCssClass).remove();
var errorMsgs = [];
//unhighlight all error fields if any
this.$form.find('.' + this.errorFieldCssClass).removeClass(this.errorFieldCssClass);
for (var i=0; i < this.registry.length; i++) {
var item = this.registry[i];
var type = item['type'];
var $ctrl = item['$ctrl'];
var label = item['label'];
var rule = item['rule'];
var params = item['params'];
var isAFailedCtrl = false;
if (jQuery.inArray($ctrl, this.failedRegistry) != -1) {
isAFailedCtrl = true;
}
if (isAFailedCtrl)
continue;
if (this.skipDisabledControls && $ctrl.prop('disabled') == true)
continue;
/*
* The following section is split into two subsections, one handling non-table fields and the other handling table fields
* Each subsection has the following structure:
* - get the value of a field
* - run the rule logic on the field
* - handle the validation result
*/
/* START: GET CONTROL VALUE */
var value = null;
switch(type) {
case 'text':
value = $ctrl.val();
break;
case 'checkbox':
var $boxes = $ctrl;
$boxes.each(function() {
if ($(this).prop('checked') == true) {
if (value == null) {
value = []; //since this is a checkbox group, values are captured in an array rather than as single string
}
value.push($(this).val());
}
});
break;
case 'select':
value = $ctrl.val();
break;
case 'radio':
var $buttons = $ctrl;
$buttons.each(function() {
if ($(this).prop('checked') == true) {
value = $(this).val();
return false;
}
});
break;
}
/* END: GET CONTROL VALUE */
/* START: APPLY RULE LOGIC */
var logic = Validator.rules[rule]['logic'];
var result = false;
if (typeof logic == 'function') {
result = logic(value, params[0], params[1], params[2]);
}
else {
result = logic.test(value);
}
/* END: APPLY RULE LOGIC */
/* START: HANDLE VALIDATION RESULT */
/*
* If validation fails, add the field to the failed registry so that any remaining
* validations for it will be skipped; add a error css class to the field, and generate
* and add the error message to the array so that all error messages are later inserted
* to the page at once
*/
if (result == false) {
var text = Validator.rules[rule]['text'];
if (typeof params[0] != 'undefined')
text = text.replace(/{param1}/g, params[0]);
if (typeof params[1] != 'undefined')
text = text.replace(/{param2}/g, params[1]);
if (typeof params[2] != 'undefined')
text = text.replace(/{param3}/g, params[2]);
//if control is group of radio buttons or checkboxes, scroll it to the first item of the group
switch(type) {
case 'radio':
case 'checkbox':
text = text.replace(/{label}/g, '<a href="javascript: void(0)" onclick="View.scrollToElement(\'' + $ctrl.first().prop('id') + '\')">' + label + '</a>');
break;
default:
text = text.replace(/{label}/g, '<a href="javascript: void(0)" onclick="View.scrollToElement(\'' + $ctrl.prop('id') + '\')">' + label + '</a>');
break;
}
this.failedRegistry.push($ctrl);
//only add class if it's not radio button group or checkboxes
if (type != 'checkbox' && type != 'radio')
$ctrl.addClass(this.errorFieldCssClass);
errorMsgs.push(text);
}
else {
$ctrl.removeClass(this.errorFieldCssClass);
}
/* END: HANDLE VALIDATION RESULT */
}
/* START: DISPLAY ERROR NOTIFICATIONS AND RETURN VALIDATE METHOD RESULT */
if (errorMsgs.length > 0) {
$('body').animate({
scrollTop: this.$notificationArea.position().top
}, 'slow');
while ( (msg = errorMsgs.pop()) != null) {
this.errorMessage(msg);
}
return false;
}
else {
return true;
}
/* END: DISPLAY ERROR NOTIFICATIONS AND RETURN VALIDATE METHOD RESULT */
}
}
/* END: VALIDATOR CLASS */
/* START: LIB CLASS */
Lib = function () {
};
/*
super imposes supplied parameters into a function/method so we have all parameters and those not supplied will be using default values
*/
Lib.setDefaultParams = function (suppliedParams, defaultParams) {
for (var key in suppliedParams) {
defaultParams[key] = suppliedParams[key];
}
suppliedParams = defaultParams;
return suppliedParams;
}
/* END: LIB CLASS */
/* START: VIEW CLASS */
function View() {
}
View.notificationCount = 0;
View.notify = function (msg, params) {
params = Lib.setDefaultParams(params, {
$notificationArea: $('body'),
highlight: true,
addClass: null
});
//create div
var $newDiv = $('<div class="notification">');
$newDiv.prop('id', 'view_notification_' + View.notificationCount);
$newDiv.css('text-align', 'center');
if (params['addClass'] != null)
$newDiv.addClass(params['addClass']);
View.notificationCount++;
//add msg to div
$newDiv.html(msg);
//prepend div to
params.$notificationArea.prepend($newDiv);
if (params['highlight'])
$newDiv.effect("highlight", {}, 10000);
}
/**
* highlight: Highlight the element after scrolling to it
*/
View.scrollToElement = function (ctrlId, options) {
if (typeof options == 'undefined' || options == null) {
options = {
highlight: false,
highlightDuration: 1000
};
}
if (typeof options['highlightDuration'] == 'undefined' || options['highlightDuration'] == null) {
options['highlightDuration'] = 1000;
}
//check if ctrl is in tabs, if it is, activate that tab first
$('body').animate({
scrollTop: $('#' + ctrlId).position().top
}, 'slow', function () {
$('#' + ctrlId).effect("highlight", {}, options['highlightDuration']);
});
}
/* START: VIEW CLASS */