]>
Dogcows Code - chaz/p5-CGI-Ex/blob - lib/CGI/Ex/validate.js
1 // Copyright 2007 - Paul Seamons - $Revision: 1.74 $
2 // Distributed under the Perl Artistic License without warranty
3 // See perldoc CGI::Ex::Validate for usage
8 function ValidateError (errors
, extra
) {
11 this.as_string
= eob_as_string
;
12 this.as_array
= eob_as_array
;
13 this.as_hash
= eob_as_hash
;
14 this.first_field
= eob_first_field
;
19 function v_error (err
) { alert (err
); return 1 }
21 function v_clean_val_hash (val_hash
) {
22 if (typeof(val_hash
) != 'object') return {error: v_error("Validation must be an associative array (hash)")};
25 for (var key
in val_hash
) {
26 if (key
== 'extend') continue; // Protoype Array()
27 if (key
.match(/^general\s/)) {
28 var new_key
= key
.replace(/^general\s+/, 'group ');
29 val_hash
[new_key
] = val_hash
[key
];
30 delete(val_hash
[key
]);
37 var f
= val_hash
['group set_hook'];
38 if (f
&& typeof(f
) == 'string') eval("val_hash['group set_hook'] = "+f
);
39 f
= val_hash
['group clear_hook'];
40 if (f
&& typeof(f
) == 'string') eval("val_hash['group clear_hook'] = "+f
);
42 if (f
= val_hash
['group validate_if']) {
43 if (typeof(f
) == 'string' || ! f
.length
) f
= [f
];
44 var deps
= v_clean_cond(f
);
47 var fields
= val_hash
['group fields'];
49 if (typeof(fields
) != 'object' || ! fields
.length
)
50 return {error:v_error("'group fields' must be a non-empty array")};
53 var _order
= (val_hash
['group order']) ? val_hash
['group order'] : order
;
54 if (typeof(_order
) != 'object' || ! _order
.length
)
55 return {error:v_error("'group order' must be a non-empty array")};
56 for (var i
= 0; i
< _order
.length
; i
++) {
57 var field
= _order
[i
];
58 if (field
.match(/^group\s/)) continue;
59 var field_val
= val_hash
[field
];
61 if (field
== 'OR') field_val
= 'OR';
62 else return {error:v_error('No element found in group for '+field
)};
64 if (typeof(field_val
) == 'object' && ! field_val
['field']) field_val
['field'] = field
;
65 fields
.push(field_val
);
70 for (var i
= 0; i
< fields
.length
; i
++) {
71 var field_val
= fields
[i
];
72 var field
= field_val
.field
;
73 if (! field
) return {error:v_error("Missing field key in validation")};
77 for (var i
= 0; i
< order
.length
; i
++) {
79 if (found
[field
] || field
.match(/^group\s/)) continue;
80 var field_val
= val_hash
[field
];
81 if (typeof(field_val
) != 'object' || field_val
.length
) return {error:v_error('Found a non-hash value on field '+field
)};
82 if (! field_val
.field
) field_val
.field
= field
;
83 fields
.push(field_val
);
86 for (var i
= 0; i
< fields
.length
; i
++) v_clean_field_val(fields
[i
]);
88 val_hash
['group was_checked'] = {};
89 val_hash
['group was_valid'] = {};
90 val_hash
['group had_error'] = {};
92 return {'fields':fields
, 'order':order
};
95 function v_clean_field_val (field_val
, N_level
) {
96 if (! field_val
.order
) field_val
.order
= v_field_order(field_val
);
97 if (! field_val
.deps
) field_val
.deps
= {};
98 for (var i
= 0; i
< field_val
.order
.length
; i
++) {
99 var k
= field_val
.order
[i
];
100 var v
= field_val
[k
];
101 if (typeof(v
) == 'undefined') return {error:v_error('No matching validation found on field '+field
+' for type '+k
)};
102 if (k
.match(/^(min|max)_in_set(\d*)$/)) {
103 if (typeof(v
) == 'string') {
104 if (! (m
= v
.match(/^\s*(\d+)(?:\s*[oO][fF])?\s+(.+)\s*$/))) return {error:v_error("Invalid "+k
+" check "+v
)};
105 field_val
[k
] = m
[2].split(/[\s,]+/);
106 field_val
[k
].unshift(m
[1]);
108 for (var j
= 1; j
< field_val
[k
].length
; j
++) if (field_val
[k
][j
] != field_val
.field
) field_val
.deps
[field_val
[k
][j
]] = 1;
109 } else if (k
.match(/^(enum|compare)\d*$/)) {
110 if (typeof(v
) == 'string') field_val
[k
] = v
.split(/\s*\|\|\s*/);
111 } else if (k
.match(/^match\d*$/)) {
112 if (typeof(v
) == 'string') v
= field_val
[k
] = v
.split(/\s*\|\|\s*/);
113 for (var j
= 0; j
< v
.length
; j
++) {
114 if (typeof(v
[j
]) != 'string' || v
[j
] == '!') continue;
115 var m
= v
[j
].match(/^\s*(!\s*|)m([^\s\w])(.*)\2([eigsmx]*)\s*$/);
116 if (! m
) return {error:v_error("Not sure how to parse that match ("+v
[j
]+")")};
120 if (opt
.indexOf('e') != -1) return {error:v_error("The e option cannot be used on field "+field_val
.field
+", test "+k
)};
121 opt
= opt
.replace(/[sg]/g,'');
122 v
[j
] = new RegExp(pat
, opt
);
123 if (not
) v
.splice(j
, 0, '!');
125 } else if (k
.match(/^custom_js\d*$/)) {
126 if (typeof(v
) == 'string' && v
.match(/^\s*function\s*\(/)) eval("field_val[k] = "+v
);
127 } else if (k
.match(/^(validate|required)_if\d*$/)) {
128 if (typeof(v
) == 'string' || ! v
.length
) v
= field_val
[k
] = [v
];
129 var deps
= v_clean_cond(v
, N_level
);
130 for (var k
in deps
) field_val
.deps
[k
] = 2;
135 function v_clean_cond (ifs
, N_level
, ifs_match
) {
136 if (typeof(ifs
) != 'object') { v_error("Need reference v_clean_cond "+typeof(ifs
)); return [] }
137 if (! N_level
) N_level
= 0;
138 if (++N_level
> 10) { v_error("Max dependency level reached " + N_level
); return [] }
142 for (var i
= 0; i
< ifs
.length
; i
++) {
143 if (typeof(ifs
[i
]) == 'string') {
144 if (ifs
[i
].match(/^\s*function\s*\(/)) eval("ifs[i] = "+ifs
[i
]);
145 else if (m
= ifs
[i
].match(/^(.+?)\s+was_valid$/)) ifs
[i
] = {field: m
[1], was_valid:1}
146 else if (m
= ifs
[i
].match(/^(.+?)\s+had_error$/)) ifs
[i
] = {field: m
[1], had_error:1}
147 else if (m
= ifs
[i
].match(/^(.+?)\s+was_checked$/)) ifs
[i
] = {field: m
[1], was_checked:1}
148 else if (m
= ifs
[i
].match(/^(\s*!\s*)(.+)\s*$/)) ifs
[i
] = {field: m
[2], max_in_set: [0, m
[2]]};
149 else if (ifs
[i
] != 'OR') ifs
[i
] = {field: ifs
[i
], required: 1};
151 if (typeof(ifs
[i
]) != 'object') continue;
152 if (! ifs
[i
].field
) { v_error("Missing field key during validate_if"); return [] }
153 deps
[ifs
[i
].field
] = 2;
154 v_clean_field_val(ifs
[i
], N_level
);
155 for (var k
in ifs
[i
].deps
) deps
[k
] = 2;
160 function v_validate (form
, val_hash
) {
161 var clean
= v_clean_val_hash(val_hash
);
162 if (clean
.error
) return;
163 var order
= clean
.order
;
164 var fields
= clean
.fields
;
168 var title
= val_hash
['group title'];
169 var v_if
= val_hash
['group validate_if'];
170 if (v_if
&& ! v_check_conditional(form
, v_if
, val_hash
)) return;
177 for (var j
= 0; j
< fields
.length
; j
++) {
179 if (typeof(ref
) != 'object' && ref
== 'OR') {
188 val_hash
['group was_checked'][f
] = 1;
189 val_hash
['group was_valid'][f
] = 1;
190 val_hash
['group had_error'][f
] = 0;
192 var err
= v_validate_buddy(form
, f
, ref
, val_hash
);
194 val_hash
['group had_error'][f
] = 1;
195 val_hash
['group was_valid'][f
] = 0;
196 if (j
<= fields
.length
&& typeof(fields
[j
+ 1] != 'object') && fields
[j
+ 1] == 'OR') {
199 if (hold_error
) err
= hold_error
;
200 for (var k
= 0; k
< err
.length
; k
++) errors
.push(err
[k
]);
209 if (title
) ERRORS
.push(title
);
210 for (var j
= 0; j
< errors
.length
; j
++) ERRORS
.push(errors
[j
]);
214 for (var j
= 0; j
< order
.length
; j
++) {
215 var field
= order
[j
];
216 if (! (m
= field
.match(/^group\s+(\w+)$/))) continue;
217 if (errors
.length
== 0 || m
[1].match(/^(field|order|title|validate_if)$/)) continue;
218 EXTRA
[m
[1]] = val_hash
[field
];
221 if (ERRORS
.length
) return new ValidateError(ERRORS
, EXTRA
);
225 function v_check_conditional (form
, ifs
, val_hash
, ifs_match
) {
227 for (var i
= 0; i
< ifs
.length
; i
++) {
228 if (typeof(ifs
[i
]) == 'function') {
230 if (! ifs
[i
]({'form':form
})) is_ok
= 0;
231 } else if (typeof(ifs
[i
]) == 'string') {
232 if (ifs
[i
] != 'OR') { v_error("Found non-OR string"); return }
238 var field
= ifs
[i
].field
;
239 field
= field
.replace(/\$(\d+)/g, function (all
, N
) {
240 return (typeof(ifs_match
) != 'object' || typeof(ifs_match
[N
]) == 'undefined') ? '' : ifs_match
[N
];
242 var err
= v_validate_buddy(form
, field
, ifs
[i
], val_hash
);
243 if (err
.length
) is_ok
= 0;
249 function v_filter_types (type
, types
) {
251 var regexp
= new RegExp('^'+type
+'_?\\d*$');
252 for (var i
= 0; i
< types
.length
; i
++)
253 if (types
[i
].match(regexp
)) values
.push(types
[i
]);
257 function v_add_error (errors
,field
,type
,field_val
,ifs_match
,form
) {
258 errors
.push([field
, type
, field_val
, ifs_match
]);
259 if (field_val
.clear_on_error
) {
260 var el
= form
[field
];
261 if (el
&& el
.type
&& el
.type
.match(/(hidden|password|text|textarea|submit)/)) el
.value
= '';
266 function v_field_order (field_val
) {
268 for (var k
in field_val
)
269 if (! k
.match(/^(extend|field|name|required|was_valid|was_checked|had_error)$/) && ! k
.match(/_error$/)) o
.push(k
);
273 function v_validate_buddy (form
, field
, field_val
, val_hash
, ifs_match
) {
275 if (! form
.elements
|| field_val
.exclude_js
) return [];
276 var types
= field_val
.order
|| v_field_order(field_val
);
279 if (m
= field
.match(/^(!\s*|)m([^\s\w])(.*)\2([eigsmx]*)$/)) {
283 if (opt
.indexOf('e') != -1) { v_error("The e option cannot be used on field "+field
); return [] }
284 opt
= opt
.replace(/[sg]/g,'');
285 var reg
= new RegExp(pat
, opt
);
287 for (var i
= 0; i
< form
.elements
.length
; i
++) {
288 var _field
= form
.elements
[i
].name
;
289 if (! _field
) continue;
290 if ( (not
&& ! (m
= _field
.match(reg
))) || (m
= _field
.match(reg
))) {
291 var err
= v_validate_buddy(form
, _field
, field_val
, val_hash
, m
);
292 for (var j
= 0; j
< err
.length
; j
++) errors
.push(err
[j
]);
298 if (field_val
.was_valid
&& ! val_hash
['group was_valid'][field
]) return v_add_error(errors
, field
, 'was_valid', field_val
, ifs_match
, form
);
299 if (field_val
.had_error
&& ! val_hash
['group had_error'][field
]) return v_add_error(errors
, field
, 'had_error', field_val
, ifs_match
, form
);
300 if (field_val
.was_checked
&& ! val_hash
['group was_checked'][field
]) return v_add_error(errors
, field
, 'was_checked', field_val
, ifs_match
, form
);
302 var _value
= v_get_form_value(form
[field
]);
305 if (typeof(field_val
['default']) != 'undefined'
306 && (typeof(_value
) == 'undefined'
307 || (typeof(_value
) == 'object' && _value
.length
== 0)
308 || ! _value
.length
)) {
309 _value
= field_val
['default'];
313 var values
= (typeof(_value
) == 'object') ? _value : [_value
];
314 var n_values
= (typeof(_value
) == 'undefined') ? 0 : values
.length
;
316 for (var i
= 0; i
< values
.length
; i
++) {
317 if (typeof(values
[i
]) == 'undefined') continue;
318 var orig
= values
[i
];
319 if (! field_val
.do_not_trim
) values
[i
] = values
[i
].replace(/^\s+/,'').replace(/\s+$/,'');
320 if (field_val
.trim_control_chars
) values
[i
] = values
[i
].replace(/\t/g,' ').replace(/[\x00-\x1F]/g,'');
321 if (field_val
.to_upper_case
) values
[i
] = values
[i
].toUpperCase();
322 if (field_val
.to_lower_case
) values
[i
] = values
[i
].toLowerCase();
324 var tests
= v_filter_types('replace', types
);
325 for (var k
= 0; k
< tests
.length
; k
++) {
326 var ref
= field_val
[tests
[k
]];
327 ref
= (typeof(ref
) == 'object') ? ref : ref
.split(/\s*\|\|\s*/);
328 for (var j
= 0; j
< ref
.length
; j
++) {
329 if (! (m
= ref
[j
].match(/^\s*s([^\s\w])(.+)\1(.*)\1([eigmx]*)$/)))
330 return v_error("Not sure how to parse that replace "+ref
[j
]);
334 if (opt
.indexOf('e') != -1) { v_error("The e option cannot be used on field "+field
+", replace "+tests
[i
]); return [] }
335 var regexp
= new RegExp(pat
, opt
);
336 values
[i
] = values
[i
].replace(regexp
, swap
);
340 if (orig
!= values
[i
]) modified
= 1;
343 var el
= form
[field
];
344 if (el
) v_set_form_value(el
, values
);
349 var tests
= v_filter_types('validate_if', types
);
350 for (var i
= 0; i
< tests
.length
; i
++) {
351 var ifs
= field_val
[tests
[i
]];
352 var ret
= v_check_conditional(form
, ifs
, val_hash
, ifs_match
);
353 if (ret
) needs_val
++;
355 if (tests
.length
&& ! needs_val
) {
356 if (field_val
.vif_disable
&& val_hash
['group was_valid'][field
]) v_set_disable(form
[field
], true);
357 val_hash
['group was_valid'][field
] = 0;
360 if (field_val
.vif_disable
) v_set_disable(form
[field
], false);
362 var is_required
= field_val
['required'] ? 'required' : '';
364 var tests
= v_filter_types('required_if', types
);
365 for (var i
= 0; i
< tests
.length
; i
++) {
366 var ifs
= field_val
[tests
[i
]];
367 if (! v_check_conditional(form
, ifs
, val_hash
, ifs_match
)) continue;
368 is_required
= tests
[i
];
374 for (var i
= 0; i
< values
.length
; i
++) {
375 if (values
[i
].length
) {
380 if (! found
) return v_add_error(errors
, field
, is_required
, field_val
, ifs_match
, form
);
383 if (field_val
.min_values
&& n_values
< field_val
.min_values
)
384 return v_add_error(errors
, field
, 'min_values', field_val
, ifs_match
, form
);
386 if (typeof(field_val
.max_values
) == 'undefined') field_val
.max_values
= 1;
387 if (field_val
.max_values
&& n_values
> field_val
.max_values
)
388 return v_add_error(errors
, field
, 'max_values', field_val
, ifs_match
, form
);
390 for (var h
= 0; h
< 2 ; h
++) {
391 var minmax
= (h
== 0) ? 'min' : 'max';
392 var tests
= v_filter_types(minmax
+'_in_set', types
);
393 for (var i
= 0; i
< tests
.length
; i
++) {
394 var a
= field_val
[tests
[i
]];
396 for (var k
= 1; k
< a
.length
; k
++) {
397 var _value
= v_get_form_value(form
[a
[k
]]);
399 if (typeof(_value
) == 'undefined') continue;
400 _values
= (typeof(_value
) == 'object') ? _value : [_value
];
401 for (var l
= 0; l
< _values
.length
; l
++) {
402 var _value
= _values
[l
];
403 if (typeof(_value
) != 'undefined' && _value
.length
) n
--;
406 if ( (minmax
== 'min' && n
> 0)
407 || (minmax
== 'max' && n
< 0)) {
408 v_add_error(errors
, field
, tests
[i
], field_val
, ifs_match
, form
);
414 for (var i
= 0; i
< types
.length
; i
++) {
416 var _fv
= field_val
[type
];
417 for (var n
= 0; n
< values
.length
; n
++) {
418 var value
= values
[n
];
420 if (type
.match(/^enum\d*$/)) {
422 for (var j
= 0; j
< _fv
.length
; j
++) if (value
== _fv
[j
]) { is_found
= 1; break }
423 if (! is_found
) v_add_error(errors
, field
, type
, field_val
, ifs_match
, form
);
426 if (type
.match(/^equals\d*$/)) {
427 var not
= _fv
.match(/^!\s*/);
428 if (not
) _fv
= _fv
.substring(not
[0].length
);
430 if (m
= _fv
.match(/^([\"\'])(.*)\1$/)) {
431 if (value
== m
[2]) success
= 1;
433 var value2
= v_get_form_value(form
[_fv
]);
434 if (typeof(value2
) == 'undefined') value2
= '';
435 if (value
== value2
) success
= 1;
437 if (not
&& success
|| ! not
&& ! success
)
438 v_add_error(errors
, field
, type
, field_val
, ifs_match
, form
);
441 if (type
== 'min_len' && value
.length
< _fv
) v_add_error(errors
, field
, 'min_len', field_val
, ifs_match
, form
);
442 if (type
== 'max_len' && value
.length
> _fv
) v_add_error(errors
, field
, 'max_len', field_val
, ifs_match
, form
);
444 if (type
.match(/^match\d*$/)) {
445 for (var j
= 0; j
< _fv
.length
; j
++) {
446 if (typeof(_fv
[j
]) == 'string') continue;
447 var not
= (j
> 0 && typeof(_fv
[j
-1]) == 'string' && _fv
[j
-1] == '!') ? 1 : 0;
448 if ( ( not
&& value
.match(_fv
[j
]))
449 || (! not
&& ! value
.match(_fv
[j
]))) v_add_error(errors
, field
, type
, field_val
, ifs_match
, form
);
453 if (type
.match(/^compare\d*$/)) {
454 for (var j
= 0; j
< _fv
.length
; j
++) {
456 if (! comp
) continue;
459 if (m
= comp
.match(/^\s*(>|<|[><!=]=)\s*([\d\.\-]+)\s*$/)) {
460 if (! copy
) copy
= 0;
462 if (m
[1] == '>' ) hold
= (copy
> m
[2])
463 else if (m
[1] == '<' ) hold
= (copy
< m
[2])
464 else if (m
[1] == '>=') hold
= (copy
>= m
[2])
465 else if (m
[1] == '<=') hold
= (copy
<= m
[2])
466 else if (m
[1] == '!=') hold
= (copy
!= m
[2])
467 else if (m
[1] == '==') hold
= (copy
== m
[2])
468 } else if (m
= comp
.match(/^\s*(eq|ne|gt|ge|lt|le)\s+(.+?)\s*$/)) {
469 if ( m
[2].match(/^\"/)) m
[2] = m
[2].replace(/^"(.*)"$/,'$1');
470 else if (m
[2].match(/^\'/)) m
[2] = m
[2].replace(/^'(.*)'$/,'$1');
471 if (m
[1] == 'gt') hold
= (copy
> m
[2])
472 else if (m
[1] == 'lt') hold
= (copy
< m
[2])
473 else if (m
[1] == 'ge') hold
= (copy
>= m
[2])
474 else if (m
[1] == 'le') hold
= (copy
<= m
[2])
475 else if (m
[1] == 'ne') hold
= (copy
!= m
[2])
476 else if (m
[1] == 'eq') hold
= (copy
== m
[2])
478 v_error("Not sure how to compare \""+comp
+"\"");
481 if (! hold
) v_add_error(errors
, field
, type
, field_val
, ifs_match
, form
);
485 if (type
.match(/^type\d*$/))
486 if (! v_check_type(value
, _fv
, field
, form
))
487 v_add_error(errors
, field
, type
, field_val
, ifs_match
, form
);
490 // the js is evaluated and should return 1 for success
491 // or 0 for failure - the variables field, value, and field_val (the hash) are available
492 if (type
.match(/^custom_js\d*$/)) {
493 var value
= values
.length
== 1 ? values
[0] : values
;
494 if (typeof(_fv
) == 'function'
495 ? ! _fv({'value':value
, 'field_val':field_val
, 'form':form
, 'key':field_val
.field
, 'errors':errors
, 'event':v_event
})
496 : ! eval(_fv
)) v_add_error(errors
, field
, type
, field_val
, ifs_match
, form
);
503 function v_check_type (value
, type
, field
, form
) {
505 type
= type
.toUpperCase();
507 if (type
== 'EMAIL') {
508 if (! value
) return 0;
509 if (! (m
= value
.match(/^(.+)@(.+?)$/))) return 0;
510 if (m
[1].length
> 60) return 0;
511 if (m
[2].length
> 100) return 0;
512 if (! v_check_type(m
[2],'DOMAIN') && ! v_check_type(m
[2],'IP')) return 0;
513 if (! v_check_type(m
[1],'LOCAL_PART')) return 0;
515 } else if (type
== 'LOCAL_PART') {
516 if (typeof(value
) == 'undefined' || ! value
.length
) return 0;
517 if (typeof(v_local_part
) != 'undefined') return (value
.match(v_local_part
) ? 1 : 0);
518 if (value
.match(/[^a-z0-9.\-!&+]/)) return 0;
519 if (value
.match(/^[.\-]/)) return 0;
520 if (value
.match(/[.\-&]$/)) return 0;
521 if (value
.match(/(\.-|-\.|\.\.)/)) return 0;
523 } else if (type
== 'IP') {
524 if (! value
) return 0;
525 var dig
= value
.split(/\./);
526 if (dig
.length
!= 4) return 0;
527 for (var i
= 0; i
< 4; i
++)
528 if (typeof(dig
[i
]) == 'undefined' || dig
[i
].match(/\D/) || dig
[i
] > 255) return 0;
530 } else if (type
== 'DOMAIN') {
531 if (! value
) return 0;
532 if (! value
.match(/^[a-z0-9.-]{4,255}$/)) return 0;
533 if (value
.match(/^[.\-]/)) return 0;
534 if (value
.match(/(\.-|-\.|\.\.)/)) return 0;
535 if (! (m
= value
.match(/^(.+\.)([a-z]{2,10})$/))) return 0;
536 if (! m
[1].match(/^([a-z0-9\-]{1,62}\.)+$/)) return 0;
538 } else if (type
== 'URL') {
539 if (! value
) return 0;
540 if (! (m
= value
.match(/^https?:\/\/([^\/]+)/i))) return 0;
541 value
= value
.substring(m
[0].length
);
542 var dom
= m
[1].replace(/:\d+$/).replace(/\.$/);
543 if (! v_check_type(dom
,'DOMAIN') && ! v_check_type(m
[1],'IP')) return 0;
544 if (value
&& ! v_check_type(value
,'URI')) return 0;
546 } else if (type
== 'URI') {
547 if (! value
) return 0;
548 if (value
.match(/\s/)) return 0;
550 } else if (type
== 'CC') {
551 if (! value
) return 0;
552 if (value
.match(/[^\d
\- ]/)) return 0;
553 value
= value
.replace(/[\- ]/g
, '');
554 if (value
.length
> 16 || value
.length
< 13) return 0;
558 for (var i
= value
.length
- 1; i
>= 0; i
--) {
559 if (++swc
> 2) swc
= 1;
560 var y
= value
.charAt(i
) * swc
;
564 if (sum
% 10) return 0;
570 function v_set_form_value (el
, values
, form
) {
571 if (typeof(el
) == 'string') el
= form
[el
];
572 if (typeof(values
) != 'object') values
= [values
];
574 var type
= (el
.type
&& ! (''+el
).match(/HTMLCollection/)) ? el
.type
.toLowerCase() : '';
575 if (el
.length
&& type
!= 'select-one') {
576 for (var i
= 0; i
< el
.length
; i
++) {
577 if (! el
[i
] || ! el
[i
].type
) continue;
578 v_set_form_value(el
[i
], (el
[i
].type
.match(/^(checkbox|radio)$/) ? values : i
< values
.length
? [values
[i
]] : ['']));
583 if (type
.match(/(hidden|password|text|textarea|submit)/)) return el
.value
= values
[0];
584 if (type
.indexOf('select') != -1) {
585 if (el
.length
) for (var i
= 0; i
< el
.length
; i
++) el
[i
].selected
= (el
[i
].value
== values
[0]) ? true : false;
588 if (type
== 'checkbox' || type
== 'radio') {
589 var f
; for (var i
= 0; i
< values
.length
; i
++) if (values
[i
] == el
.value
) f
= 1;
590 return el
.checked
= f
? true : false;
592 if (type
== 'file') return;
594 alert('Unknown form type for '+el
.name
+': '+type
);
598 function v_set_disable (el
, disable
) {
600 var type
= el
.type
? el
.type
.toLowerCase() : '';
601 if (el
.length
&& type
!= 'select-one') {
602 for (var j
=0;j
<el
.length
;j
++) el
[i
].disabled
= disable
;
603 } else el
.disabled
= disable
;
606 function v_get_form_value (el
, form
) {
607 if (typeof(el
) == 'string') el
= form
[el
];
609 var type
= (el
.type
&& ! (''+el
).match(/HTMLCollection/)) ? el
.type
.toLowerCase() : '';
610 if (el
.length
&& type
!= 'select-one') {
612 for (var j
=0;j
<el
.length
;j
++) {
613 if (type
.indexOf('multiple') != -1) {
614 if (el
[j
].selected
) a
.push(el
[j
].value
);
616 if (el
[j
].checked
) a
.push(v_get_form_value(el
[j
]));
619 if (a
.length
== 0) return '';
620 if (a
.length
== 1) return a
[0];
623 if (! type
) return '';
624 if (type
.match(/(hidden|password|text|textarea|submit)/)) return el
.value
;
625 if (type
.indexOf('select') != -1) {
626 if (! el
.length
) return '';
627 if (el
.selectedIndex
== -1) return '';
628 return el
[el
.selectedIndex
].value
;
630 if (type
== 'checkbox' || type
== 'radio') return el
.checked
? el
.value : '';
631 if (type
== 'file') return el
.value
;
633 alert('Unknown form type for '+el
.name
+': '+type
);
637 function v_find_val () {
638 var key
= arguments
[0];
639 for (var i
= 1; i
< arguments
.length
; i
++) {
640 if (typeof(arguments
[i
]) == 'string') return arguments
[i
];
641 if (typeof(arguments
[i
]) == 'undefined') continue;
642 if (typeof(arguments
[i
][key
]) != 'undefined') return arguments
[i
][key
];
647 function v_get_error_text (err
, extra1
, extra2
) {
650 var field_val
= err
[2];
651 var ifs_match
= err
[3];
655 if (m
= type
.match(/^(.+?)(\d+)$/)) { type
= m
[1] ; dig
= m
[2] }
656 var type_lc
= type
.toLowerCase();
657 var v
= field_val
[type
+ dig
];
659 if (field_val
.delegate_error
) {
660 field
= field_val
.delegate_error
;
661 field
= field
.replace(/\$(\d+)/g, function (all
, N
) {
662 if (typeof(ifs_match
) != 'object'
663 || typeof(ifs_match
[N
]) == 'undefined') return ''
668 var name
= field_val
.name
|| "The field " +field
;
669 name
= name
.replace(/\$(\d+)/g, function (all
, N
) {
670 if (typeof(ifs_match
) != 'object'
671 || typeof(ifs_match
[N
]) == 'undefined') return ''
675 var msg
= v_find_val(type
+ '_error', extra1
, extra2
);
677 if (dig
.length
) msg
= field_val
[type
+ dig
+ '_error'];
678 if (! msg
) msg
= field_val
[type
+ '_error'];
681 msg
= msg
.replace(/\$(\d+)/g, function (all
, N
) {
682 if (typeof(ifs_match
) != 'object' || typeof(ifs_match
[N
]) == 'undefined') return '';
685 msg
= msg
.replace(/\$field/g, field
);
686 msg
= msg
.replace(/\$name/g, name
);
687 if (v
&& typeof(v
) == 'string') msg
= msg
.replace(/\$value/g, v
);
691 if (type
== 'equals') {
692 var field2
= field_val
["equals" + dig
];
693 var name2
= field_val
["equals" +dig
+ "_name"];
694 if (! name2
) name2
= "the field " +field2
;
695 name2
= name2
.replace(/\$(\d+)/g, function (all
, N
) {
696 return (typeof(ifs_match
) != 'object' || typeof(ifs_match
[N
]) == 'undefined') ? '' : ifs_match
[N
];
698 return name
+ " did not equal " + name2
+".";
700 if (type
== 'min_in_set') return "Not enough fields were chosen from the set ("+v
[0]+' of '+v
.join(", ").replace(/^\d+,\s*/,'')+")";
701 if (type
== 'max_in_set') return "Too many fields were chosen from the set (" +v
[0]+' of '+v
.join(", ").replace(/^\d+,\s*/,'')+")";
704 (type
== 'required' || type
== 'required_if') ? " is required."
705 : type
== 'match' ? " contains invalid characters."
706 : type
== 'compare' ? " did not fit comparison."
707 : type
== 'custom_js' ? " did not match custom_js"+dig
+" check."
708 : type
== 'enum' ? " is not in the given list."
709 : type
== 'min_values' ? " had less than "+v
+" value"+(v
== 1 ? '' : 's')+"."
710 : type
== 'max_values' ? " had more than "+v
+" value"+(v
== 1 ? '' : 's')+"."
711 : type
== 'min_len' ? " was less than "+v
+" character"+(v
== 1 ? '' : 's')+"."
712 : type
== 'max_len' ? " was more than "+v
+" character"+(v
== 1 ? '' : 's')+"."
713 : type
== 'type' ? " did not match type "+v
+"."
714 : type
== 'had_error' ? " had no error (but should have had)."
715 : type
== 'was_valid' ? " was not valid."
716 : type
== 'was_checked'? " was not checked."
717 : alert("Missing error on field "+field
+" for type "+type
+dig
));
722 function eob_as_string (extra
) {
723 var joiner
= v_find_val('as_string_join', extra
, this.extra
, '\n');
724 var header
= v_find_val('as_string_header', extra
, this.extra
, '');
725 var footer
= v_find_val('as_string_footer', extra
, this.extra
, '');
726 return header
+ this.as_array(extra
).join(joiner
) + footer
;
729 function eob_as_array (extra
) {
730 var errors
= this.errors
;
731 var title
= v_find_val('as_array_title', extra
, this.extra
, 'Please correct the following items:');
734 if (title
) has_headings
= 1;
735 else for (var i
= 0; i
< errors
.length
; i
++) if (typeof(errors
[i
]) == 'string') has_headings
= 1;
737 var prefix
= v_find_val('as_array_prefix', extra
, this.extra
, (has_headings
? ' ' : ''));
740 if (title
&& title
.length
) arr
.push(title
);
743 for (var i
= 0; i
< errors
.length
; i
++) {
744 if (typeof(errors
[i
]) == 'string') {
748 var text
= v_get_error_text(errors
[i
], extra
, this.extra
);
749 if (found
[text
]) continue;
751 arr
.push(prefix
+ text
);
758 function eob_as_hash (extra
) {
759 var errors
= this.errors
;
760 var suffix
= v_find_val('as_hash_suffix', extra
, this.extra
, '_error');
761 var joiner
= v_find_val('as_hash_join', extra
, this.extra
, '<br/>');
765 for (var i
= 0; i
< errors
.length
; i
++) {
766 if (typeof(errors
[i
]) == 'string') continue;
767 if (! errors
[i
].length
) continue;
769 var field
= errors
[i
][0];
770 var type
= errors
[i
][1];
771 var field_val
= errors
[i
][2];
772 var ifs_match
= errors
[i
][3];
774 if (! field
) return alert("Missing field name");
775 if (field_val
['delegate_error']) {
776 field
= field_val
['delegate_error'];
777 field
= field
.replace(/\$(\d+)/g, function (all
, N
) {
778 if (typeof(ifs_match
) != 'object'
779 || typeof(ifs_match
[N
]) == 'undefined') return ''
784 var text
= v_get_error_text(errors
[i
], extra
, this.extra
);
785 if (! found
[field
]) found
[field
] = {};
786 if (found
[field
][text
]) continue;
787 found
[field
][text
] = 1;
790 if (! ret
[field
]) ret
[field
] = [];
791 ret
[field
].push(text
);
795 var header
= v_find_val('as_hash_header', extra
, this.extra
, '');
796 var footer
= v_find_val('as_hash_footer', extra
, this.extra
, '');
797 for (var key
in ret
) {
798 if (key
== 'extend') continue; // Protoype Array()
799 ret
[key
] = header
+ ret
[key
].join(joiner
) + footer
;
806 function eob_first_field () {
807 for (var i
= 0; i
< this.errors
.length
; i
++) {
808 if (typeof(this.errors
[i
]) != 'object') continue;
809 if (! this.errors
[i
][0]) continue;
810 return this.errors
[i
][0];
817 document
.validate = function (form
, val_hash
) {
818 val_hash
= document
.load_val_hash(form
, val_hash
);
819 if (typeof(val_hash
) == 'undefined') return true;
821 if (v_event
!= 'load') {
822 for (var key
in v_did_inline
) {
823 if (key
== 'extend') continue; // Protoype Array()
824 v_inline_error_clear(key
, val_hash
, form
);
828 var err_obj
= v_validate(form
, val_hash
);
829 if (! err_obj
) return true;
831 var field
= err_obj
.first_field();
832 if (field
&& form
[field
]) {
833 if (form
[field
].focus
) form
[field
].focus();
834 else if (form
[field
].length
&& form
[field
][0].focus
) form
[field
][0].focus();
837 if (! val_hash
['group no_inline']) {
838 var hash
= err_obj
.as_hash({as_hash_suffix:""});
839 for (var key
in hash
) {
840 if (key
== 'extend') continue; // Protoype Array()
841 v_inline_error_set(key
, hash
[key
], val_hash
, form
);
845 if (v_event
== 'load') {
847 } else if (! val_hash
['group no_confirm']) {
848 return confirm(err_obj
.as_string()) ? false : true;
849 } else if (! val_hash
['group no_alert']) {
850 alert(err_obj
.as_string());
852 } else if (! val_hash
['group no_inline']) {
859 document
.load_val_hash = function (form
, val_hash
) {
860 if (! form
) return alert('Missing form or form name');
861 if (typeof(form
) == 'string') {
862 if (! document
[form
]) return alert('No form by name '+form
);
863 form
= document
[form
];
866 if (form
.val_hash
) return form
.val_hash
;
868 if (typeof(val_hash
) != 'object') {
869 if (typeof(val_hash
) == 'function') {
870 val_hash
= val_hash(formname
);
871 } else if (typeof(val_hash
) == 'undefined') {
873 if (typeof(document
.validation
) != 'undefined') {
874 val_hash
= document
.validation
;
875 } else if (el
= document
.getElementById('validation')) {
876 val_hash
= el
.innerHTML
.replace(/</ig,'<').replace(/>/ig,'>').replace(/&/ig,'&');
880 var yaml
= form
.getAttribute('validation');
882 if (m
= yaml
.match(/^( +)/)) yaml
= yaml
.replace(new RegExp('^'+m
[1], 'g'), '');
883 yaml
= yaml
.replace(/\s*$/,'\n');
887 for (var i
= 0; i
< form
.elements
.length
; i
++) {
888 var name
= form
.elements
[i
].name
;
889 var yaml
= form
.elements
[i
].getAttribute('validation');
890 if (! name
|| ! yaml
) continue;
891 yaml
= yaml
.replace(/\s*$/,'\n').replace(/^(.)/mg,' $1').replace(/^( *[^\s
&*\[\{])/,'\n$1');
892 str
+= name
+':' + yaml
;
895 if (str
) val_hash
= str
+ "group order: [" + order
.join(', ') + "]\n";
898 if (typeof(val_hash
) == 'string') {
899 if (! document
.yaml_load
) return;
900 document
.hide_yaml_errors
= (! document
.show_yaml_errors
);
901 if (location
.search
&& location
.search
.indexOf('show_yaml_errors') != -1)
902 document
.hide_yaml_errors
= 0;
903 val_hash
= document
.yaml_load(val_hash
);
904 if (document
.yaml_error_occured
) return;
905 val_hash
= val_hash
[0];
909 form
.val_hash
= val_hash
;
910 return form
.val_hash
;
913 document
.check_form = function (form
, val_hash
) {
914 if (! form
) return alert('Missing form or form name');
915 if (typeof(form
) == 'string') {
916 if (! document
[form
]) return alert('No form by name '+form
);
917 form
= document
[form
];
920 val_hash
= document
.load_val_hash(form
, val_hash
);
921 if (! val_hash
) return;
923 var types
= val_hash
['group onevent'] || {submit:1};
924 if (typeof(types
) == 'string') types
= types
.split(/\s*,\s*/);
925 if (typeof(types
.length
) != 'undefined') {
927 for (var i
= 0; i
< types
.length
; i
++) t
[types
[i
]] = 1;
930 val_hash
['group onevent'] = types
;
932 if (types
.change
|| types
.blur
) {
933 var clean
= v_clean_val_hash(val_hash
);
934 if (clean
.error
) return clean
.error
;
936 _add = function (k
, v
) { if (! h
[k
]) h
[k
] = []; h
[k
].push(v
) };
937 for (var i
= 0; i
< clean
.fields
.length
; i
++) {
938 _add(clean
.fields
[i
].field
, clean
.fields
[i
]);
939 for (var j
in clean
.fields
[i
].deps
) if (j
!= clean
.fields
[i
].field
) _add(j
, clean
.fields
[i
]);
942 if (k
== 'extend') continue; // Protoype Array()
944 if (! el
) return v_error("No form element by the name "+k
);
945 v_el_attach(el
, h
[k
], form
, val_hash
);
950 var orig_submit
= form
.onsubmit
|| function () { return true };
951 form
.onsubmit = function (e
) { v_event
= 'submit'; return document
.validate(this) && orig_submit(e
, this) };
954 if (types
.load
) { v_event
= 'load'; document
.validate(form
) }
957 function v_el_attach (el
, fvs
, form
, val_hash
) {
959 if (el
.length
) for (var i
= 0; i
< el
.length
; i
++) v_el_attach(el
[i
], fvs
, form
, val_hash
);
962 var types
= val_hash
['group onevent'];
963 var func = function () {
968 for (var i
= 0; i
< fvs
.length
; i
++) {
969 var field_val
= fvs
[i
];
970 var k
= field_val
.field
;
973 val_hash
['group was_checked'][k
] = 1;
974 val_hash
['group was_valid'][k
] = 1;
975 val_hash
['group had_error'][k
] = 0;
977 var _e
= v_validate_buddy(form
, k
, field_val
, val_hash
);
979 val_hash
['group had_error'][k
] = 1;
980 val_hash
['group was_valid'][k
] = 0;
981 for (var j
= 0; j
< _e
.length
; j
++) e
.push(_e
[j
]);
983 f
[field_val
.delegate_error
|| field_val
.field
] = _e
.length
? 0 : 1;
985 for (var k
in f
) if (f
[k
]) v_inline_error_clear(k
, val_hash
, form
);
986 if (! e
.length
) return;
987 e
= new ValidateError(e
, {});
988 e
= e
.as_hash({as_hash_suffix:"", first_only:(val_hash
['group first_only']?1:0)});
990 if (k
== 'extend') continue; // Protoype Array()
991 v_inline_error_set(k
, e
[k
], val_hash
, form
);
994 if (types
.blur
) el
.onblur
= func
;
995 if (types
.change
&& ! (''+el
).match(/HTMLCollection/)) { // find better way on opera
996 var type
= el
.type
? el
.type
.toLowerCase() : '';
997 if (type
.match(/(password|text|textarea)/)) el
.onkeyup
= func
;
998 else if (type
.match(/(checkbox|radio)/)) el
.onclick
= func
;
999 else if (type
.match(/(select)/)) el
.onchange
= func
;
1003 function v_inline_error_clear (key
, val_hash
, form
) {
1004 delete(v_did_inline
[key
]);
1005 var f
= val_hash
['group clear_hook'] || document
.validate_clear_hook
;
1006 var g
= val_hash
['group was_valid'] || {};
1007 if (typeof(f
) == 'function') if (f({'key':key
, 'val_hash':val_hash
, 'form':form
, was_valid:g
[key
], 'event':v_event
})) return 1;
1008 var el
= document
.getElementById(key
+ v_find_val('as_hash_suffix', val_hash
, '_error'));
1009 if (el
) el
.innerHTML
= '';
1012 function v_inline_error_set (key
, val
, val_hash
, form
) {
1013 v_did_inline
[key
] = 1;
1014 var f
= val_hash
['group set_hook'] || document
.validate_set_hook
;
1015 if (typeof(f
) == 'function') if (f({'key':key
, 'value':val
, 'val_hash':val_hash
, 'form':form
, 'event':v_event
})) return 1;
1016 var el
= document
.getElementById(key
+ v_find_val('as_hash_suffix', val_hash
, '_error'));
1017 if (el
) el
.innerHTML
= val
;
This page took 0.119891 seconds and 4 git commands to generate.