]>
Dogcows Code - chaz/p5-CGI-Ex/blob - lib/CGI/Ex/validate.js
388f6739d8277700c5752e80fe700d836619dc92
1 // Copyright 2008 - Paul Seamons - $Revision: 1.81 $
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_get_ordered_fields (val_hash
) {
22 if (typeof(val_hash
) != 'object') return {error: v_error("Validation must be an associative array (hash)")};
27 for (var key
in val_hash
) {
28 if (key
== 'extend') continue; // Protoype Array()
29 if (m
= key
.match(/^(general|group)\s+(\w+)/)) {
30 ARGS
[m
[2]] = val_hash
[key
];
35 field_keys
= field_keys
.sort();
37 var f
= ARGS
.set_hook
; if (f
&& typeof(f
) == 'string') eval("ARGS.set_hook = "+f
);
38 f
= ARGS
.clear_hook
; if (f
&& typeof(f
) == 'string') eval("ARGS.clear_hook = "+f
);
39 f
= ARGS
.set_all_hook
; if (f
&& typeof(f
) == 'string') eval("ARGS.set_all_hook = "+f
);
40 f
= ARGS
.clear_all_hook
; if (f
&& typeof(f
) == 'string') eval("ARGS.clear_all_hook = "+f
);
42 if (f
= ARGS
.validate_if
) {
43 if (typeof(f
) == 'string' || ! f
.length
) f
= [f
];
44 var deps
= v_clean_cond(f
);
49 if (ref
= ARGS
.fields
|| ARGS
['order']) {
50 if (typeof(ref
) != 'object' || ! ref
.length
)
51 return {error:v_error("'group fields' must be a non-empty array")};
52 for (var i
= 0; i
< ref
.length
; i
++) {
54 if (typeof(field
) == 'object') {
55 if (! field
.field
) return {error:v_error("Missing field key in validation")};
57 } else if (field
== 'OR') {
60 var field_val
= val_hash
[field
];
61 if (! field_val
) return {error:v_error('No element found in group for '+field
)};
62 if (typeof(field_val
) == 'object' && ! field_val
['field']) field_val
['field'] = field
;
63 fields
.push(field_val
);
69 for (var i
= 0; i
< fields
.length
; i
++) {
70 var field_val
= fields
[i
];
71 if (typeof(field_val
) != 'object') continue;
72 found
[field_val
.field
] = 1;
75 for (var i
= 0; i
< field_keys
.length
; i
++) {
76 var field
= field_keys
[i
];
77 if (found
[field
]) continue;
78 var field_val
= val_hash
[field
];
79 if (typeof(field_val
) != 'object' || field_val
.length
) return {error:v_error('Found a non-hash value on field '+field
)};
80 if (! field_val
.field
) field_val
.field
= field
;
81 fields
.push(field_val
);
84 for (var i
= 0; i
< fields
.length
; i
++) v_clean_field_val(fields
[i
]);
86 val_hash
['group was_checked'] = {};
87 val_hash
['group was_valid'] = {};
88 val_hash
['group had_error'] = {};
90 return {'fields':fields
, 'args':ARGS
};
93 function v_clean_field_val (field_val
, N_level
) {
94 if (! field_val
.order
) field_val
.order
= v_field_order(field_val
);
95 if (! field_val
.deps
) field_val
.deps
= {};
96 for (var i
= 0; i
< field_val
.order
.length
; i
++) {
97 var k
= field_val
.order
[i
];
99 if (typeof(v
) == 'undefined') return {error:v_error('No matching validation found on field '+field
+' for type '+k
)};
100 if (k
.match(/^(min|max)_in_set(\d*)$/)) {
101 if (typeof(v
) == 'string') {
102 if (! (m
= v
.match(/^\s*(\d+)(?:\s*[oO][fF])?\s+(.+)\s*$/))) return {error:v_error("Invalid "+k
+" check "+v
)};
103 field_val
[k
] = m
[2].split(/[\s,]+/);
104 field_val
[k
].unshift(m
[1]);
106 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;
107 } else if (k
.match(/^(enum|compare)\d*$/)) {
108 if (typeof(v
) == 'string') field_val
[k
] = v
.split(/\s*\|\|\s*/);
109 } else if (k
.match(/^match\d*$/)) {
110 if (typeof(v
) == 'string') v
= field_val
[k
] = v
.split(/\s*\|\|\s*/);
111 for (var j
= 0; j
< v
.length
; j
++) {
112 if (typeof(v
[j
]) != 'string' || v
[j
] == '!') continue;
113 var m
= v
[j
].match(/^\s*(!\s*|)m([^\s\w])(.*)\2([eigsmx]*)\s*$/);
114 if (! m
) return {error:v_error("Not sure how to parse that match ("+v
[j
]+")")};
118 if (opt
.indexOf('e') != -1) return {error:v_error("The e option cannot be used on field "+field_val
.field
+", test "+k
)};
119 opt
= opt
.replace(/[sg]/g,'');
120 v
[j
] = new RegExp(pat
, opt
);
121 if (not
) v
.splice(j
, 0, '!');
123 } else if (k
.match(/^custom_js\d*$/)) {
124 if (typeof(v
) == 'string' && v
.match(/^\s*function\s*\(/)) eval("field_val[k] = "+v
);
125 } else if (k
.match(/^(validate|required)_if\d*$/)) {
126 if (typeof(v
) == 'string' || ! v
.length
) v
= field_val
[k
] = [v
];
127 var deps
= v_clean_cond(v
, N_level
);
128 for (var k
in deps
) field_val
.deps
[k
] = 2;
133 function v_clean_cond (ifs
, N_level
, ifs_match
) {
134 if (typeof(ifs
) != 'object') { v_error("Need reference v_clean_cond "+typeof(ifs
)); return [] }
135 if (! N_level
) N_level
= 0;
136 if (++N_level
> 10) { v_error("Max dependency level reached " + N_level
); return [] }
140 for (var i
= 0; i
< ifs
.length
; i
++) {
141 if (typeof(ifs
[i
]) == 'string') {
142 if (ifs
[i
].match(/^\s*function\s*\(/)) eval("ifs[i] = "+ifs
[i
]);
143 else if (m
= ifs
[i
].match(/^(.+?)\s+was_valid$/)) ifs
[i
] = {field: m
[1], was_valid:1}
144 else if (m
= ifs
[i
].match(/^(.+?)\s+had_error$/)) ifs
[i
] = {field: m
[1], had_error:1}
145 else if (m
= ifs
[i
].match(/^(.+?)\s+was_checked$/)) ifs
[i
] = {field: m
[1], was_checked:1}
146 else if (m
= ifs
[i
].match(/^(\s*!\s*)(.+)\s*$/)) ifs
[i
] = {field: m
[2], max_in_set: [0, m
[2]]};
147 else if (ifs
[i
] != 'OR') ifs
[i
] = {field: ifs
[i
], required: 1};
149 if (typeof(ifs
[i
]) != 'object') continue;
150 if (! ifs
[i
].field
) { v_error("Missing field key during validate_if"); return [] }
151 deps
[ifs
[i
].field
] = 2;
152 v_clean_field_val(ifs
[i
], N_level
);
153 for (var k
in ifs
[i
].deps
) deps
[k
] = 2;
158 function v_validate (form
, val_hash
) {
159 var clean
= v_get_ordered_fields(val_hash
);
160 if (clean
.error
) return;
161 var fields
= clean
.fields
;
165 var title
= val_hash
['group title'];
166 var v_if
= val_hash
['group validate_if'];
167 if (v_if
&& ! v_check_conditional(form
, v_if
, val_hash
)) return;
174 for (var j
= 0; j
< fields
.length
; j
++) {
176 if (typeof(ref
) != 'object' && ref
== 'OR') {
185 val_hash
['group was_checked'][f
] = 1;
186 val_hash
['group was_valid'][f
] = 1;
187 val_hash
['group had_error'][f
] = 0;
189 var err
= v_validate_buddy(form
, f
, ref
, val_hash
);
191 val_hash
['group had_error'][f
] = 1;
192 val_hash
['group was_valid'][f
] = 0;
193 if (j
<= fields
.length
&& typeof(fields
[j
+ 1] != 'object') && fields
[j
+ 1] == 'OR') {
196 if (hold_error
) err
= hold_error
;
197 for (var k
= 0; k
< err
.length
; k
++) errors
.push(err
[k
]);
206 if (title
) ERRORS
.push(title
);
207 for (var j
= 0; j
< errors
.length
; j
++) ERRORS
.push(errors
[j
]);
210 for (var field
in clean
.args
) {
211 if (errors
.length
== 0 || field
.match(/^(field|order|title|validate_if)$/)) continue;
212 EXTRA
[field
] = clean
.args
[field
];
215 if (ERRORS
.length
) return new ValidateError(ERRORS
, EXTRA
);
219 function v_check_conditional (form
, ifs
, val_hash
, ifs_match
) {
221 for (var i
= 0; i
< ifs
.length
; i
++) {
222 if (typeof(ifs
[i
]) == 'function') {
224 if (! ifs
[i
]({'form':form
})) is_ok
= 0;
225 } else if (typeof(ifs
[i
]) == 'string') {
226 if (ifs
[i
] != 'OR') { v_error("Found non-OR string"); return }
232 var field
= ifs
[i
].field
;
233 field
= field
.replace(/\$(\d+)/g, function (all
, N
) {
234 return (typeof(ifs_match
) != 'object' || typeof(ifs_match
[N
]) == 'undefined') ? '' : ifs_match
[N
];
236 var err
= v_validate_buddy(form
, field
, ifs
[i
], val_hash
);
237 if (err
.length
) is_ok
= 0;
243 function v_filter_types (type
, types
) {
245 var regexp
= new RegExp('^'+type
+'_?\\d*$');
246 for (var i
= 0; i
< types
.length
; i
++)
247 if (types
[i
].match(regexp
)) values
.push(types
[i
]);
251 function v_add_error (errors
,field
,type
,field_val
,ifs_match
,form
) {
252 errors
.push([field
, type
, field_val
, ifs_match
]);
253 if (field_val
.clear_on_error
) {
254 var el
= form
[field
];
255 if (el
&& el
.type
&& el
.type
.match(/(hidden|password|text|textarea|submit)/)) el
.value
= '';
260 function v_field_order (field_val
) {
262 for (var k
in field_val
)
263 if (! k
.match(/^(extend|field|name|required|was_valid|was_checked|had_error)$/) && ! k
.match(/_error$/)) o
.push(k
);
267 function v_validate_buddy (form
, field
, field_val
, val_hash
, ifs_match
) {
269 if (! form
.elements
|| field_val
.exclude_js
) return [];
270 var types
= field_val
.order
|| v_field_order(field_val
);
273 if (m
= field
.match(/^(!\s*|)m([^\s\w])(.*)\2([eigsmx]*)$/)) {
277 if (opt
.indexOf('e') != -1) { v_error("The e option cannot be used on field "+field
); return [] }
278 opt
= opt
.replace(/[sg]/g,'');
279 var reg
= new RegExp(pat
, opt
);
281 for (var i
= 0; i
< form
.elements
.length
; i
++) {
282 var _field
= form
.elements
[i
].name
;
283 if (! _field
) continue;
284 if ( (not
&& ! (m
= _field
.match(reg
))) || (m
= _field
.match(reg
))) {
285 var err
= v_validate_buddy(form
, _field
, field_val
, val_hash
, m
);
286 for (var j
= 0; j
< err
.length
; j
++) errors
.push(err
[j
]);
292 if (field_val
.was_valid
&& ! val_hash
['group was_valid'][field
]) return v_add_error(errors
, field
, 'was_valid', field_val
, ifs_match
, form
);
293 if (field_val
.had_error
&& ! val_hash
['group had_error'][field
]) return v_add_error(errors
, field
, 'had_error', field_val
, ifs_match
, form
);
294 if (field_val
.was_checked
&& ! val_hash
['group was_checked'][field
]) return v_add_error(errors
, field
, 'was_checked', field_val
, ifs_match
, form
);
296 var _value
= v_get_form_value(form
[field
]);
299 if (typeof(field_val
['default']) != 'undefined'
300 && (typeof(_value
) == 'undefined'
301 || (typeof(_value
) == 'object' && _value
.length
== 0)
302 || ! _value
.length
)) {
303 _value
= field_val
['default'];
307 var values
= (typeof(_value
) == 'object') ? _value : [_value
];
308 var n_values
= (typeof(_value
) == 'undefined') ? 0 : values
.length
;
310 for (var i
= 0; i
< values
.length
; i
++) {
311 if (typeof(values
[i
]) == 'undefined') continue;
312 var orig
= values
[i
];
313 if (! field_val
.do_not_trim
) values
[i
] = values
[i
].replace(/^\s+/,'').replace(/\s+$/,'');
314 if (field_val
.trim_control_chars
) values
[i
] = values
[i
].replace(/\t/g,' ').replace(/[\x00-\x1F]/g,'');
315 if (field_val
.to_upper_case
) values
[i
] = values
[i
].toUpperCase();
316 if (field_val
.to_lower_case
) values
[i
] = values
[i
].toLowerCase();
318 var tests
= v_filter_types('replace', types
);
319 for (var k
= 0; k
< tests
.length
; k
++) {
320 var ref
= field_val
[tests
[k
]];
321 ref
= (typeof(ref
) == 'object') ? ref : ref
.split(/\s*\|\|\s*/);
322 for (var j
= 0; j
< ref
.length
; j
++) {
323 if (! (m
= ref
[j
].match(/^\s*s([^\s\w])(.+)\1(.*)\1([eigmx]*)$/)))
324 return v_error("Not sure how to parse that replace "+ref
[j
]);
328 if (opt
.indexOf('e') != -1) { v_error("The e option cannot be used on field "+field
+", replace "+tests
[i
]); return [] }
329 var regexp
= new RegExp(pat
, opt
);
330 values
[i
] = values
[i
].replace(regexp
, swap
);
334 if (orig
!= values
[i
]) modified
= 1;
337 var el
= form
[field
];
338 if (el
) v_set_form_value(el
, values
);
343 var tests
= v_filter_types('validate_if', types
);
344 for (var i
= 0; i
< tests
.length
; i
++) {
345 var ifs
= field_val
[tests
[i
]];
346 var ret
= v_check_conditional(form
, ifs
, val_hash
, ifs_match
);
347 if (ret
) needs_val
++;
349 if (tests
.length
&& ! needs_val
) {
350 if (field_val
.vif_disable
&& val_hash
['group was_valid'][field
]) v_set_disable(form
[field
], true);
351 val_hash
['group was_valid'][field
] = 0;
354 if (field_val
.vif_disable
) v_set_disable(form
[field
], false);
356 var is_required
= field_val
['required'] ? 'required' : '';
358 var tests
= v_filter_types('required_if', types
);
359 for (var i
= 0; i
< tests
.length
; i
++) {
360 var ifs
= field_val
[tests
[i
]];
361 if (! v_check_conditional(form
, ifs
, val_hash
, ifs_match
)) continue;
362 is_required
= tests
[i
];
368 for (var i
= 0; i
< values
.length
; i
++) {
369 if (values
[i
].length
) {
374 if (! found
) return v_add_error(errors
, field
, is_required
, field_val
, ifs_match
, form
);
377 if (field_val
.min_values
&& n_values
< field_val
.min_values
)
378 return v_add_error(errors
, field
, 'min_values', field_val
, ifs_match
, form
);
380 if (typeof(field_val
.max_values
) == 'undefined') field_val
.max_values
= 1;
381 if (field_val
.max_values
&& n_values
> field_val
.max_values
)
382 return v_add_error(errors
, field
, 'max_values', field_val
, ifs_match
, form
);
384 for (var h
= 0; h
< 2 ; h
++) {
385 var minmax
= (h
== 0) ? 'min' : 'max';
386 var tests
= v_filter_types(minmax
+'_in_set', types
);
387 for (var i
= 0; i
< tests
.length
; i
++) {
388 var a
= field_val
[tests
[i
]];
390 for (var k
= 1; k
< a
.length
; k
++) {
391 var _value
= v_get_form_value(form
[a
[k
]]);
393 if (typeof(_value
) == 'undefined') continue;
394 _values
= (typeof(_value
) == 'object') ? _value : [_value
];
395 for (var l
= 0; l
< _values
.length
; l
++) {
396 var _value
= _values
[l
];
397 if (typeof(_value
) != 'undefined' && _value
.length
) n
--;
400 if ( (minmax
== 'min' && n
> 0)
401 || (minmax
== 'max' && n
< 0)) {
402 v_add_error(errors
, field
, tests
[i
], field_val
, ifs_match
, form
);
408 for (var i
= 0; i
< types
.length
; i
++) {
410 var _fv
= field_val
[type
];
411 for (var n
= 0; n
< values
.length
; n
++) {
412 var value
= values
[n
];
414 if (type
.match(/^enum\d*$/)) {
416 for (var j
= 0; j
< _fv
.length
; j
++) if (value
== _fv
[j
]) { is_found
= 1; break }
417 if (! is_found
) v_add_error(errors
, field
, type
, field_val
, ifs_match
, form
);
420 if (type
.match(/^equals\d*$/)) {
421 var not
= _fv
.match(/^!\s*/);
422 if (not
) _fv
= _fv
.substring(not
[0].length
);
424 if (m
= _fv
.match(/^([\"\'])(.*)\1$/)) {
425 if (value
== m
[2]) success
= 1;
427 var value2
= v_get_form_value(form
[_fv
]);
428 if (typeof(value2
) == 'undefined') value2
= '';
429 if (value
== value2
) success
= 1;
431 if (not
&& success
|| ! not
&& ! success
)
432 v_add_error(errors
, field
, type
, field_val
, ifs_match
, form
);
435 if (type
== 'min_len' && value
.length
< _fv
) v_add_error(errors
, field
, 'min_len', field_val
, ifs_match
, form
);
436 if (type
== 'max_len' && value
.length
> _fv
) v_add_error(errors
, field
, 'max_len', field_val
, ifs_match
, form
);
438 if (type
.match(/^match\d*$/)) {
439 for (var j
= 0; j
< _fv
.length
; j
++) {
440 if (typeof(_fv
[j
]) == 'string') continue;
441 var not
= (j
> 0 && typeof(_fv
[j
-1]) == 'string' && _fv
[j
-1] == '!') ? 1 : 0;
442 if ( ( not
&& value
.match(_fv
[j
]))
443 || (! not
&& ! value
.match(_fv
[j
]))) v_add_error(errors
, field
, type
, field_val
, ifs_match
, form
);
447 if (type
.match(/^compare\d*$/)) {
448 for (var j
= 0; j
< _fv
.length
; j
++) {
450 if (! comp
) continue;
453 if (m
= comp
.match(/^\s*(>|<|[><!=]=)\s*([\d\.\-]+)\s*$/)) {
454 if (! copy
) copy
= 0;
456 if (m
[1] == '>' ) hold
= (copy
> m
[2])
457 else if (m
[1] == '<' ) hold
= (copy
< m
[2])
458 else if (m
[1] == '>=') hold
= (copy
>= m
[2])
459 else if (m
[1] == '<=') hold
= (copy
<= m
[2])
460 else if (m
[1] == '!=') hold
= (copy
!= m
[2])
461 else if (m
[1] == '==') hold
= (copy
== m
[2])
462 } else if (m
= comp
.match(/^\s*(eq|ne|gt|ge|lt|le)\s+(.+?)\s*$/)) {
463 if ( m
[2].match(/^\"/)) m
[2] = m
[2].replace(/^"(.*)"$/,'$1');
464 else if (m
[2].match(/^\'/)) m
[2] = m
[2].replace(/^'(.*)'$/,'$1');
465 if (m
[1] == 'gt') hold
= (copy
> m
[2])
466 else if (m
[1] == 'lt') hold
= (copy
< m
[2])
467 else if (m
[1] == 'ge') hold
= (copy
>= m
[2])
468 else if (m
[1] == 'le') hold
= (copy
<= m
[2])
469 else if (m
[1] == 'ne') hold
= (copy
!= m
[2])
470 else if (m
[1] == 'eq') hold
= (copy
== m
[2])
472 v_error("Not sure how to compare \""+comp
+"\"");
475 if (! hold
) v_add_error(errors
, field
, type
, field_val
, ifs_match
, form
);
479 if (type
.match(/^type\d*$/))
480 if (! v_check_type(value
, _fv
, field
, form
))
481 v_add_error(errors
, field
, type
, field_val
, ifs_match
, form
);
484 // the js is evaluated and should return 1 for success
485 // or 0 for failure - the variables field, value, and field_val (the hash) are available
486 if (type
.match(/^custom_js\d*$/)) {
487 var value
= values
.length
== 1 ? values
[0] : values
;
488 if (typeof(_fv
) == 'function'
489 ? ! _fv({'value':value
, 'field_val':field_val
, 'form':form
, 'key':field_val
.field
, 'errors':errors
, 'event':v_event
})
490 : ! eval(_fv
)) v_add_error(errors
, field
, type
, field_val
, ifs_match
, form
);
497 function v_check_type (value
, type
, field
, form
) {
499 type
= type
.toUpperCase();
501 if (type
== 'EMAIL') {
502 if (! value
) return 0;
503 if (! (m
= value
.match(/^(.+)@(.+?)$/))) return 0;
504 if (m
[1].length
> 60) return 0;
505 if (m
[2].length
> 100) return 0;
506 if (! v_check_type(m
[2],'DOMAIN') && ! v_check_type(m
[2],'IP')) return 0;
507 if (! v_check_type(m
[1],'LOCAL_PART')) return 0;
509 } else if (type
== 'LOCAL_PART') {
510 if (typeof(value
) == 'undefined' || ! value
.length
) return 0;
511 if (typeof(v_local_part
) != 'undefined') return (value
.match(v_local_part
) ? 1 : 0);
512 if (value
.match(/[^a-z0-9.\-!&+]/)) return 0;
513 if (value
.match(/^[.\-]/)) return 0;
514 if (value
.match(/[.\-&]$/)) return 0;
515 if (value
.match(/(\.-|-\.|\.\.)/)) return 0;
517 } else if (type
== 'IP') {
518 if (! value
) return 0;
519 var dig
= value
.split(/\./);
520 if (dig
.length
!= 4) return 0;
521 for (var i
= 0; i
< 4; i
++)
522 if (typeof(dig
[i
]) == 'undefined' || dig
[i
].match(/\D/) || dig
[i
] > 255) return 0;
524 } else if (type
== 'DOMAIN') {
525 if (! value
) return 0;
526 if (! value
.match(/^[a-z0-9.-]{4,255}$/)) return 0;
527 if (value
.match(/^[.\-]/)) return 0;
528 if (value
.match(/(\.-|-\.|\.\.)/)) return 0;
529 if (! (m
= value
.match(/^(.+\.)([a-z]{2,10})$/))) return 0;
530 if (! m
[1].match(/^([a-z0-9\-]{1,63}\.)+$/)) return 0;
532 } else if (type
== 'URL') {
533 if (! value
) return 0;
534 if (! (m
= value
.match(/^https?:\/\/([^\/]+)/i))) return 0;
535 value
= value
.substring(m
[0].length
);
536 var dom
= m
[1].replace(/:\d+$/).replace(/\.$/);
537 if (! v_check_type(dom
,'DOMAIN') && ! v_check_type(m
[1],'IP')) return 0;
538 if (value
&& ! v_check_type(value
,'URI')) return 0;
540 } else if (type
== 'URI') {
541 if (! value
) return 0;
542 if (value
.match(/\s/)) return 0;
544 } else if (type
== 'CC') {
545 if (! value
) return 0;
546 if (value
.match(/[^\d
\- ]/)) return 0;
547 value
= value
.replace(/[\- ]/g
, '');
548 if (value
.length
> 16 || value
.length
< 13) return 0;
552 for (var i
= value
.length
- 1; i
>= 0; i
--) {
553 if (++swc
> 2) swc
= 1;
554 var y
= value
.charAt(i
) * swc
;
558 if (sum
% 10) return 0;
564 function v_set_form_value (el
, values
, form
) {
565 if (typeof(el
) == 'string') el
= form
[el
];
566 if (typeof(values
) != 'object') values
= [values
];
568 var type
= (el
.type
&& ! (''+el
).match(/HTMLCollection/)) ? el
.type
.toLowerCase() : '';
569 if (el
.length
&& type
!= 'select-one') {
570 for (var i
= 0; i
< el
.length
; i
++) {
571 if (! el
[i
] || ! el
[i
].type
) continue;
572 v_set_form_value(el
[i
], (el
[i
].type
.match(/^(checkbox|radio)$/) ? values : i
< values
.length
? [values
[i
]] : ['']));
577 if (type
.match(/(hidden|password|text|textarea|submit)/)) return el
.value
= values
[0];
578 if (type
.indexOf('select') != -1) {
579 if (el
.length
) for (var i
= 0; i
< el
.length
; i
++) el
[i
].selected
= (el
[i
].value
== values
[0]) ? true : false;
582 if (type
== 'checkbox' || type
== 'radio') {
583 var f
; for (var i
= 0; i
< values
.length
; i
++) if (values
[i
] == el
.value
) f
= 1;
584 return el
.checked
= f
? true : false;
586 if (type
== 'file') return;
588 alert('Unknown form type for '+el
.name
+': '+type
);
592 function v_set_disable (el
, disable
) {
594 var type
= el
.type
? el
.type
.toLowerCase() : '';
595 if (el
.length
&& type
!= 'select-one') {
596 for (var j
=0;j
<el
.length
;j
++) el
[i
].disabled
= disable
;
597 } else el
.disabled
= disable
;
600 function v_get_form_value (el
, form
) {
601 if (typeof(el
) == 'string') el
= form
[el
];
603 var type
= (el
.type
&& ! (''+el
).match(/HTMLCollection/)) ? el
.type
.toLowerCase() : '';
604 if (el
.length
&& type
!= 'select-one') {
606 for (var j
=0;j
<el
.length
;j
++) {
607 if (type
.indexOf('multiple') != -1) {
608 if (el
[j
].selected
) a
.push(el
[j
].value
);
610 if (el
[j
].checked
) a
.push(v_get_form_value(el
[j
]));
613 if (a
.length
== 0) return '';
614 if (a
.length
== 1) return a
[0];
617 if (! type
) return '';
618 if (type
.match(/(hidden|password|text|textarea|submit)/)) return el
.value
;
619 if (type
.indexOf('select') != -1) {
620 if (! el
.length
) return '';
621 if (el
.selectedIndex
== -1) return '';
622 return el
[el
.selectedIndex
].value
;
624 if (type
== 'checkbox' || type
== 'radio') return el
.checked
? el
.value : '';
625 if (type
== 'file') return el
.value
;
627 alert('Unknown form type for '+el
.name
+': '+type
);
631 function v_find_val () {
632 var key
= arguments
[0];
633 for (var i
= 1; i
< arguments
.length
; i
++) {
634 if (typeof(arguments
[i
]) == 'string') return arguments
[i
];
635 if (typeof(arguments
[i
]) == 'undefined') continue;
636 if (typeof(arguments
[i
][key
]) != 'undefined') return arguments
[i
][key
];
641 function v_get_error_text (err
, extra1
, extra2
) {
644 var field_val
= err
[2];
645 var ifs_match
= err
[3];
649 if (m
= type
.match(/^(.+?)(\d+)$/)) { type
= m
[1] ; dig
= m
[2] }
650 var type_lc
= type
.toLowerCase();
651 var v
= field_val
[type
+ dig
];
653 if (field_val
.delegate_error
) {
654 field
= field_val
.delegate_error
;
655 field
= field
.replace(/\$(\d+)/g, function (all
, N
) {
656 if (typeof(ifs_match
) != 'object'
657 || typeof(ifs_match
[N
]) == 'undefined') return ''
662 var name
= field_val
.name
;
663 if (! name
&& (field
.match(/\W/) || (field
.match(/\d/) && field
.match(/\D/)))) {
664 name
= "The field " +field
;
666 if (! name
) name
= field
.replace(/_
/g
, ' ').replace(/\b(\w)/g, function(all
,str
){return str
.toUpperCase()});
667 name
= name
.replace(/\$(\d+)/g, function (all
, N
) {
668 if (typeof(ifs_match
) != 'object'
669 || typeof(ifs_match
[N
]) == 'undefined') return ''
673 var msg
= v_find_val(type
+ '_error', extra1
, extra2
);
675 if (dig
.length
) msg
= field_val
[type
+ dig
+ '_error'];
676 if (! msg
) msg
= field_val
[type
+ '_error'];
677 if (! msg
) msg
= field_val
['error'];
680 msg
= msg
.replace(/\$(\d+)/g, function (all
, N
) {
681 if (typeof(ifs_match
) != 'object' || typeof(ifs_match
[N
]) == 'undefined') return '';
684 msg
= msg
.replace(/\$field/g, field
);
685 msg
= msg
.replace(/\$name/g, name
);
686 if (v
&& typeof(v
) == 'string') msg
= msg
.replace(/\$value/g, v
);
690 if (type
== 'equals') {
691 var field2
= field_val
["equals" + dig
];
692 var name2
= field_val
["equals" +dig
+ "_name"];
693 if (! name2
) name2
= "the field " +field2
;
694 name2
= name2
.replace(/\$(\d+)/g, function (all
, N
) {
695 return (typeof(ifs_match
) != 'object' || typeof(ifs_match
[N
]) == 'undefined') ? '' : ifs_match
[N
];
697 return name
+ " did not equal " + name2
+".";
699 if (type
== 'min_in_set') return "Not enough fields were chosen from the set ("+v
[0]+' of '+v
.join(", ").replace(/^\d+,\s*/,'')+")";
700 if (type
== 'max_in_set') return "Too many fields were chosen from the set (" +v
[0]+' of '+v
.join(", ").replace(/^\d+,\s*/,'')+")";
703 (type
== 'required' || type
== 'required_if') ? " is required."
704 : type
== 'match' ? " contains invalid characters."
705 : type
== 'compare' ? " did not fit comparison."
706 : type
== 'custom_js' ? " did not match custom_js"+dig
+" check."
707 : type
== 'enum' ? " is not in the given list."
708 : type
== 'min_values' ? " had less than "+v
+" value"+(v
== 1 ? '' : 's')+"."
709 : type
== 'max_values' ? " had more than "+v
+" value"+(v
== 1 ? '' : 's')+"."
710 : type
== 'min_len' ? " was less than "+v
+" character"+(v
== 1 ? '' : 's')+"."
711 : type
== 'max_len' ? " was more than "+v
+" character"+(v
== 1 ? '' : 's')+"."
712 : type
== 'type' ? " did not match type "+v
+"."
713 : type
== 'had_error' ? " had no error (but should have had)."
714 : type
== 'was_valid' ? " was not valid."
715 : type
== 'was_checked'? " was not checked."
716 : alert("Missing error on field "+field
+" for type "+type
+dig
));
721 function eob_as_string (extra
) {
722 var joiner
= v_find_val('as_string_join', extra
, this.extra
, '\n');
723 var header
= v_find_val('as_string_header', extra
, this.extra
, '');
724 var footer
= v_find_val('as_string_footer', extra
, this.extra
, '');
725 return header
+ this.as_array(extra
).join(joiner
) + footer
;
728 function eob_as_array (extra
) {
729 var errors
= this.errors
;
730 var title
= v_find_val('as_array_title', extra
, this.extra
, 'Please correct the following items:');
733 if (title
) has_headings
= 1;
734 else for (var i
= 0; i
< errors
.length
; i
++) if (typeof(errors
[i
]) == 'string') has_headings
= 1;
736 var prefix
= v_find_val('as_array_prefix', extra
, this.extra
, (has_headings
? ' ' : ''));
739 if (title
&& title
.length
) arr
.push(title
);
742 for (var i
= 0; i
< errors
.length
; i
++) {
743 if (typeof(errors
[i
]) == 'string') {
747 var text
= v_get_error_text(errors
[i
], extra
, this.extra
);
748 if (found
[text
]) continue;
750 arr
.push(prefix
+ text
);
757 function eob_as_hash (extra
) {
758 var errors
= this.errors
;
759 var suffix
= v_find_val('as_hash_suffix', extra
, this.extra
, '_error');
760 var joiner
= v_find_val('as_hash_join', extra
, this.extra
, '<br/>');
764 for (var i
= 0; i
< errors
.length
; i
++) {
765 if (typeof(errors
[i
]) == 'string') continue;
766 if (! errors
[i
].length
) continue;
768 var field
= errors
[i
][0];
769 var type
= errors
[i
][1];
770 var field_val
= errors
[i
][2];
771 var ifs_match
= errors
[i
][3];
773 if (! field
) return alert("Missing field name");
774 if (field_val
['delegate_error']) {
775 field
= field_val
['delegate_error'];
776 field
= field
.replace(/\$(\d+)/g, function (all
, N
) {
777 if (typeof(ifs_match
) != 'object'
778 || typeof(ifs_match
[N
]) == 'undefined') return ''
783 var text
= v_get_error_text(errors
[i
], extra
, this.extra
);
784 if (! found
[field
]) found
[field
] = {};
785 if (found
[field
][text
]) continue;
786 found
[field
][text
] = 1;
789 if (! ret
[field
]) ret
[field
] = [];
790 ret
[field
].push(text
);
794 var header
= v_find_val('as_hash_header', extra
, this.extra
, '');
795 var footer
= v_find_val('as_hash_footer', extra
, this.extra
, '');
796 for (var key
in ret
) {
797 if (key
== 'extend') continue; // Protoype Array()
798 ret
[key
] = header
+ ret
[key
].join(joiner
) + footer
;
805 function eob_first_field () {
806 for (var i
= 0; i
< this.errors
.length
; i
++) {
807 if (typeof(this.errors
[i
]) != 'object') continue;
808 if (! this.errors
[i
][0]) continue;
809 return this.errors
[i
][0];
816 document
.validate = function (form
, val_hash
) {
817 val_hash
= document
.load_val_hash(form
, val_hash
);
818 if (typeof(val_hash
) == 'undefined') return true;
820 if (v_event
!= 'load') {
821 for (var key
in v_did_inline
) {
822 if (key
== 'extend') continue; // Protoype Array()
823 v_inline_error_clear(key
, val_hash
, form
);
827 var err_obj
= v_validate(form
, val_hash
);
829 var f
= val_hash
['group clear_all_hook'] || document
.validate_clear_all_hook
;
834 var f
= val_hash
['group set_all_hook'] || document
.validate_set_all_hook
;
837 var field
= err_obj
.first_field();
838 if (field
&& form
[field
]) {
839 if (form
[field
].focus
) form
[field
].focus();
840 else if (form
[field
].length
&& form
[field
][0].focus
) form
[field
][0].focus();
843 if (! val_hash
['group no_inline']) {
844 var hash
= err_obj
.as_hash({as_hash_suffix:""});
845 for (var key
in hash
) {
846 if (key
== 'extend') continue; // Protoype Array()
847 v_inline_error_set(key
, hash
[key
], val_hash
, form
);
851 if (v_event
== 'load') {
853 } else if (! val_hash
['group no_confirm']) {
854 return confirm(err_obj
.as_string()) ? false : true;
855 } else if (! val_hash
['group no_alert']) {
856 alert(err_obj
.as_string());
858 } else if (! val_hash
['group no_inline']) {
865 document
.load_val_hash = function (form
, val_hash
) {
866 if (! form
) return alert('Missing form or form name');
867 if (typeof(form
) == 'string') {
868 if (! document
[form
]) return alert('No form by name '+form
);
869 form
= document
[form
];
872 if (form
.val_hash
) return form
.val_hash
;
874 if (typeof(val_hash
) != 'object') {
875 if (typeof(val_hash
) == 'function') {
876 val_hash
= val_hash(formname
);
877 } else if (typeof(val_hash
) == 'undefined') {
879 if (typeof(document
.validation
) != 'undefined') {
880 val_hash
= document
.validation
;
881 } else if (el
= document
.getElementById('validation')) {
882 val_hash
= el
.innerHTML
.replace(/</ig,'<').replace(/>/ig,'>').replace(/&/ig,'&');
886 var yaml
= form
.getAttribute('validation');
888 if (m
= yaml
.match(/^( +)/)) yaml
= yaml
.replace(new RegExp('^'+m
[1], 'g'), '');
889 yaml
= yaml
.replace(/\s*$/,'\n');
893 for (var i
= 0; i
< form
.elements
.length
; i
++) {
894 var name
= form
.elements
[i
].name
;
895 var yaml
= form
.elements
[i
].getAttribute('validation');
896 if (! name
|| ! yaml
) continue;
897 yaml
= yaml
.replace(/\s*$/,'\n').replace(/^(.)/mg,' $1').replace(/^( *[^\s
&*\[\{])/,'\n$1');
898 str
+= name
+':' + yaml
;
901 if (str
) val_hash
= str
+ "group order: [" + order
.join(', ') + "]\n";
904 if (typeof(val_hash
) == 'string') {
905 if (! document
.yaml_load
) return;
906 document
.hide_yaml_errors
= (! document
.show_yaml_errors
);
907 if (location
.search
&& location
.search
.indexOf('show_yaml_errors') != -1)
908 document
.hide_yaml_errors
= 0;
909 val_hash
= document
.yaml_load(val_hash
);
910 if (document
.yaml_error_occured
) return;
911 val_hash
= val_hash
[0];
915 form
.val_hash
= val_hash
;
916 return form
.val_hash
;
919 document
.check_form = function (form
, val_hash
) {
920 if (! form
) return alert('Missing form or form name');
921 if (typeof(form
) == 'string') {
922 if (! document
[form
]) return alert('No form by name '+form
);
923 form
= document
[form
];
926 val_hash
= document
.load_val_hash(form
, val_hash
);
927 if (! val_hash
) return;
929 var types
= val_hash
['group onevent'] || {submit:1};
930 if (typeof(types
) == 'string') types
= types
.split(/\s*,\s*/);
931 if (typeof(types
.length
) != 'undefined') {
933 for (var i
= 0; i
< types
.length
; i
++) t
[types
[i
]] = 1;
936 val_hash
['group onevent'] = types
;
938 if (types
.change
|| types
.blur
) {
939 var clean
= v_get_ordered_fields(val_hash
);
940 if (clean
.error
) return clean
.error
;
942 _add = function (k
, v
) { if (! h
[k
]) h
[k
] = []; h
[k
].push(v
) };
943 for (var i
= 0; i
< clean
.fields
.length
; i
++) {
944 _add(clean
.fields
[i
].field
, clean
.fields
[i
]);
945 for (var j
in clean
.fields
[i
].deps
) if (j
!= clean
.fields
[i
].field
) _add(j
, clean
.fields
[i
]);
948 if (k
== 'extend') continue; // Protoype Array()
950 if (! el
) return v_error("No form element by the name "+k
);
951 v_el_attach(el
, h
[k
], form
, val_hash
);
956 var orig_submit
= form
.onsubmit
|| function () { return true };
957 form
.onsubmit = function (e
) { v_event
= 'submit'; return document
.validate(this) && orig_submit(e
, this) };
960 if (types
.load
) { v_event
= 'load'; document
.validate(form
) }
963 function v_el_attach (el
, fvs
, form
, val_hash
) {
965 if (el
.length
) for (var i
= 0; i
< el
.length
; i
++) v_el_attach(el
[i
], fvs
, form
, val_hash
);
968 var types
= val_hash
['group onevent'];
969 var func = function () {
974 for (var i
= 0; i
< fvs
.length
; i
++) {
975 var field_val
= fvs
[i
];
976 var k
= field_val
.field
;
979 val_hash
['group was_checked'][k
] = 1;
980 val_hash
['group was_valid'][k
] = 1;
981 val_hash
['group had_error'][k
] = 0;
983 var _e
= v_validate_buddy(form
, k
, field_val
, val_hash
);
985 val_hash
['group had_error'][k
] = 1;
986 val_hash
['group was_valid'][k
] = 0;
987 for (var j
= 0; j
< _e
.length
; j
++) e
.push(_e
[j
]);
989 f
[field_val
.delegate_error
|| field_val
.field
] = _e
.length
? 0 : 1;
991 for (var k
in f
) if (f
[k
]) v_inline_error_clear(k
, val_hash
, form
);
992 if (! e
.length
) return;
993 e
= new ValidateError(e
, {});
994 e
= e
.as_hash({as_hash_suffix:"", first_only:(val_hash
['group first_only']?1:0)});
996 if (k
== 'extend') continue; // Protoype Array()
997 v_inline_error_set(k
, e
[k
], val_hash
, form
);
1000 if (types
.blur
) el
.onblur
= func
;
1001 if (types
.change
&& ! (''+el
).match(/HTMLCollection/)) { // find better way on opera
1002 var type
= el
.type
? el
.type
.toLowerCase() : '';
1003 if (type
.match(/(password|text|textarea)/)) el
.onkeyup
= func
;
1004 else if (type
.match(/(checkbox|radio)/)) el
.onclick
= func
;
1005 else if (type
.match(/(select)/)) el
.onchange
= func
;
1009 function v_inline_error_clear (key
, val_hash
, form
) {
1010 delete(v_did_inline
[key
]);
1011 var f
= val_hash
['group clear_hook'] || document
.validate_clear_hook
;
1012 var g
= val_hash
['group was_valid'] || {};
1013 if (typeof(f
) == 'function') if (f({'key':key
, 'val_hash':val_hash
, 'form':form
, was_valid:g
[key
], 'event':v_event
})) return 1;
1014 var el
= document
.getElementById(key
+ v_find_val('as_hash_suffix', val_hash
, '_error'));
1015 if (el
) el
.innerHTML
= '';
1018 function v_inline_error_set (key
, val
, val_hash
, form
) {
1019 v_did_inline
[key
] = 1;
1020 var f
= val_hash
['group set_hook'] || document
.validate_set_hook
;
1021 if (typeof(f
) == 'function') if (f({'key':key
, 'value':val
, 'val_hash':val_hash
, 'form':form
, 'event':v_event
})) return 1;
1022 var el
= document
.getElementById(key
+ v_find_val('as_hash_suffix', val_hash
, '_error'));
1023 if (el
) el
.innerHTML
= val
;
This page took 0.106671 seconds and 4 git commands to generate.