-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
684 lines (581 loc) · 24.5 KB
/
index.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
(function () {
var regex = {
username: /^[a-z]+[0-8]*$/i,
// RFC 2822
email: /^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$/,
number: /[0-9]+/,
},
// error messages
defaultMessages = {
// english
en: {
custom: 'Field value should match the regex pattern {0}',
email: 'Please enter a valid email address.',
username: 'Please enter a valid username.',
number: 'Please enter a valid number.',
required: 'This field is required.',
maxlength: 'Please enter at least {0} characters.',
equal: 'Please enter the same value.',
},
// french
fr: {
custom: "La valeur du champs doit correspondre à l'expression régulière {0}",
email: 'Veuillez saisir une adresse e-mail valide.',
username: "Veuillez saisir un nom d'utilisateur valide.",
number: 'Veuillez entrer un nombre valide.',
required: 'Ce champ est requis.',
maxlength: 'Veuillez saisir au moins {0} caractères.',
equal: 'Veuillez saisir la même valeur.',
},
},
// error messages replacing token's regex
tokenRegex = /\{\d+\}/,
showErrors = true,
// default validation events
defaultEvents = ['submit'],
// default messages language
defaultLang = 'en',
// supported validation events
supportedEvents = ['submit', 'input', 'change'],
// supported constraints types
defaultConstraints = ['required', 'match', 'maxlength', 'equal'],
// supported HTML5 validation attributes
html5attributes = [
'required',
'maxlength',
'pattern',
// 'minlength',
// 'min',
// 'max',
],
// HTML5 input types supported
html5inpuTypes = [
'email',
'number',
// 'url',
],
// error message element configs
errorElement = {
tagName: 'span',
css: {
color: 'red',
display: 'block',
},
classes: ['error'],
},
inputErrorBorder = '2px solid red';
var FormValidator = function (form, settings) {
if (!(this instanceof FormValidator)) {
return new FormValidator(form, settings);
}
// The settings object argument is optional and if is defined
// and wasn't an object then an empty object returned
if (!form || settings === null || (typeof settings !== 'undefined' && typeof settings !== 'object')) {
return {};
}
this.form = form instanceof Element ? form : document.querySelector(form);
this.events = (settings && settings.events && this.initEvents(settings.events)) || defaultEvents;
this.showErrors = typeof (settings && settings.showErrors) !== 'undefined' ? settings.showErrors : showErrors;
this.lang = (settings && settings.lang) || defaultLang;
this.errors = {};
this.buildConstraints((settings && settings.constraints) || {});
if (settings) {
typeof settings.submitHandler === 'function' && (this.submitHandler = settings.submitHandler);
typeof settings.invalidHandler === 'function' && (this.invalidHandler = settings.invalidHandler);
}
// disable built-in browser validation
this.form.setAttribute('novalidate', 'novalidate');
};
FormValidator.prototype = {
defaults: {
regex: regex,
messages: defaultMessages,
errorElement: errorElement,
inputErrorBorder: inputErrorBorder,
// errorElementConfigs: errorElement
},
/**
* Initializes and builds the constraints object that holds all the information needed for validation
* @param {Object} constraints
*/
buildConstraints: function (constraints) {
if (constraints && typeof constraints !== 'object') {
return;
}
this.constraints = {};
this.processConstraints(constraints);
this.mergeHTML5Constraints();
this.setErrorMessages();
this.bindEvents();
},
/**
* Process the giving constraints object to respect the default constraints defined by the library
* @param {Object} constraints
*/
processConstraints: function (constraints) {
var ref = this.constraints,
formElements = this.getFormElements();
for (var filedName in constraints) {
if (!Object.prototype.hasOwnProperty.call(constraints, filedName)) {
continue;
}
var formElement = formElements[filedName];
if (!formElement) {
console.warn(
"A form element with the name '" +
filedName +
"' is not found in the form '#" +
this.form.id +
"'."
);
continue;
}
for (var constraintType in constraints[filedName]) {
if (
constraintType === 'messages' ||
!Object.prototype.hasOwnProperty.call(constraints[filedName], constraintType)
) {
continue;
}
var isValidConstraint = true;
// detected unsupported validation constraints types and send a warn to the consoles
if (defaultConstraints.indexOf(constraintType) === -1) {
console.warn("'" + constraintType + "' is unsupported validation constraint type.");
isValidConstraint = false;
}
// handle constraint type value if it defined as a function
var constraintValue = constraints[filedName][constraintType];
if (typeof constraintValue === 'function') {
constraints[filedName][constraintType] = constraintValue.call(this);
}
!isValidConstraint && delete constraints[filedName][constraintType];
}
ref[filedName] = constraints[filedName];
}
},
/**
* Merge the HTML5 validation attributes and input types constraints with the constraints object
* @link https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Constraint_validation
*/
mergeHTML5Constraints: function () {
var ref = this.constraints,
formElements = this.getFormElements();
for (var key in formElements) {
if (!Object.prototype.hasOwnProperty.call(formElements, key)) {
continue;
}
var element = formElements[key],
i = 0;
// handles checkbox/radio inputs required attribute
if (Array.isArray(element)) {
var firstElement = element[0];
element.forEach(function (ele) {
if (ele.name === firstElement.name && ele.required) {
if (!ref[ele.name]) ref[ele.name] = {};
ref[ele.name].required = true;
}
});
} else {
var name = element.name;
// html5 input types
var eleType = element.type;
if (eleType && html5inpuTypes.indexOf(eleType) !== -1) {
if (!ref[name]) ref[name] = {};
ref[name].match = eleType;
}
// handles html5 validation attributes
while (i < html5attributes.length) {
var attr = html5attributes[i];
if (!element.hasAttribute(attr)) {
i++;
continue;
}
if (!ref[name]) {
ref[name] = {};
}
switch (attr) {
case 'required':
ref[name][attr] = true;
break;
case 'maxlength':
var val = element.maxLength;
ref[name][attr] = val && Number.parseInt(val);
break;
case 'pattern':
ref[name].match = new RegExp(element.pattern);
var title = element.title;
if (title && title.length > 0) {
if (typeof ref[name].messages === 'undefined') {
ref[name].messages = {};
}
ref[name].messages.match = title;
}
break;
}
i++;
}
}
}
},
/**
* Sets constraints validation error messages for the constraints object
*/
setErrorMessages: function () {
var ref = this.constraints;
for (var fieldName in ref) {
if (!Object.prototype.hasOwnProperty.call(ref, fieldName)) {
continue;
}
var constraints = ref[fieldName];
// initialize the messages object if not defined
if (typeof constraints.messages === 'undefined') {
constraints.messages = {};
}
// loop througth all the contraints types to define the error messages for each
for (var constraintType in constraints) {
if (
!Object.prototype.hasOwnProperty.call(constraints, constraintType) ||
constraintType === 'messages'
) {
continue;
}
var constraintTypeVal = constraints[constraintType],
messages = constraints.messages,
definedMsg = messages && messages[constraintType],
msg = definedMsg;
// handle if the message was defined and it was a function
if (typeof definedMsg === 'function') {
var defaultMsg =
this.defaults.messages[this.lang][
constraintType === 'match' ? constraintTypeVal : constraintType
];
// call the callback function and pass the default message to it
msg = definedMsg.call(this, defaultMsg);
}
// match: /someregex/
else if (constraintType === 'match' && constraintTypeVal instanceof RegExp) {
msg = this.defaults.messages[this.lang].custom;
}
// match: regexName
else if (constraintType === 'match' && Object.keys(regex).indexOf(constraintTypeVal) !== -1) {
msg = this.defaults.messages[this.lang][constraintTypeVal];
}
// set the default message for all other cases (maxlength, required, equal)
else {
msg = this.defaults.messages[this.lang][constraintType];
}
// if the message contains a '{1}' placeholder then replace it with
// the actual value of the constraint type
if (tokenRegex.test(msg)) {
constraintTypeVal =
constraintType === 'equal' ? constraintTypeVal.substr(1) : constraintTypeVal;
msg = msg.replace(tokenRegex, constraintTypeVal);
}
ref[fieldName].messages[constraintType] = msg;
}
}
},
/**
* Initializes validation events for a FormValidator instance
* @param {Array} events
* @returns {Array}
*/
initEvents: function (events) {
if (!Array.isArray(events) || events.length === 0) {
return defaultEvents;
}
var i = 0,
len = events.length,
ref = [];
while (i < len) {
var ev = events[i];
if (supportedEvents.indexOf(ev) !== -1) {
ref.push(ev);
i++;
continue;
}
console.warn(ev + ' is invalid form validation event.');
i++;
}
return ref;
},
/**
* Binding validation events to an appropriate elements
*/
bindEvents: function () {
var events = this.events;
// bind the form submit event
if (events.indexOf('submit') !== -1) {
this.addEvent(this.form, 'submit', function (e) {
// prevent default submit action
e.preventDefault();
var args = [this.form, e];
if (this.all() === true) {
// if validation successed excutes the submit handler if defined
this.submitHandler ? this.submitHandler.apply(this, args) : this.form.submit();
} else {
// if fails
this.invalidHandler && this.invalidHandler.apply(this, args);
}
});
}
// bind the other events to form fileds
var elementsToValidate = this.getElemetsToValidate(),
events = events.filter(function (ev) {
if (ev !== 'submit') {
return ev;
}
});
this.addEvent(
elementsToValidate,
events,
function (element) {
this.element.call(this, element);
}
);
},
/**
* Validate constraints for a single form element
*/
element: function (element, constraints) {
if (typeof constraints === 'object') {
var newConstraints = {};
newConstraints[element.name] = constraints;
this.buildConstraints(newConstraints);
}
var elementName = element.name,
constraints = this.constraints[elementName],
types = Object.keys(constraints),
messages = constraints.messages,
isValid = true;
// handle required constraint type first
if (types.indexOf('required') !== -1 && !this.getFieldValue(element)) {
this.raiseError.apply(this, [elementName, messages.required]);
// there is no need to continue checking the other constraint since the field value is empty
return !isValid;
}
// handle the other constraint types
if (this.getFieldValue(element)) {
types.forEach(
function (type) {
if (type !== 'required' && type !== 'messages') {
if (this.handle.apply(this, [element, type, constraints[type]]) === true) {
this.raiseError.apply(this, [elementName, messages[type]]);
isValid = false;
}
}
}.bind(this)
);
}
if (isValid === true && this.errors) {
this.removeError.call(this, elementName);
}
return isValid;
},
raiseError: function (elementName, message) {
this.errors[elementName] = message;
this.showErrors && this.mark(elementName, message);
},
removeError: function (elementName) {
delete this.errors[elementName];
this.showErrors && this.unmark(elementName);
},
handle: function (element, constraintName, constraintValue) {
var elementValue = this.getFieldValue(element);
switch (constraintName) {
case 'maxlength':
return elementValue.length >= constraintValue;
case 'match':
regex = constraintValue instanceof RegExp ? constraintValue : this.defaults.regex[constraintValue];
return !regex.test(elementValue);
case 'equal':
return elementValue !== this.getFormElements()[constraintValue.substring(1)].value;
}
},
all: function () {
var elements = this.getElemetsToValidate(),
isValid = true,
i = 0;
while (i < elements.length) {
if (!this.element.call(this, elements[i])) {
isValid = false;
}
i++;
}
return isValid;
},
isValid: function () {
if (this.showErrors) {
this.showErrors = false;
try {
return this.all();
} finally {
this.showErrors = true;
}
}
return this.all();
},
/**
* Gets the correct field form value
* @param {String} element
*/
getFieldValue: function (element) {
if (element.type === 'checkbox' || element.type === 'radio') {
var value,
choices = this.getFormElements()[element.name];
choices.forEach(function (ele) {
if (ele.checked === true) {
value = ele.value;
}
});
return value;
}
return element.value;
},
/**
* Gets only the defined form elements in the constraints object to be validated
* @returns Array<DOM Element>
*/
getElemetsToValidate: function () {
var elements = [],
constraints = this.constraints;
for (var fieldName in constraints) {
if (!Object.prototype.hasOwnProperty.call(constraints, fieldName)) {
continue;
}
var element = this.form[fieldName];
if (element.length > 0) {
var i = 0;
while (i < element.length) {
elements.push(element[i]);
i++;
}
} else {
elements.push(element);
}
}
return elements;
},
/**
* Marks the givig element as invalid
* @param {DOM Object} element
* @param {String|Function} error
*/
mark: function (elementName, error) {
var element = this.getFormElements()[elementName];
if (Array.isArray(element)) {
element = element[element.length - 1];
}
// if the span is already added just update just the text inside
var span = this.getSiblingByClass(element, 'error');
if (span instanceof Element) {
span.textContent = error;
return;
}
// create the span that shows the error message
var elementConfigs = this.defaults.errorElement;
span = document.createElement(elementConfigs.tagName);
elementConfigs.classes.forEach(function (className) {
span.classList.add(className);
});
var styles = elementConfigs.css;
for (var prop in styles) {
var val = styles[prop];
span.style[prop] = val;
}
span.textContent = error;
var border = this.defaults.inputErrorBorder;
border && (element.style.border = border);
element.parentNode.appendChild(span);
},
/**
* Unmark invalid form element
* @param {DOM Object} element
*/
unmark: function (elementName) {
var element = this.getFormElements()[elementName];
if (Array.isArray(element)) {
element.forEach(
function (ele) {
removeErrorFromElement.call(this, ele);
}.bind(this)
);
} else {
removeErrorFromElement.call(this, element);
}
function removeErrorFromElement(ele) {
var span = this.getSiblingByClass(ele, 'error');
if (span instanceof Element) {
span.remove();
// TODO revert to origin style
ele.style.border = '1px solid #999';
}
}
},
/**
* @param {DOM Object} element
* @param {String} className
* @returns {DOM Object|null}
*/
getSiblingByClass: function (element, className) {
var parent = element.parentNode,
childs = parent.childNodes;
for (var i = 0; i < childs.length; i++) {
var e = childs[i];
if (e.nodeType !== 3 && e.classList.contains(className)) {
return e;
}
}
return null;
},
/**
* Gets all the form elements as an object, for each pair the key
* is the input name and the value is the actual DOM element
* @returns {Object}
*/
getFormElements: function () {
var elements = this.form.elements,
res = {};
for (var i = 0, element; (element = elements[i++]); ) {
if (element.type === 'submit' && !element.name) continue;
var val = res[element.name];
if (element.type === 'radio' || element.type === 'checkbox') {
if (!val) {
res[element.name] = [];
}
res[element.name].push(element);
} else {
res[element.name] = element;
}
}
return res;
},
/**
* Simple helper to attaches event handlers to a DOM element/elements
*
* @param {DOM Object|Array<DOM Object>} target
* @param {String|Array<String>} events
* @param {Function} callback
*/
addEvent: function (target, events, callback) {
if (typeof events === 'string') {
var arr = [];
arr.push(events);
events = arr;
}
var self = this;
if (target instanceof Element) {
events.forEach(function (ev) {
target.addEventListener(ev, callback.bind(self), false);
});
} else if (Array.isArray(target)) {
events.forEach(function (ev) {
target.forEach(function (element) {
element.addEventListener(ev, callback.bind(self, element), false);
});
});
}
},
};
window.FormValidator = FormValidator;
})();