]> Dogcows Code - chaz/p5-CGI-Ex/blob - lib/CGI/Ex/validate.js
CGI::Ex 2.27
[chaz/p5-CGI-Ex] / lib / CGI / Ex / validate.js
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
4
5 var v_did_inline = {};
6 var v_event;
7
8 function ValidateError (errors, extra) {
9 this.errors = errors;
10 this.extra = 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;
15 }
16
17 //
18
19 function v_error (err) { alert (err); return 1 }
20
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)")};
23
24 var ARGS = {};
25 var field_keys = [];
26 var m;
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];
31 continue;
32 }
33 field_keys.push(key);
34 }
35 field_keys = field_keys.sort();
36
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);
41
42 if (f = ARGS.validate_if) {
43 if (typeof(f) == 'string' || ! f.length) f = [f];
44 var deps = v_clean_cond(f);
45 }
46
47 var fields = [];
48 var ref;
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++) {
53 var field = ref[i];
54 if (typeof(field) == 'object') {
55 if (! field.field) return {error:v_error("Missing field key in validation")};
56 fields.push(field);
57 } else if (field == 'OR') {
58 fields.push('OR');
59 } else {
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);
64 }
65 }
66 }
67
68 var found = {};
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;
73 }
74
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);
82 }
83
84 for (var i = 0; i < fields.length; i++) v_clean_field_val(fields[i]);
85
86 val_hash['group was_checked'] = {};
87 val_hash['group was_valid'] = {};
88 val_hash['group had_error'] = {};
89
90 return {'fields':fields, 'args':ARGS};
91 }
92
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];
98 var v = field_val[k];
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]);
105 }
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]+")")};
115 var not = m[1];
116 var pat = m[3];
117 var opt = m[4];
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, '!');
122 }
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;
129 }
130 }
131 }
132
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 [] }
137
138 var deps = {};
139 var m;
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};
148 }
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;
154 }
155 return deps;
156 }
157
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;
162
163 var ERRORS = [];
164 var EXTRA = [];
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;
168
169 var is_found = 1;
170 var errors = [];
171 var hold_error;
172
173 var chk = {};
174 for (var j = 0; j < fields.length; j++) {
175 var ref = fields[j];
176 if (typeof(ref) != 'object' && ref == 'OR') {
177 if (is_found) j++;
178 is_found = 1;
179 continue;
180 }
181 is_found = 1;
182 var f = ref.field;
183 if (! chk[f]) {
184 chk[f] = 1;
185 val_hash['group was_checked'][f] = 1;
186 val_hash['group was_valid'][f] = 1;
187 val_hash['group had_error'][f] = 0;
188 }
189 var err = v_validate_buddy(form, f, ref, val_hash);
190 if (err.length) {
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') {
194 hold_error = err;
195 } else {
196 if (hold_error) err = hold_error;
197 for (var k = 0; k < err.length; k++) errors.push(err[k]);
198 hold_error = '';
199 }
200 } else {
201 hold_error = '';
202 }
203 }
204
205 if (errors.length) {
206 if (title) ERRORS.push(title);
207 for (var j = 0; j < errors.length; j++) ERRORS.push(errors[j]);
208 }
209
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];
213 }
214
215 if (ERRORS.length) return new ValidateError(ERRORS, EXTRA);
216 return;
217 }
218
219 function v_check_conditional (form, ifs, val_hash, ifs_match) {
220 var is_ok = 1;
221 for (var i = 0; i < ifs.length; i++) {
222 if (typeof(ifs[i]) == 'function') {
223 if (! is_ok) break;
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 }
227 if (is_ok) i++;
228 is_ok = 1;
229 continue;
230 } else {
231 if (! is_ok) break;
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];
235 });
236 var err = v_validate_buddy(form, field, ifs[i], val_hash);
237 if (err.length) is_ok = 0;
238 }
239 }
240 return is_ok;
241 }
242
243 function v_filter_types (type, types) {
244 var values = [];
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]);
248 return values;
249 }
250
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 = '';
256 }
257 return errors;
258 }
259
260 function v_field_order (field_val) {
261 var o = [];
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);
264 return o.sort();
265 }
266
267 function v_validate_buddy (form, field, field_val, val_hash, ifs_match) {
268 var errors = [];
269 if (! form.elements || field_val.exclude_js) return [];
270 var types = field_val.order || v_field_order(field_val);
271
272 var m;
273 if (m = field.match(/^(!\s*|)m([^\s\w])(.*)\2([eigsmx]*)$/)) {
274 var not = m[1];
275 var pat = m[3];
276 var opt = m[4];
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);
280
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]);
287 }
288 }
289 return errors;
290 }
291
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);
295
296 var _value = v_get_form_value(form[field]);
297 var modified = 0;
298
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'];
304 modified = 1;
305 }
306
307 var values = (typeof(_value) == 'object') ? _value : [_value];
308 var n_values = (typeof(_value) == 'undefined') ? 0 : values.length;
309
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();
317
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]);
325 var pat = m[2];
326 var swap = m[3];
327 var opt = m[4];
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);
331 }
332 }
333
334 if (orig != values[i]) modified = 1;
335 }
336 if (modified) {
337 var el = form[field];
338 if (el) v_set_form_value(el, values);
339 }
340
341
342 var needs_val = 0;
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++;
348 }
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;
352 return [];
353 }
354 if (field_val.vif_disable) v_set_disable(form[field], false);
355
356 var is_required = field_val['required'] ? 'required' : '';
357 if (! is_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];
363 break;
364 }
365 }
366 if (is_required) {
367 var found;
368 for (var i = 0; i < values.length; i++) {
369 if (values[i].length) {
370 found = 1;
371 break;
372 }
373 }
374 if (! found) return v_add_error(errors, field, is_required, field_val, ifs_match, form);
375 }
376
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);
379
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);
383
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]];
389 var n = a[0];
390 for (var k = 1; k < a.length; k++) {
391 var _value = v_get_form_value(form[a[k]]);
392 var _values;
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--;
398 }
399 }
400 if ( (minmax == 'min' && n > 0)
401 || (minmax == 'max' && n < 0)) {
402 v_add_error(errors, field, tests[i], field_val, ifs_match, form);
403 return errors;
404 }
405 }
406 }
407
408 for (var i = 0; i < types.length; i++) {
409 var type = types[i];
410 var _fv = field_val[type];
411 for (var n = 0; n < values.length; n++) {
412 var value = values[n];
413
414 if (type.match(/^enum\d*$/)) {
415 var is_found = 0;
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);
418 }
419
420 if (type.match(/^equals\d*$/)) {
421 var not = _fv.match(/^!\s*/);
422 if (not) _fv = _fv.substring(not[0].length);
423 var success = 0;
424 if (m = _fv.match(/^([\"\'])(.*)\1$/)) {
425 if (value == m[2]) success = 1;
426 } else {
427 var value2 = v_get_form_value(form[_fv]);
428 if (typeof(value2) == 'undefined') value2 = '';
429 if (value == value2) success = 1;
430 }
431 if (not && success || ! not && ! success)
432 v_add_error(errors, field, type, field_val, ifs_match, form);
433 }
434
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);
437
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);
444 }
445 }
446
447 if (type.match(/^compare\d*$/)) {
448 for (var j = 0; j < _fv.length; j++) {
449 var comp = _fv[j];
450 if (! comp) continue;
451 var hold = false;
452 var copy = value;
453 if (m = comp.match(/^\s*(>|<|[><!=]=)\s*([\d\.\-]+)\s*$/)) {
454 if (! copy) copy = 0;
455 copy *= 1;
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])
471 } else {
472 v_error("Not sure how to compare \""+comp+"\"");
473 return errors;
474 }
475 if (! hold) v_add_error(errors, field, type, field_val, ifs_match, form);
476 }
477 }
478
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);
482 }
483
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);
491 }
492 }
493
494 return errors;
495 }
496
497 function v_check_type (value, type, field, form) {
498 var m;
499 type = type.toUpperCase();
500
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;
508
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;
516
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;
523
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;
531
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;
539
540 } else if (type == 'URI') {
541 if (! value) return 0;
542 if (value.match(/\s/)) return 0;
543
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;
549 // mod10
550 var sum = 0;
551 var swc = 0;
552 for (var i = value.length - 1; i >= 0; i--) {
553 if (++swc > 2) swc = 1;
554 var y = value.charAt(i) * swc;
555 if (y > 9) y -= 9;
556 sum += y;
557 }
558 if (sum % 10) return 0;
559 }
560
561 return 1;
562 }
563
564 function v_set_form_value (el, values, form) {
565 if (typeof(el) == 'string') el = form[el];
566 if (typeof(values) != 'object') values = [values];
567 if (! el) return;
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]] : ['']));
573 }
574 return;
575 }
576 if (! type) return;
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;
580 return;
581 }
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;
585 }
586 if (type == 'file') return;
587
588 alert('Unknown form type for '+el.name+': '+type);
589 return;
590 }
591
592 function v_set_disable (el, disable) {
593 if (! el) return
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;
598 }
599
600 function v_get_form_value (el, form) {
601 if (typeof(el) == 'string') el = form[el];
602 if (! el) return '';
603 var type = (el.type && ! (''+el).match(/HTMLCollection/)) ? el.type.toLowerCase() : '';
604 if (el.length && type != 'select-one') {
605 var a = [];
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);
609 } else {
610 if (el[j].checked) a.push(v_get_form_value(el[j]));
611 }
612 }
613 if (a.length == 0) return '';
614 if (a.length == 1) return a[0];
615 return a;
616 }
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;
623 }
624 if (type == 'checkbox' || type == 'radio') return el.checked ? el.value : '';
625 if (type == 'file') return el.value;
626
627 alert('Unknown form type for '+el.name+': '+type);
628 return '';
629 }
630
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];
637 }
638 return '';
639 }
640
641 function v_get_error_text (err, extra1, extra2) {
642 var field = err[0];
643 var type = err[1];
644 var field_val = err[2];
645 var ifs_match = err[3];
646 var m;
647
648 var dig = '';
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];
652
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 ''
658 return ifs_match[N];
659 });
660 }
661
662 var name = field_val.name;
663 if (! name && (field.match(/\W/) || (field.match(/\d/) && field.match(/\D/)))) {
664 name = "The field " +field;
665 }
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 ''
670 return ifs_match[N];
671 });
672
673 var msg = v_find_val(type + '_error', extra1, extra2);
674 if (! msg) {
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'];
678 }
679 if (msg) {
680 msg = msg.replace(/\$(\d+)/g, function (all, N) {
681 if (typeof(ifs_match) != 'object' || typeof(ifs_match[N]) == 'undefined') return '';
682 return ifs_match[N];
683 });
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);
687 return msg;
688 }
689
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];
696 });
697 return name + " did not equal " + name2 +".";
698 }
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*/,'')+")";
701
702 return name + (
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));
717 }
718
719 //
720
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;
726 }
727
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:');
731
732 var has_headings;
733 if (title) has_headings = 1;
734 else for (var i = 0; i < errors.length; i++) if (typeof(errors[i]) == 'string') has_headings = 1;
735
736 var prefix = v_find_val('as_array_prefix', extra, this.extra, (has_headings ? ' ' : ''));
737
738 var arr = [];
739 if (title && title.length) arr.push(title);
740
741 var found = {};
742 for (var i = 0; i < errors.length; i++) {
743 if (typeof(errors[i]) == 'string') {
744 arr.push(errors[i]);
745 found = {};
746 } else {
747 var text = v_get_error_text(errors[i], extra, this.extra);
748 if (found[text]) continue;
749 found[text] = 1;
750 arr.push(prefix + text);
751 }
752 }
753
754 return arr;
755 }
756
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/>');
761
762 var found = {};
763 var ret = {};
764 for (var i = 0; i < errors.length; i++) {
765 if (typeof(errors[i]) == 'string') continue;
766 if (! errors[i].length) continue;
767
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];
772
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 ''
779 return ifs_match[N];
780 });
781 }
782
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;
787
788 field += suffix;
789 if (! ret[field]) ret[field] = [];
790 ret[field].push(text);
791 }
792
793 if (joiner) {
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;
799 }
800 }
801
802 return ret;
803 }
804
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];
810 }
811 return;
812 }
813
814 //
815
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;
819
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);
824 }
825 }
826
827 var err_obj = v_validate(form, val_hash);
828 if (! err_obj) {
829 var f = val_hash['group clear_all_hook'] || document.validate_clear_all_hook;
830 if (f) f();
831 return true;
832 }
833
834 var f = val_hash['group set_all_hook'] || document.validate_set_all_hook;
835 if (f) f(err_obj);
836
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();
841 }
842
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);
848 }
849 }
850
851 if (v_event == 'load') {
852 return false;
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());
857 return false;
858 } else if (! val_hash['group no_inline']) {
859 return false;
860 } else {
861 return true;
862 }
863 }
864
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];
870 }
871
872 if (form.val_hash) return form.val_hash;
873
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') {
878 var el;
879 if (typeof(document.validation) != 'undefined') {
880 val_hash = document.validation;
881 } else if (el = document.getElementById('validation')) {
882 val_hash = el.innerHTML.replace(/&lt;/ig,'<').replace(/&gt;/ig,'>').replace(/&amp;/ig,'&');
883 } else {
884 var order = [];
885 var str = '';
886 var yaml = form.getAttribute('validation');
887 if (yaml) {
888 if (m = yaml.match(/^( +)/)) yaml = yaml.replace(new RegExp('^'+m[1], 'g'), '');
889 yaml = yaml.replace(/\s*$/,'\n');
890 str += yaml;
891 }
892 var m;
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;
899 order.push(name);
900 }
901 if (str) val_hash = str + "group order: [" + order.join(', ') + "]\n";
902 }
903 }
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];
912 }
913 }
914
915 form.val_hash = val_hash;
916 return form.val_hash;
917 }
918
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];
924 }
925
926 val_hash = document.load_val_hash(form, val_hash);
927 if (! val_hash) return;
928
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') {
932 var t = {};
933 for (var i = 0; i < types.length; i++) t[types[i]] = 1;
934 types = t;
935 }
936 val_hash['group onevent'] = types;
937
938 if (types.change || types.blur) {
939 var clean = v_get_ordered_fields(val_hash);
940 if (clean.error) return clean.error;
941 var h = {};
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]);
946 }
947 for (var k in h) {
948 if (k == 'extend') continue; // Protoype Array()
949 var el = form[k];
950 if (! el) return v_error("No form element by the name "+k);
951 v_el_attach(el, h[k], form, val_hash);
952 }
953 }
954
955 if (types.submit) {
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) };
958 }
959
960 if (types.load) { v_event = 'load'; document.validate(form) }
961 }
962
963 function v_el_attach (el, fvs, form, val_hash) {
964 if (! el.type) {
965 if (el.length) for (var i = 0; i < el.length; i++) v_el_attach(el[i], fvs, form, val_hash);
966 return;
967 }
968 var types = val_hash['group onevent'];
969 var func = function () {
970 v_event = 'change';
971 var e = [];
972 var f = {};
973 var chk = {};
974 for (var i = 0; i < fvs.length; i++) {
975 var field_val = fvs[i];
976 var k = field_val.field;
977 if (! chk[k]) {
978 chk[k] = 1;
979 val_hash['group was_checked'][k] = 1;
980 val_hash['group was_valid'][k] = 1;
981 val_hash['group had_error'][k] = 0;
982 }
983 var _e = v_validate_buddy(form, k, field_val, val_hash);
984 if (_e.length) {
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]);
988 }
989 f[field_val.delegate_error || field_val.field] = _e.length ? 0 : 1;
990 }
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)});
995 for (var k in e) {
996 if (k == 'extend') continue; // Protoype Array()
997 v_inline_error_set(k, e[k], val_hash, form);
998 }
999 };
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;
1006 }
1007 }
1008
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 = '';
1016 }
1017
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;
1024 }
This page took 0.103 seconds and 4 git commands to generate.