-
Notifications
You must be signed in to change notification settings - Fork 8
/
tinyvalidation.js
102 lines (87 loc) · 3.63 KB
/
tinyvalidation.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
(function ($) {
$.fn.tinyValidation = function (options) {
var defaultOptions = {
immediateValidation: false,
disableSubmit: true,
validateOnBlur: true,
validateOnKeyUp: false,
errorClass: 'error', // added to inputs with errors
validClass: 'valid', // added to valid inputs
onValid: null, // function called when field is valid
onError: null, // function called when field has error
validators: {
email: function (email) {
var re = /^(([^<>()[\]\\.,;:\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,}))$/;
return email.match(re) ? true : "Please enter a valid email";
},
notEmpty: function (val) {
return val != "" ? true : "This field is required";
},
matchesOtherField: function (val) {
var $otherEl = $($(this).data('match-field-selector'));
return val === $otherEl.val();
}
}
};
options = $.extend(true, defaultOptions, options);
this.each(function () {
var $form = $(this),
numValidatedFields = 0;
if (options.disableSubmit) $form.find(':submit').prop('disabled', true);
$form.find('input, textarea').each(function () {
if (!$(this).data('validate')) return;
numValidatedFields++;
var validations = $(this).data('validate').split(/\s*,\s*|\s+/),
dirty = false,
hasBeenBlurredWhileDirty = options.immediateValidation;
var validateField = function (e) {
var val = $(this).val(),
errors = [],
i,
validation,
res;
// True iff user has typed in this field, ignoring tab (9) and shift
// (16) key presses
if (e.type === "keyup" && e.which !== 9 && e.which !== 16) dirty = true;
// True iff user has blurred this field after it was typed in
if (e.type === "blur" && dirty) hasBeenBlurredWhileDirty = true;
for (i=0; i < validations.length; i++) {
validation = validations[i];
res = options.validators[validation].call(this, val);
if (res == false || typeof res === 'string') {
// Error or error message returned
errors.push(res || "This field has an error");
}
}
if (errors.length > 0 && hasBeenBlurredWhileDirty) {
$(this).addClass(options.errorClass);
$(this).removeClass(options.validClass);
if (typeof options.onError === 'function') options.onError.call(this, errors);
} else if (errors.length === 0) {
$(this).addClass(options.validClass);
$(this).removeClass(options.errorClass);
if (typeof options.onValid === 'function') options.onValid.call(this);
}
if (options.disableSubmit) {
if ($form.find("." + options.validClass).length == numValidatedFields) {
$form.find(':submit').prop('disabled', false);
} else {
$form.find(':submit').prop('disabled', true);
}
}
};
if (options.validateOnBlur) $(this).blur(validateField);
if (options.validateOnKeyUp) $(this).keyup(validateField);
if ($(this).is(':checkbox')) $(this).change(validateField);
if (options.disableSubmit) {
$(this).bind('input', validateField);
$(this).trigger('input');
}
if ($form.data("validate-on-load") === true) {
validateField.call(this);
}
});
});
return this;
};
})(jQuery);