-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathform-validation.js
158 lines (139 loc) · 4.04 KB
/
form-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
var __formValidation = {
/*
* "Cache"
*/
forms: {},
inputs: {},
validations: {
equal: function(input, data) {
if (typeof data.value === 'object') {
return __formValidation.inputs[input].input.val() == data.value.val();
} else {
return __formValidation.inputs[input].input.val() == data.value;
}
},
lessThan: function(input, data) {
return __formValidation.inputs[input].input.val().parseInt() < data.value;
},
greaterThan: function(input, data) {
return __formValidation.inputs[input].input.val().parseInt() > data.value;
},
minLength: function(input, data) {
return __formValidation.inputs[input].input.val().length >= data.value;
},
maxLength: function(input, data) {
return __formValidation.inputs[input].input.val().length <= data.value;
},
regularExpression: function(input, data) {
return data.value.test(__formValidation.inputs[input].input.val());
},
email: function(input, data) {
var emailExpression = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return __formValidation.validations.regularExpression(input, { value: emailExpression });
}
},
/**
* @param input <String> Id of the input to validate
* @param rules <Object> Validation rules
*/
validate: function(input, rules) {
var ok = true;
var errorDiv;
/*
* Check if the object is in the "cache",
* if is not, then add it
*/
if (!__formValidation.inputs[input]) {
__formValidation.inputs[input] = {
input: $('#' + input),
errorDiv: $('#error-' + input)
};
}
errorDiv = __formValidation.inputs[input].errorDiv;
/*
* Removing old errores from the
* error div (only if exists)
*/
if (errorDiv) {
errorDiv.hide();
errorDiv.children().remove();
}
for (var rule in rules) {
/*
* Check if the rule is satisfied
*/
if (!__formValidation.validations[rule](input, rules[rule])) {
/*
* If not, then add the error message
* (only if the error div exists)
*/
if (errorDiv) {
if (rules[rule].message) {
errorDiv.append('<div>' + rules[rule].message + '</div>');
}
errorDiv.show();
} else { // If error div not exists show a tooltip
// Input that not have error div
var inputTooltip = __formValidation.inputs[input].input;
$(inputTooltip).attr('rel', 'tooltip');
$(inputTooltip).attr('title', rules[rule].message);
// Show error in tooltip
$(inputTooltip).tooltip({placement: 'right',trigger: 'manual', template: '<div class="tooltip error-class"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'}).tooltip('show');
// Hide tooltip (4000ms)
setTimeout(function(){
$(inputTooltip).tooltip('hide');
$(inputTooltip).removeAttr('rel');
$(inputTooltip).removeAttr('title');
}, 4000);
}
ok = false;
}
}
return ok;
},
/**
* @param inputs <Object> Inputs to validate
* @return <Boolean> True if everything is sucessfully validated
*/
validateAllInputs: function(inputs) {
var ok = true;
for (var input in inputs) {
if (!__formValidation.validate(input, inputs[input])) {
ok = false;
}
}
return ok;
}
};
/**
* @param name <String> Name of the new validation
* @param fn <Function> Function to call when the validation is needed
*/
var addCustomFormValidation = function(name, fn) {
__formValidation.validations[name] = function(input, data) {
return fn(__formValidation.inputs[input].input, data);
};
};
/**
* @param rules <Object> Inputs and validations rules
* @param callback <Function> If all fields are sucessfully validated
* then this function will be called
*/
jQuery.prototype.setValidationRules = function(rules, callback) {
var id = this.attr('id');
if (!__formValidation.forms[id]) {
__formValidation.forms[id] = {
form: this,
data: rules,
fn: callback
};
this.on('submit', function(e) {
var _this = __formValidation.forms[$(this).attr('id')];
if (__formValidation.validateAllInputs(_this.data)) {
_this.fn(e);
} else {
e.preventDefault();
}
});
}
};