-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvalidation.js
173 lines (161 loc) · 5.15 KB
/
validation.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
// Generated by CoffeeScript 1.4.0
(function() {
$(document).ready(function() {
/*
RegEx
*/
var addErrorStyle, allinputs, choices, code, codes, email, emailRegEx, emails, emptyRegEx, input, inputs, number, numberRegEx, numbers, postalCodeRegEx, removeErrorStyle, select, selects, validateChoiceSelect, validateForm, validateInputs, validateSelect, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _len5, _m, _n;
emailRegEx = new RegExp(/^((?!\.)[a-z0-9._%+-]+(?!\.)\w)@[a-z0-9-\.]+\.[a-z.]{2,5}(?!\.)\w$/i);
emptyRegEx = new RegExp(/[-_.a-zA-Z0-9]{3,}/);
numberRegEx = new RegExp(/^[0-9]{3,}$/);
postalCodeRegEx = new RegExp(/^[A-Z]{1}[0-9]{1}[A-Z]{1} [0-9]{1}[A-Z]{1}[0-9]{1}/);
/*
Arrays of inputs, by types
*/
inputs = [];
emails = [];
codes = [];
selects = [];
choices = [$("#premier-choix"), $("#deuxieme-choix"), $("#troisieme-choix"), $("#quatrieme-choix")];
numbers = [];
/*
Fetching and sorting all form inputs
*/
allinputs = $(".validate").filter(":input");
for (_i = 0, _len = allinputs.length; _i < _len; _i++) {
input = allinputs[_i];
if ($(input).hasClass("text")) {
inputs.push($(input));
}
if ($(input).hasClass("email")) {
emails.push($(input));
}
if ($(input).hasClass("code")) {
codes.push($(input));
}
if ($(input).hasClass("select")) {
selects.push($(input));
}
if ($(input).hasClass("number")) {
numbers.push($(input));
}
}
/*
Inputs onblur validation
*/
for (_j = 0, _len1 = inputs.length; _j < _len1; _j++) {
input = inputs[_j];
input.blur(function() {
return validateInputs($(this), emptyRegEx);
});
}
/*
Email onblur validation
*/
for (_k = 0, _len2 = emails.length; _k < _len2; _k++) {
email = emails[_k];
email.blur(function() {
return validateInputs($(this), emailRegEx);
});
}
/*
Postal Code onblur validation
*/
for (_l = 0, _len3 = codes.length; _l < _len3; _l++) {
code = codes[_l];
code.blur(function() {
return validateInputs($(this), postalCodeRegEx);
});
}
/*
Selects onchange validation
*/
for (_m = 0, _len4 = selects.length; _m < _len4; _m++) {
select = selects[_m];
select.change(function() {
return validateSelect($(this));
});
}
/*
Numbers onblur validation
*/
for (_n = 0, _len5 = numbers.length; _n < _len5; _n++) {
number = numbers[_n];
number.blur(function() {
return validateInputs($(this), numberRegEx);
});
}
validateForm = function() {
var badFields, valid;
$.extend(badFields = [], validateInputs(inputs, emptyRegEx), validateInputs(emails, emailRegEx), validateInputs(codes, postalCodeRegEx), validateSelect(selects), validateInputs(numbers, numberRegEx), validateChoiceSelect(choices));
if (badFields.length === 0) {
valid = true;
} else {
valid = false;
}
return valid;
};
validateInputs = function(inputs, regex) {
var error, _len6, _o;
error = [];
for (_o = 0, _len6 = inputs.length; _o < _len6; _o++) {
input = inputs[_o];
if (regex.test($(input).val())) {
removeErrorStyle(input);
} else {
error.push($(input).attr("id"));
addErrorStyle(input);
}
}
return error;
};
validateSelect = function(selects) {
var error, _len6, _o;
error = [];
for (_o = 0, _len6 = selects.length; _o < _len6; _o++) {
select = selects[_o];
if ($(select).val() !== "0") {
removeErrorStyle(select);
} else {
error.push($(select).attr("id"));
addErrorStyle(select);
}
}
return error;
};
validateChoiceSelect = function(choices) {
var choice, current, error, verif, _len6, _len7, _o, _p;
error = [];
for (_o = 0, _len6 = choices.length; _o < _len6; _o++) {
choice = choices[_o];
current = choice;
for (_p = 0, _len7 = choices.length; _p < _len7; _p++) {
verif = choices[_p];
if ($(current).attr("id") === $(verif).attr("id") || $(current).val() !== $(verif).val()) {
} else {
error.push($(current).attr("id"));
$("#error-choice").html(errorMessages['choices']);
}
}
}
if (error.length === 0) {
$("#error-choice").html("");
}
return error;
};
/*
Error Styling, I changed the border of the input and put an error message within a span in the label of the same input, it's opt to you.
*/
addErrorStyle = function(element) {
$(element).addClass('form-error');
return $(element).prev('label').find('.error-message').html(errorMessages[$(element).attr("id")]);
};
removeErrorStyle = function(element) {
$(element).removeClass('form-error');
return $(element).prev('label').find('.error-message').html("");
};
return $('.validate-form').submit(function() {
return validateForm();
});
});
}).call(this);