-
Notifications
You must be signed in to change notification settings - Fork 0
/
formvalidator.js
292 lines (274 loc) · 9.4 KB
/
formvalidator.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
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
/**
* @author Rob Mills <http://www.seengee.co.uk/>
* @license MIT
* @require prototype.js,scriptaculous.js
*/
if(typeof(Prototype) == "undefined") { throw "protoValid requires prototype.js"; }
if(typeof(Effect) == "undefined") { throw "protoValid requires scriptaculous.js"; }
var protoValid = Class.create({
initialize: function(options) {
this.options = {
form: "myForm",
valid: "tick",
notValid: "cross",
msgElement: "span",
effectShow: "Appear",
effectHide: "Fade",
regexes: {
email: "^[-!#$%&'*+\\/0-9=?A-Z^_a-z{|}~](\\.?[-!#$%&'*+\\/0-9=?A-Z^_a-z{|}~])*@[a-zA-Z0-9](-?[a-zA-Z0-9])*(\\.[a-zA-Z0-9](-?[a-zA-Z0-9])*)+$",
string: "[^\\\\]*(?:\\\\.[^\\\\]*)*",
number: "\\b\\d+\\b"
},
validateOnBlur: true,
clearOnFocus: false,
passwordToggles: false,
extend: null
};
this.internals = {
event : null,
form : null,
validated : []
};
Object.extend(this.options, options || {});
this.internals.form = $(this.options.form);
if(!this.internals.form) return false;
this.internals.form.observe("submit",function(ev){
this.internals.event = ev; // this.internals.event.type = "submit"
this.validateForm();
}.bind(this));
if(this.options.validateOnBlur === true){
this.internals.form.getElements().invoke("observe","blur", function(ev) {
this.internals.event = ev; // this.internals.event.type = "blur"
this.validateElement(ev.element());
}.bind(this));
}
if(this.options.clearOnFocus === true){
this.internals.form.getElements().invoke("observe","focus", function(ev) {
var el = ev.element();
if(el.hasClassName(this.options.notValid)) {
$(el).next(this.options.msgElement).hide();
}
el.removeClassName(this.options.valid).removeClassName(this.options.notValid);
}.bind(this));
}
if(this.options.passwordToggles === true){
this.internals.form.getInputs('password').each(function(el){
var chk = el.id+'_chk';
el.insert({after:'<input type="checkbox" id="'+chk+'">'});
$(chk).observe("click",function(){
var chkel = ($(chk).id).replace("_chk","");
if($(chk).checked){
$(chkel).type = "text";
}else{
$(chkel).type = "password";
}
});
},this);
}
},
/**
* Overall Form Validation Routine
*/
validateForm: function(){
this.internals.form.getElements().each(function(el){
this.validateElement(el);
},this);
},
/**
* Validate Individual Elements
*/
validateElement: function(el){
if(el.className.include('required')) this.validateRequired(el);
if(el.className.include('optional') || el.className.include('combo')) this.validateOptional(el);
if(el.className.include('duplicate')){
if(el.className.include('optional')) {
this.validateOptional(el);
}else{
this.validateRequired(el);
}
}
},
/**
* Validates Optional Inputs
* Checks first if elememt is empty, if not empty it validates against Regular Expression
*/
validateOptional: function(el){
if ($F(el).strip()) {
this.validateRegexp(el);
} else {
this.passValidation(el);
}
},
/**
* Validates Required Inputs
* Checks required elements have values and then checks them against relavant Regular Expression
*/
validateRequired: function(el){
if(el.type == 'checkbox'){
if(!el.checked){
this.failValidation(el);
} else {
this.passValidation(el);
}
}else{
if(!$F(el).strip()){
this.failValidation(el);
} else {
this.validateRegexp(el);
}
}
},
/**
* Add element to array of objects that have been validated
*/
validatedObjectsAdd: function(el){
if(this.internals.validated.indexOf(el.id) == -1){
this.internals.validated.push(el.id);
}
},
/**
* Remove element from array of objects that have been validated
*/
validatedObjectsRemove: function(el){
if(this.internals.validated.indexOf(el.id) != -1){
this.internals.validated.splice(this.internals.validated.indexOf(el.id),1);
}
},
/**
* Validate input using a regular expression
*/
validateRegexp: function(el){
for(var regex in (this.options.regexes)){
if(el.hasClassName(regex)){
var regexp = new RegExp(this.options.regexes[regex]);
var match = regexp.exec($F(el));
if (match !== null) {
this.passValidation(el);
} else {
this.failValidation(el);
}
}
}
},
/**
* Validation of multiple items with at least one value required
*/
validateCombo: function(el){
el.className.match(/(?:^|\s+)combi-(\d+)(?:\s|$)/);
var ocid = RegExp.$1;
var cont = true;
var combos = this.internals.form.select('.combi-'+ocid);
combos.each(function(elm){
if(this.internals.validated.indexOf(elm.id) == -1){ // field has not yet been validated
this.validateElement(elm);
cont = false;
}
},this);
if(cont){
var allempty = true;
combos.each(function(elmt){
if($F(elmt).strip()) allempty = false;
});
if(allempty) {
this.failElementValidations(combos);
}else{
this.passElementValidations(combos);
}
}
},
/**
* Validation of items that must contain the same value
*/
validateDuplicate: function(el){
el.className.match(/(?:^|\s+)dupe-(\d+)(?:\s|$)/);
var val = RegExp.$1;
var cont = true;
var duplicates = this.internals.form.select('.dupe-'+val);
// only check duplicate validation if both fields have been validated already
duplicates.each(function(elm){
if(this.internals.validated.indexOf(elm.id) == -1){ // field has not yet been validated
cont = false;
throw $break;
}
},this);
// check duplicate values match
if(cont){
var value = null;
duplicates.each(function(e){
fail = (value = value || $F(e)) !== $F(e);
},this);
if(fail){
this.failElementValidations(duplicates);
}else{
this.passElementValidations(duplicates);
}
}else{
this.styleValidationResult(el,this.options.valid);
}
},
/**
* Pass element to additional validation routines
*/
passValidation: function(el){
this.validatedObjectsAdd(el);
if(el.className.include('duplicate')){
this.validateDuplicate(el);
}else if(el.className.include('combo')){
this.validateCombo(el);
}else{
this.styleValidationResult(el,this.options.valid);
}
},
/**
* Pass element(s) to the passed validation routine
*/
passElementValidations: function(el){
if(Object.isArray(el)){
el.each(function(e){
this.styleValidationResult(e,this.options.valid);
},this);
}else{
this.styleValidationResult(el,this.options.valid);
}
},
/**
* Pass element(s) to the failed validation routine
*/
failElementValidations: function(el){
if(Object.isArray(el)){
el.each(function(e){
this.styleValidationResult(e,this.options.notValid);
},this);
}else{
this.styleValidationResult(el,this.options.notValid);
}
},
/**
* Update class name on element to show whether validation has been passed or failed
*/
styleValidationResult: function(el,newclass){
var testclass = (newclass == this.options.valid) ? this.options.notValid : this.options.valid;
var errEl = "";
if(!$(el).hasClassName(newclass)){
$(el).removeClassName(testclass).addClassName(newclass);
}
if(newclass == this.options.notValid){
errEl = $(el).next(this.options.msgElement);
$(errEl).update($(el).title).hide();
new Effect[this.options.effectShow](errEl);
}else{
errEl = $(el).next(this.options.msgElement);
if($(errEl).visible()){
new Effect[this.options.effectHide](errEl);
}
}
},
/**
* Use overall failed validation to prevent form submission
*/
failValidation: function(el){
if(this.internals.event) Event.stop(this.internals.event);
this.validatedObjectsRemove(el);
this.failElementValidations(el);
}
});