forked from via-profit/js-form-validator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjs-form-validator.js
873 lines (646 loc) · 22.8 KB
/
js-form-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
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/**
* Simple Encapsulation Class template
*/
(function (root) {
"use strict";
/**
* Common object params
* @type {Object}
*/
var common = {
publicMethods: ['validate', 'formatString', 'destroy', 'reload', 'getFormHandle', 'getFields', 'showErrors', 'hideErrors'],
className: 'Validator'
},
// main constructor
Protected = function (formHandle, submitCallback, settings) {
formHandle.JsValidator = this;
this.settings = {
// Validation of a current field after the events of "change", "keyup", "blur"
onAir: true,
// Show validation errors
showErrors: true,
// Auto-hide the error messages
autoHideErrors: false,
// Timeout auto-hide error messages
autoHideErrorsTimeout: 2000,
// Language error messages
locale: 'en',
// Object for custom error messages
messages: {},
// Object for custom rules
rules: {},
// classname for error messages
errorClassName: 'error',
// remove spaces from validation field values
removeSpaces: false,
// tracking of new elements
autoTracking: true,
// events list for binding
eventsList: ['keyup', 'change', 'blur']
};
var self = this;
// set handle
this.formHandle = formHandle || null;
// set callback
this.submitCallback = submitCallback || null;
// get fields and rules
this.fields = this.getFields(this.formHandle.querySelectorAll('[data-rule]'));
// apply custom settings
this.applySettings(settings || {});
this.submitCallback = this.submitCallback.bind(this);
this._eventChangeWithDelay = this._eventChangeWithDelay.bind(this);
this._eventChange = this._eventChange.bind(this);
this._eventSubmit = this._eventSubmit.bind(this);
// bind events
this.submitCallback && this.eventsBuilder('addEventListener');
// autotracking for new form elements
this.settings.autoTracking && ('MutationObserver' in window) && new MutationObserver(function(mutationRecords) {
[].forEach.call(mutationRecords, function (mutation) {
switch (mutation.type) {
case 'subtree':
case 'childList':
var reloadFlag = false,
childsArray = [];
[].forEach.call(mutation.addedNodes, function (targetElem) {
childsArray = targetElem.querySelectorAll ? targetElem.querySelectorAll('*') : [];
if (['SELECT', 'INPUT', 'TEXTAREA', 'CHECKBOX', 'RADIOBUTTON'].indexOf(targetElem.tagName) !== -1) {
reloadFlag = true;
};
!reloadFlag && [].forEach.call(childsArray, function (elem) {
if (['SELECT', 'INPUT', 'TEXTAREA', 'CHECKBOX', 'RADIOBUTTON'].indexOf(elem.tagName) !== -1) {
reloadFlag = true;
}
});
});
reloadFlag && self.reload();
break;
}
});
}).observe(this.formHandle, {
childList: true,
subtree: true
});
return this;
};
/**
* Main prototype
* @type {Object}
*/
Protected.prototype = {
messages: {
// English
en: {
required: {
empty: 'This field is required',
incorrect: 'Incorrect value'
},
notzero: {
empty: 'Please make a selection',
incorrect: 'Incorrect value'
},
integer: {
empty: 'Enter an integer value',
incorrect: 'Incorrect integer value'
},
float: {
empty: 'Enter an float number',
incorrect: 'Incorrect float'
},
min: {
empty: 'Enter more',
incorrect: 'Enter more'
},
max: {
empty: 'Enter less',
incorrect: 'Enter less'
},
between: {
empty: 'Enter the between {0}-{1}',
incorrect: 'Enter the between {0}-{1}'
},
name: {
empty: 'Please, enter your name',
incorrect: 'Incorrect name'
},
lastname: {
empty: 'Please, enter your lastname',
incorrect: 'Incorrect lastname'
},
phone: {
empty: 'Please, enter the phone number',
incorrect: 'Incorrect phone number'
},
email: {
empty: 'Please, enter your email address',
incorrect: 'Incorrect email address'
},
length: {
empty: 'Please, Enter a minimum of {0} characters and a maximum of {1}',
incorrect: 'Incorrect. Enter a minimum of {0} characters and a maximum of {1}'
},
minlength: {
empty: 'Please, enter at least {0} characters',
incorrect: 'You have entered less than {0} characters'
},
maxlength: {
empty: 'Please, enter at maximum {0} characters',
incorrect: 'You have entered more than {0} characters'
},
maxfilesize: {
empty: 'The size of one or more selected files larger than {0} {1}',
incorrect: 'The size of one or more selected files larger than {0} {1}'
},
fileextension: {
empty: 'Select file',
incorrect: 'One or more files have an invalid type'
}
}
},
// rules
rules: {
required: function (value) {
return '' !== value;
},
notzero: function (value) {
return parseInt(value, 10) > 0;
},
integer: function (value) {
return new RegExp(/^[0-9]+$/gi).test(value);
},
float: function (value) {
value = value.toString().replace(/\,/, '.');
return this.integer(value) || new RegExp(/^([0-9])+(\.)([0-9]+$)/gi).test(value);
},
min: function (value, params) {
if (this.float(value)) {
return parseFloat(value) >= parseFloat(params[0]);
}
return parseInt(value, 10) >= parseInt(params[0], 10);
},
max: function (value, params) {
if (this.float(value)) {
return parseFloat(value) <= parseFloat(params[0]);
}
return parseInt(value, 10) <= parseInt(params[0], 10);
},
between: function (value, params) {
params[1] = params[1] || 999999;
if (this.float(value)) {
return parseFloat(value) >= parseFloat(params[0]) && parseFloat(value) <= parseFloat(params[1]);
}
if (this.integer(value)) {
return parseInt(value, 10) >= parseInt(params[0], 10) && parseInt(value, 10) <= parseInt(params[1], 10);
}
return false;
},
name: function (value) {
if (value.length > 0 && value.length < 2) {
return false;
}
return new RegExp(/^[a-zA-Z\sа-яА-ЯёЁ\-]+$/g).test(value);
},
lastname: function (value) {
return this.name(value);
},
phone: function (value) {
if (value.replace(/[^0-9]+/gi, '').match(/[0-9]+/gi) && value.replace(/[^0-9]+/gi, '').match(/[0-9]+/gi)[0].length < 6) {
return false;
}
return new RegExp(/^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/g).test(value);
},
email: function (value) {
return new RegExp(/^(("[\w-\s]+")|([\w\-]+(?:\.[\w\-]+)*)|("[\w-\s]+")([\w\-]+(?:\.[\w\-]+)*))(@((?:[\w\-]+\.)*\w[\w\-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i).test(value);
},
length: function (value, params) {
return this.between(value.replace(/\s{2,}/g, ' ').length, params);
},
maxlength: function (value, params) {
return this.max(value.replace(/\s{2,}/g, ' ').length, params);
},
minlength: function (value, params) {
return this.min(value.replace(/\s{2,}/g, ' ').length, params);
},
maxfilesize: function (value, params) {
var i,
l = value.length,
unitsOffset = 1;
switch (params[1].toLowerCase()) {
case 'b':
unitsOffset = 1;
break;
case 'kb':
unitsOffset = 1024;
break;
case 'mb':
unitsOffset = 1048576;
break;
case 'gb':
unitsOffset = 1073741824;
break;
case 'tb':
unitsOffset = 1099511627776;
break;
}
for (i = 0; i < l; i += 1) {
if (parseFloat(value[i]) > (parseFloat(params[0]) * unitsOffset)) {
return false;
}
}
return true;
},
fileextension: function (value, params) {
var i,
a,
l = params.length,
b = value.length,
cmpResC = 0;
for (i = 0; i < l; i += 1) {
for (a = 0; a < b; a += 1) {
if (params[i] === value[a].split('.').pop()) {
cmpResC += 1;
}
}
}
return value.length === cmpResC ? true : false;
}
},
orderFields: function (attrName, attrValue) {
var self = this,
retObj = {};
!!attrName && !!attrValue && Object.keys(this.fields).forEach(function (field) {
if (self.fields[field].handle[attrName] && self.fields[field].handle[attrName] === attrValue) {
retObj[field] = self.fields[field];
}
});
return retObj;
},
_eventSubmit: function (e) {
e.preventDefault();
//hide errors
this.hideErrors(false, true);
//show errors if validation failure
!this.validate() && this.showErrors();
//callback
(this.submitCallback(this.errors || null, this.errors ? false : true) === true) && this.formHandle.submit();
},
_eventChange: function (e) {
var radioBtns,
self = this;
//remove spaces
if (this.settings.removeSpaces && new RegExp(/\s{2,}/g).test(e.target.value)) {
e.target.value = e.target.value.replace(/\s{2,}/g, ' ');
}
//if is radio buttons
if (e.target.type === 'radio') {
//get radio groupe
radioBtns = this.orderFields('name', e.target.name);
Object.keys(radioBtns).forEach(function (btn) {
self.hideErrors(radioBtns[btn].handle);
});
} else {
//hide errors for this
this.hideErrors(e.target);
}
//validate and show errors for this
if (!this.validate(e.target)) {
this.showErrors(e.target);
!this.settings.showErrors && this.submitCallback(this.errors, false);
}
},
_eventChangeWithDelay: function (e) {
var self = this;
if (this.intervalID) {
clearTimeout(this.intervalID);
}
this.intervalID = setTimeout(function () {
self._eventChange.apply(self, [e]);
}, 400);
},
applySettings: function (settings) {
var self = this;
// apply rules
settings.rules && Object.keys(settings.rules).forEach(function(ruleName) {
self.rules[ruleName] = settings.rules[ruleName];
});
// apply messages
settings.messages && Object.keys(settings.messages).forEach(function(locale) {
Object.keys(settings.messages[locale]).forEach(function (ruleName) {
Object.keys(settings.messages[locale][ruleName]).forEach(function (param) {
self.settings.messages[locale] = self.settings.messages[locale] || {};
self.settings.messages[locale][ruleName] = self.settings.messages[locale][ruleName] || {};
self.settings.messages[locale][ruleName][param] = settings.messages[locale][ruleName][param];
});
});
});
// apply other settings
Object.keys(settings).forEach(function (param) {
self.settings[param] = settings[param];
});
return this;
},
getFields: function (fields) {
var retData = {},
rules = [],
params = [];
fields = fields || this.formHandle.querySelectorAll('[data-rule]');
// each fields with data-rule attribute
Object.keys(fields).forEach(function (fieldIndex) {
rules = fields[fieldIndex].getAttribute('data-rule').split('|');
Object.keys(rules).forEach(function (ruleIndex) {
// parse rule
if (rules[ruleIndex].match(/-/gi)) {
params = rules[ruleIndex].split('-');
rules[ruleIndex] = params[0];
params = params.splice(1);
rules[ruleIndex] = [rules[ruleIndex], params];
} else {
rules[ruleIndex] = [rules[ruleIndex], []];
}
});
retData[fieldIndex] = {
name: fields[fieldIndex].getAttribute('name'),
rules: rules,
defaultValue: fields[fieldIndex].getAttribute('data-default'),
handle: fields[fieldIndex],
intervalID: null
};
});
return retData;
},
validate: function (validationField) {
var self = this,
fields = validationField ? this.getFields([validationField]) : this.fields,
result,
ruleName,
params,
defaultValue,
value,
message,
messageType = null;
this.errors = this.errors ? null : this.errors;
Object.keys(fields).forEach(function (n) {
result = true;
// loop rules of this field
fields[n].rules && Object.keys(fields[n].rules).forEach(function (ruleIndex) {
// set rule data
ruleName = fields[n].rules[ruleIndex][0];
params = fields[n].rules[ruleIndex][1];
defaultValue = fields[n].defaultValue;
value = fields[n].handle.value;
switch (fields[n].handle.type) {
case 'checkbox':
!fields[n].handle.checked && (value = '');
break;
case 'radio':
// get radio groupe
var radioBtns = self.orderFields('name', fields[n].handle.name),
checked = false;
Object.keys(radioBtns).forEach(function (i) {
radioBtns[i].handle.checked && (checked = true);
});
if (!checked) {
// add an error to one element
Object.keys(radioBtns).forEach(function (i) {
try {
message = self.settings.messages[self.settings.locale][ruleName].empty;
} catch (e) {
message = self.messages[self.settings.locale][ruleName].empty;
}
});
// set value as for empty rules
value = '';
}
break;
case 'file':
// if the files were selected
if (fields[n].handle.files && fields[n].handle.files.length) {
value = [];
Object.keys(fields[n].handle.files).forEach(function (fileIndex) {
switch (ruleName) {
case 'maxfilesize':
value.push(fields[n].handle.files[fileIndex].size);
break;
case 'fileextension':
value.push(fields[n].handle.files[fileIndex].name);
break;
}
});
}
break;
}
if (result && !(value === '' && !fields[n].rules.join('|').match(/\|{0,1}required\|{0,1}/))) {
// if exist default value and value is eq default
if (result && defaultValue && value !== defaultValue) {
result = false;
messageType = 'incorrect';
// if default value not exist
} else if (result && self.rules[ruleName] && !self.rules[ruleName](value, params)) {
// set message to empty data
if ('' === value) {
result = false;
messageType = 'empty';
// set message to incorrect data
} else {
result = false;
messageType = 'incorrect';
}
}
if (result) {
self.hideErrors(fields[n].handle, true);
} else {
// define errors stack if not exist
self.errors = self.errors || {};
// append error messages
if (ruleName === 'required' && fields[n].rules[1] && fields[n].rules[1][0]) {
ruleName = fields[n].rules[1][0];
messageType = 'empty';
}
try {
try {
message = self.settings.messages[self.settings.locale][ruleName][messageType];
} catch (e) {
message = self.messages[self.settings.locale][ruleName][messageType];
}
} catch (e) {
ruleName = 'required';
message = self.messages[self.settings.locale][ruleName][messageType];
}
// push value into params if params is empty
!params.length && params.push(value);
// add errors
self.errors[n] = {
name: fields[n].name,
errorText: self.formatString(message, params)
};
// call callback if exist
if (!self.submitCallback) {
self.errors[n].handle = fields[n].handle;
}
}
}
});
});
// run callback if callback is exists and not errors or return error data object
if (this.submitCallback) {
return (this.errors) ? false : true;
}
return this.errors || true;
},
hideErrors: function (validationField, removeClass) {
var self = this,
errorDiv;
Object.keys(this.fields).forEach(function (n) {
if ((validationField && validationField === self.fields[n].handle) || !validationField) {
errorDiv = self.fields[n].handle.nextElementSibling;
// remove class error
removeClass && self.fields[n].handle.classList.remove(self.settings.errorClassName);
// remove error element
errorDiv && (errorDiv.getAttribute('data-type') === 'validator-error') && errorDiv.parentNode.removeChild(errorDiv);
}
});
},
showErrors: function (validationField) {
var self = this,
errorDiv,
insertNodeError = function (refNode, errorObj) {
// set error class
refNode.classList.add(self.settings.errorClassName);
// check to error div element exist
if (refNode.nextElementSibling && refNode.nextElementSibling.getAttribute('data-type') === 'validator-error') {
return;
}
// insert error element
if (self.settings.showErrors) {
errorDiv = document.createElement('div');
errorDiv.setAttribute('class', self.settings.errorClassName);
errorDiv.setAttribute('data-type', 'validator-error');
errorDiv.innerHTML = errorObj.errorText;
refNode.parentNode.insertBefore(errorDiv, refNode.nextSibling);
}
};
Object.keys(this.errors).forEach(function (r) {
// show error to specified field
if (validationField) {
Object.keys(self.fields).forEach(function (n) {
(self.fields[n].handle.getAttribute('name') === validationField.getAttribute('name')) && insertNodeError(self.fields[n].handle, self.errors[r]);
});
// show error to all fields
} else {
if (r === '0' || (r > 0 && self.fields[r].name !== self.fields[r - 1].name)) {
insertNodeError(self.fields[r].handle, self.errors[r]);
}
}
});
// auto hide errors
if (this.settings.autoHideErrors) {
// for all fields
if (!validationField) {
if (this.intervalID) {
clearTimeout(this.intervalID);
}
this.intervalID = setTimeout(function () {
self.intervalID = null;
self.hideErrors(false);
}, this.settings.autoHideErrorsTimeout);
// for current field
} else {
if (validationField.intervalID) {
clearTimeout(validationField.intervalID);
}
if (!this.intervalID) {
validationField.intervalID = setTimeout(function () {
validationField.intervalID = null;
self.hideErrors(validationField);
}, this.settings.autoHideErrorsTimeout);
}
}
}
},
/*
* Get Form handle
* @return {element} - Form handle
*/
getFormHandle: function () {
return this.formHandle;
},
/*
* Formatting string. Replace string
* @param {string} string - Source string. Example: "{0} age {1} years."
* @param {array} params - An array of values, which will be replaced with markers. Example: ['Bob', 36]
* @return {string} - Formatted string with replacing markers. Example "Bob age 36 years"
*/
formatString: function (string, params) {
return string.replace(/\{(\d+)\}/gi, function (match, number) {
return (match && params[number]) ? params[number] : '';
});
},
/*
* Destroy validator
*/
destroy: function () {
//hide errors
this.hideErrors(false, true);
// remove events
this.eventsBuilder('removeEventListener');
},
/*
* Reload validator.
* Example 1: reload(function (err, res) {...}, {autoHideErrors: false})
* Example 2: reload({autoHideErrors: false})
* @param {function} [submitCallback] - Submit callback function
* @param {object} [settings] - Settings object
*/
reload: function (submitCallback, settings) {
this.destroy();
//set variables
switch (arguments.length) {
case 2:
this.submitCallback = submitCallback;
this.settings = settings;
break;
case 1:
this.settings = submitCallback;
break;
}
this.fields = this.getFields(this.formHandle.querySelectorAll('[data-rule]'));
this.submitCallback && this.eventsBuilder('addEventListener');
this.applySettings(settings || {});
},
eventsBuilder: function (actionName) {
var self = this;
this.formHandle[actionName]('submit', this._eventSubmit);
// air mode
this.settings.onAir && Object.keys(this.fields).forEach(function (field) {
[].forEach.call(self.settings.eventsList, function (event) {
if (event === 'keyup') {
self.fields[field].handle[actionName](event, self._eventChangeWithDelay);
} else {
self.fields[field].handle[actionName](event, self._eventChange);
}
});
});
}
};
/**
* Encapsulation
* @return {Object} - this handle
*/
root[common.className] = function () {
function construct(constructor, args) {
function Class() {
return constructor.apply(this, args);
}
Class.prototype = constructor.prototype;
return new Class();
}
var original = construct(Protected, arguments),
Publicly = function () {};
Publicly.prototype = {};
[].forEach.call(common.publicMethods, function (member) {
Publicly.prototype[member] = function () {
return original[member].apply(original, arguments);
};
});
return new Publicly(arguments);
};
}(this));