-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidator.js
More file actions
357 lines (337 loc) · 12.8 KB
/
validator.js
File metadata and controls
357 lines (337 loc) · 12.8 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
/*
jQuery Form Validation for Bootstrap
Author: Timo Fischer
Version: 1.2.1
*/
"use strict";
/**
* Form Validation Class Constructor
* @param form
* @param options
* @constructor
*/
function Validation(form, options) {
this.form = $(form);
this.options = options;
this.regex = {
email: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/m,
date: /^(([0-9]{4}(-[0-9]{1,2}){2})|(([0-9]{1,2}(\s|\.)){2}[0-9]{4}))$/m,
min: /^minLength:\d*$/m,
max: /^maxLength:\d*$/m,
re: /^regex:(.*)$/m,
re_replace: /^regex:/m
};
// check if form is found in DOM
/*
if (this.form.length === 0) {
console.warn('Element could not be find in DOM.');
return;
}
*/
// check if options are in valid format
if (typeof this.options !== "object") {
console.warn('Options have to be a valid json object!');
return;
}
var _this = this;
// on form submit
this.form.on('submit', function (e) {
e.preventDefault();
// validate form
_this.validate();
_this.validate_on_change();
});
// on form reset
this.form.on('reset', function (e) {
e.preventDefault();
// reset form
_this.reset();
});
// on valid form
this.form.on('is-valid', function (e) {
// remove error message
_this.removeErrorMessage();
// check submit option; default: true
if (typeof _this.options.submitOnValid === "undefined" || _this.options.submitOnValid === true) {
// submit form
_this.form[0].submit();
}
});
// on invalid form
this.form.on('is-invalid', function (e) {
// check show error message; default: true
if (typeof _this.options.showErrorMessage === "undefined" || _this.options.showErrorMessage === true) {
// show error message
_this.showErrorMessage();
}
});
}
/**
* Validate Form
*/
Validation.prototype.validate = function () {
// form status (valid or invalid)
var has_errors = false;
// for each field in options
for (var i = 0; i < this.options.fields.length; i++) {
var field = this.options.fields[i];
var _this = this;
// get all form form-group classes
this.form.find('.form-group').each(function () {
var group = $(this);
// get input or select
var input = $(this).find('input, select');
// check if input is disabled
if (typeof input.attr("disabled") !== "undefined" && input.attr("disabled") !== false) {
// skip this field
return true;
}
// check if inout is valid
if (input.length !== 0) {
// compare input name and field name
if (input.attr('name') === field.name) {
// check input for error
_this.check(input, field.rule, function (error) {
if (error === true) {
// form has error
has_errors = true;
// show error
_this.showError(group);
// check if field options has prompt message
if (typeof field.rule.prompt !== "undefined") {
// display prompt message
_this.showPrompt(group, field.rule.prompt);
}
} else {
// remove error from field
_this.removeError(group);
// remove prompt message
_this.removePrompt(group);
// check if field options showSuccess is undefined or false
if (field.rule.showSuccess !== "undefined" && field.rule.showSuccess !== false) {
// default: show success status
_this.showSuccess(group);
}
}
});
}
}
});
}
// check if form has error
if (has_errors === true) {
// trigger 'is-invalid' on form
this.form.trigger('is-invalid');
} else { // field is valid
// trigger 'is-valid' on form
this.form.trigger('is-valid');
}
};
/**
* Validate form field on change
*/
Validation.prototype.validate_on_change = function () {
var _this = this;
this.form.find('.form-group').each(function () {
var group = $(this);
// get input or select
var input = $(this).find('input, select');
// check if input is disabled
if (typeof input.attr("disabled") !== "undefined" && input.attr("disabled") !== false) {
// skip this field
return true;
}
input.on('change input', function () {
for (var i = 0; i < _this.options.fields.length; i++) {
var field = _this.options.fields[i];
if (field.name === input.attr('name')) {
_this.check(input, field.rule, function (error) {
if (error === true) {
// show error
_this.showError(group);
// check if field options has prompt message
if (typeof field.rule.prompt !== "undefined") {
// display prompt message
_this.showPrompt(group, field.rule.prompt);
}
} else {
// remove error from field
_this.removeError(group);
// remove prompt message
_this.removePrompt(group);
// check if field options showSuccess is undefined or false
if (field.rule.showSuccess !== "undefined" && field.rule.showSuccess !== false) {
// default: show success status
_this.showSuccess(group);
}
}
});
}
}
});
});
};
/**
* Check field if rule applies
* @param input
* @param rule
* @param _callback
*/
Validation.prototype.check = function (input, rule, _callback) {
var error = false;
if (input.attr("type") === "checkbox" || input.attr("type") === "radio") {
// check if field rule type is checked
if (rule.type === "checked") {
// get all input fields
var input_fields = document.getElementsByName(input.attr('name'));
// set error to true
error = true;
// for each input field
for (var _i = 0; _i < input_fields.length; _i++) {
// check if at least one field for name is checked
if (input_fields[_i].checked === true) {
error = false;
}
}
}
} else { // input is no checkbox or radio
// trim input value
var val = input.val().trim();
// on field rule type: required
if (rule.type === "required") {
// check if value is empty string
if (val.length === 0) {
// field is invalid
error = true;
}
} else if (rule.type === "email") { // on field rule type: email
// check email regex for valid email format
if (!this.regex.email.test(val)) {
// field is invalid
error = true;
}
} else if (rule.type === "date") {
var date_format_1 = new Date(val);
var data_format_2 = Date.parse(val.replace('.', ' '));
// check if date has "invalid date" format or does not match date regex
if (!this.regex.date.test(val) || isNaN(date_format_1.getTime()) || isNaN(data_format_2)) {
error = true;
}
} else if (this.regex.min.test(rule.type)) { // on field rule type: minLength
// get string length after "minLength:"
var l = parseInt(rule.type.replace('minLength:', ''));
// check if value is shorter than passed length
if (val.length < l) {
// field is invalid
error = true;
}
} else if (this.regex.max.test(rule.type)) { // on field rule type: maxLength
// get string length after "maxLength:"
var l = parseInt(rule.type.replace('maxLength:', ''));
// check if value is longer than passed length or empty
if (val.length > l || val.length === 0) {
// field is invalid
error = true;
}
} else if (this.regex.re.test(rule.type)) { // on field rule type: regex
// get regex after "regex:"
var sub_str = rule.type.replace(this.regex.re_replace, '');
var re = new RegExp(sub_str, "g");
// check if field matches passed regex
if (!re.test(val)) {
// field is valid
error = true;
}
}
}
return _callback(error);
};
/**
* Reset Form
*/
Validation.prototype.reset = function () {
var _this = this;
// for each form-group in form
this.form.find('.form-group').each(function () {
var group = $(this);
var input = $(this).find('input, select');
if (input.length !== 0) {
// clear input values
input.val('');
input.prop('checked', false);
// remove error, success and prompt
_this.removeError(group);
_this.removeSuccess(group);
_this.removePrompt(group);
_this.removeErrorMessage();
}
});
};
// show error on form-group
Validation.prototype.showError = function (field) {
field.removeClass(typeof this.options.errorGroupClass !== "undefined" ? this.options.errorGroupClass : 'has-success');
field.addClass(typeof this.options.errorGroupClass !== "undefined" ? this.options.errorGroupClass : 'has-error');
};
// remove error from form-group
Validation.prototype.removeError = function (field) {
field.removeClass(typeof this.options.errorGroupClass !== "undefined" ? this.options.errorGroupClass : 'has-error');
// remove validation help-block from field
field.find('div.help-block[data-validation]').remove();
};
// show success on form-group
Validation.prototype.showSuccess = function (field) {
field.removeClass(typeof this.options.errorGroupClass !== "undefined" ? this.options.successGroupClass : 'has-error');
field.addClass(typeof this.options.successGroupClass !== "undefined" ? this.options.successGroupClass : 'has-success');
};
// remove success from form-group
Validation.prototype.removeSuccess = function (field) {
field.removeClass(typeof this.options.successGroupClass !== "undefined" ? this.options.successGroupClass : 'has-success');
};
// append prompt message to form-group
Validation.prototype.showPrompt = function (field, prompt) {
// search for help-block
var block = field.find('div.help-block');
// create validation prompt
var helper = '<div class="help-block" data-validation>' + prompt + '</div>';
if (block.length === 0) {
// add help-block to field
field.append(helper);
} else {
// hide default help-block
block.hide();
// add validation help-block to field
field.append(helper);
}
};
// remove prompt message from form-group
Validation.prototype.removePrompt = function (field) {
// remove validation help-block
field.find('div.help-block[data-validation]').remove();
// show default help-block
field.find('div.help-block').show();
};
// show error message in alert box
Validation.prototype.showErrorMessage = function () {
var message = "";
// check if errorMessageText is undefined
if (typeof this.options.errorMessageText === "undefined") {
// display default text
message = "Please check the fields below.";
} else {
// add custom text
message = this.options.errorMessageText;
}
// create alert-box
var alert = '<div class="alert alert-danger" id="validationErrorMsg">' +
'<p>' + message + '</p>' +
'</div>';
// place alert box on top of form
if (this.form.find('#validationErrorMsg').length === 0) {
this.form.prepend(alert);
}
};
// remove error message
Validation.prototype.removeErrorMessage = function () {
// remove
$('#validationErrorMsg').remove();
};