-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (106 loc) · 4.6 KB
/
index.js
File metadata and controls
140 lines (106 loc) · 4.6 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
(function (global) {
'use strict';
function DCValidator(formObj) {
if (!formObj.form) throw new Error(ERRORS.EMPTY_FORM_ID);
if (!formObj.onError || !formObj.offError) throw new Error(ERRORS.ON_ERROR_EMPTY_WARNING);
if (!formObj.onFormValid || !formObj.onFormInvalid) throw new Error(ERRORS.ON_VALID_EMPTY_WARNING);
this.form = document.querySelector(formObj.form);
this.inputs = this.form.querySelectorAll('[data-rule]');
this.submit = this.form.querySelector('[type="submit"]');
this.errorListeners = formObj.errorListeners || ["blur"];
if (!this.inputs.length) console.warn(ERRORS.NO_RULES_FOUND);
if (this.submit.length > 1) throw new Error(ERRORS.MULTIPLE_SUBMIT);
this.onError = function(input) {
formObj.onError ? formObj.onError(input) : null;
};
this.offError = function(input) {
formObj.offError ? formObj.offError(input) : null;
};
this.onFormValid = function() {
formObj.onFormValid ? formObj.onFormValid(this.form, this.submit) : null;
};
this.onFormInvalid = function() {
formObj.onFormInvalid ? formObj.onFormInvalid(this.form, this.submit) : null;
};
this.onSubmit = function() {
formObj.onSubmit ? formObj.onSubmit(this.form, this.submit) : null;
};
this.rules = this.getCoreRules();
if (formObj.rules) this.setCustomRules(formObj.rules);
var self = this;
if (formObj.listeners && formObj.listeners.submit) delete formObj.listeners.submit;
var listeners = formObj.listeners || ["blur", "keyup"];
if (formObj.listeners && !Array.isArray(formObj.listeners)) throw new Error(ERRORS.WRONG_LISTENERS_PATTERN);
if (formObj.errorListeners && !Array.isArray(formObj.errorListeners)) throw new Error(ERRORS.WRONG_LISTENERS_PATTERN);
this.inputs.forEach(function(input) {
listeners.forEach(function(listener) {
input.addEventListener(listener, function() {
self.validate(listener);
});
});
});
this.form.addEventListener("submit", function(e) {
e.preventDefault();
e.stopPropagation();
if (!self.validate("submit")) return;
if (!formObj.onSubmit) {
self.form.submit();
} else {
self.onSubmit(e);
}
});
}
DCValidator.prototype.setCustomRules = function(rules) {
for (var rule in rules) {
this.rules[rule] = rules[rule];
}
};
DCValidator.prototype.getCoreRules = function() {
return {
email: function(input) {
return /\S+@\S+\.\S+/.test(input.value);
}
};
};
DCValidator.prototype.isValid = function(input, listener) {
var rule = input.getAttribute("data-rule");
if (rule.length && !this.rules[rule]) throw new Error(ERRORS.MISSING_RULE + "\"" + rule + "\"");
var valid = !rule || this.rules[rule](input);
if (this.errorListeners.includes(listener) || listener == "submit") {
this[valid ? "offError" : "onError"](input);
}
return valid;
};
DCValidator.prototype.validate = function(listener) {
var self = this;
var allInputsAreValid = true;
this.inputs.forEach(function(input) {
if(!self.isValid(input, listener)) allInputsAreValid = false;
});
this[allInputsAreValid ? "onFormValid" : "onFormInvalid"]();
return allInputsAreValid;
};
var ERRORS = {
EMPTY_FORM_ID: "Inform the Form id",
NO_RULES_FOUND: "No rules found. Form always will return true.",
MULTIPLE_SUBMIT: "Multiple submit found.",
MISSING_RULE: "Missing Rule on the rules instance",
WRONG_LISTENERS_PATTERN: "Listeners should be an array",
ON_ERROR_EMPTY_WARNING: "Inform \"onError\" and \"offError\" functions on Validator instance.",
ON_VALID_EMPTY_WARNING: "Inform \"onFormValid\" and \"onFormInvalid\" functions on Validator instance.",
};
// AMD support
if (typeof define === 'function' && define.amd) {
define(function () {
return DCValidator;
});
}
else if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = DCValidator;
}
exports.DCValidator = DCValidator;
} else {
global.DCValidator = DCValidator;
}
})(this);