]> Dogcows Code - chaz/p5-CGI-Ex/blob - lib/CGI/Ex/validate.js
CGI::Ex 2.24
[chaz/p5-CGI-Ex] / 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
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_clean_val_hash (val_hash) {
22 if (typeof(val_hash) != 'object') return {error: v_error("Validation must be an associative array (hash)")};
23
24 var order = [];
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]);
31 key = new_key;
32 }
33 order.push(key);
34 }
35 order = order.sort();
36
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);
41
42 if (f = val_hash['group validate_if']) {
43 if (typeof(f) == 'string' || ! f.length) f = [f];
44 var deps = v_clean_cond(f);
45 }
46
47 var fields = val_hash['group fields'];
48 if (fields) {
49 if (typeof(fields) != 'object' || ! fields.length)
50 return {error:v_error("'group fields' must be a non-empty array")};
51 } else {
52 fields = [];
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];
60 if (! field_val) {
61 if (field == 'OR') field_val = 'OR';
62 else return {error:v_error('No element found in group for '+field)};
63 }
64 if (typeof(field_val) == 'object' && ! field_val['field']) field_val['field'] = field;
65 fields.push(field_val);
66 }
67 }
68
69 var found = {};
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")};
74 found[field] = 1;
75 }
76
77 for (var i = 0; i < order.length; i++) {
78 var field = order[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);
84 }
85
86 for (var i = 0; i < fields.length; i++) v_clean_field_val(fields[i]);
87
88 val_hash['group was_checked'] = {};
89 val_hash['group was_valid'] = {};
90 val_hash['group had_error'] = {};
91
92 return {'fields':fields, 'order':order};
93 }
94
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]);
107 }
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]+")")};
117 var not = m[1];
118 var pat = m[3];
119 var opt = m[4];
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, '!');
124 }
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;
131 }
132 }
133 }
134
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 [] }
139
140 var deps = {};
141 var m;
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};
150 }
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;
156 }
157 return deps;
158 }
159
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;
165
166 var ERRORS = [];
167 var EXTRA = [];
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;
171
172 var is_found = 1;
173 var errors = [];
174 var hold_error;
175
176 var chk = {};
177 for (var j = 0; j < fields.length; j++) {
178 var ref = fields[j];
179 if (typeof(ref) != 'object' && ref == 'OR') {
180 if (is_found) j++;
181 is_found = 1;
182 continue;
183 }
184 is_found = 1;
185 var f = ref.field;
186 if (! chk[f]) {
187 chk[f] = 1;
188 val_hash['group was_checked'][f] = 1;
189 val_hash['group was_valid'][f] = 1;
190 val_hash['group had_error'][f] = 0;
191 }
192 var err = v_validate_buddy(form, f, ref, val_hash);
193 if (err.length) {
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') {
197 hold_error = err;
198 } else {
199 if (hold_error) err = hold_error;
200 for (var k = 0; k < err.length; k++) errors.push(err[k]);
201 hold_error = '';
202 }
203 } else {
204 hold_error = '';
205 }
206 }
207
208 if (errors.length) {
209 if (title) ERRORS.push(title);
210 for (var j = 0; j < errors.length; j++) ERRORS.push(errors[j]);
211 }
212
213 var m;
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];
219 }
220
221 if (ERRORS.length) return new ValidateError(ERRORS, EXTRA);
222 return;
223 }
224
225 function v_check_conditional (form, ifs, val_hash, ifs_match) {
226 var is_ok = 1;
227 for (var i = 0; i < ifs.length; i++) {
228 if (typeof(ifs[i]) == 'function') {
229 if (! is_ok) break;
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 }
233 if (is_ok) i++;
234 is_ok = 1;
235 continue;
236 } else {
237 if (! is_ok) break;
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];
241 });
242 var err = v_validate_buddy(form, field, ifs[i], val_hash);
243 if (err.length) is_ok = 0;
244 }
245 }
246 return is_ok;
247 }
248
249 function v_filter_types (type, types) {
250 var values = [];
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]);
254 return values;
255 }
256
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 = '';
262 }
263 return errors;
264 }
265
266 function v_field_order (field_val) {
267 var o = [];
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);
270 return o.sort();
271 }
272
273 function v_validate_buddy (form, field, field_val, val_hash, ifs_match) {
274 var errors = [];
275 if (! form.elements || field_val.exclude_js) return [];
276 var types = field_val.order || v_field_order(field_val);
277
278 var m;
279 if (m = field.match(/^(!\s*|)m([^\s\w])(.*)\2([eigsmx]*)$/)) {
280 var not = m[1];
281 var pat = m[3];
282 var opt = m[4];
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);
286
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]);
293 }
294 }
295 return errors;
296 }
297
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);
301
302 var _value = v_get_form_value(form[field]);
303 var modified = 0;
304
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'];
310 modified = 1;
311 }
312
313 var values = (typeof(_value) == 'object') ? _value : [_value];
314 var n_values = (typeof(_value) == 'undefined') ? 0 : values.length;
315
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();
323
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]);
331 var pat = m[2];
332 var swap = m[3];
333 var opt = m[4];
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);
337 }
338 }
339
340 if (orig != values[i]) modified = 1;
341 }
342 if (modified) {
343 var el = form[field];
344 if (el) v_set_form_value(el, values);
345 }
346
347
348 var needs_val = 0;
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++;
354 }
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;
358 return [];
359 }
360 if (field_val.vif_disable) v_set_disable(form[field], false);
361
362 var is_required = field_val['required'] ? 'required' : '';
363 if (! is_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];
369 break;
370 }
371 }
372 if (is_required) {
373 var found;
374 for (var i = 0; i < values.length; i++) {
375 if (values[i].length) {
376 found = 1;
377 break;
378 }
379 }
380 if (! found) return v_add_error(errors, field, is_required, field_val, ifs_match, form);
381 }
382
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);
385
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);
389
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]];
395 var n = a[0];
396 for (var k = 1; k < a.length; k++) {
397 var _value = v_get_form_value(form[a[k]]);
398 var _values;
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--;
404 }
405 }
406 if ( (minmax == 'min' && n > 0)
407 || (minmax == 'max' && n < 0)) {
408 v_add_error(errors, field, tests[i], field_val, ifs_match, form);
409 return errors;
410 }
411 }
412 }
413
414 for (var i = 0; i < types.length; i++) {
415 var type = types[i];
416 var _fv = field_val[type];
417 for (var n = 0; n < values.length; n++) {
418 var value = values[n];
419
420 if (type.match(/^enum\d*$/)) {
421 var is_found = 0;
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);
424 }
425
426 if (type.match(/^equals\d*$/)) {
427 var not = _fv.match(/^!\s*/);
428 if (not) _fv = _fv.substring(not[0].length);
429 var success = 0;
430 if (m = _fv.match(/^([\"\'])(.*)\1$/)) {
431 if (value == m[2]) success = 1;
432 } else {
433 var value2 = v_get_form_value(form[_fv]);
434 if (typeof(value2) == 'undefined') value2 = '';
435 if (value == value2) success = 1;
436 }
437 if (not && success || ! not && ! success)
438 v_add_error(errors, field, type, field_val, ifs_match, form);
439 }
440
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);
443
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);
450 }
451 }
452
453 if (type.match(/^compare\d*$/)) {
454 for (var j = 0; j < _fv.length; j++) {
455 var comp = _fv[j];
456 if (! comp) continue;
457 var hold = false;
458 var copy = value;
459 if (m = comp.match(/^\s*(>|<|[><!=]=)\s*([\d\.\-]+)\s*$/)) {
460 if (! copy) copy = 0;
461 copy *= 1;
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])
477 } else {
478 v_error("Not sure how to compare \""+comp+"\"");
479 return errors;
480 }
481 if (! hold) v_add_error(errors, field, type, field_val, ifs_match, form);
482 }
483 }
484
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);
488 }
489
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);
497 }
498 }
499
500 return errors;
501 }
502
503 function v_check_type (value, type, field, form) {
504 var m;
505 type = type.toUpperCase();
506
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;
514
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;
522
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;
529
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;
537
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;
545
546 } else if (type == 'URI') {
547 if (! value) return 0;
548 if (value.match(/\s/)) return 0;
549
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;
555 // mod10
556 var sum = 0;
557 var swc = 0;
558 for (var i = value.length - 1; i >= 0; i--) {
559 if (++swc > 2) swc = 1;
560 var y = value.charAt(i) * swc;
561 if (y > 9) y -= 9;
562 sum += y;
563 }
564 if (sum % 10) return 0;
565 }
566
567 return 1;
568 }
569
570 function v_set_form_value (el, values, form) {
571 if (typeof(el) == 'string') el = form[el];
572 if (typeof(values) != 'object') values = [values];
573 if (! el) return;
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]] : ['']));
579 }
580 return;
581 }
582 if (! type) return;
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;
586 return;
587 }
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;
591 }
592 if (type == 'file') return;
593
594 alert('Unknown form type for '+el.name+': '+type);
595 return;
596 }
597
598 function v_set_disable (el, disable) {
599 if (! el) return
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;
604 }
605
606 function v_get_form_value (el, form) {
607 if (typeof(el) == 'string') el = form[el];
608 if (! el) return '';
609 var type = (el.type && ! (''+el).match(/HTMLCollection/)) ? el.type.toLowerCase() : '';
610 if (el.length && type != 'select-one') {
611 var a = [];
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);
615 } else {
616 if (el[j].checked) a.push(v_get_form_value(el[j]));
617 }
618 }
619 if (a.length == 0) return '';
620 if (a.length == 1) return a[0];
621 return a;
622 }
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;
629 }
630 if (type == 'checkbox' || type == 'radio') return el.checked ? el.value : '';
631 if (type == 'file') return el.value;
632
633 alert('Unknown form type for '+el.name+': '+type);
634 return '';
635 }
636
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];
643 }
644 return '';
645 }
646
647 function v_get_error_text (err, extra1, extra2) {
648 var field = err[0];
649 var type = err[1];
650 var field_val = err[2];
651 var ifs_match = err[3];
652 var m;
653
654 var dig = '';
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];
658
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 ''
664 return ifs_match[N];
665 });
666 }
667
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 ''
672 return ifs_match[N];
673 });
674
675 var msg = v_find_val(type + '_error', extra1, extra2);
676 if (! msg) {
677 if (dig.length) msg = field_val[type + dig + '_error'];
678 if (! msg) msg = field_val[type + '_error'];
679 }
680 if (msg) {
681 msg = msg.replace(/\$(\d+)/g, function (all, N) {
682 if (typeof(ifs_match) != 'object' || typeof(ifs_match[N]) == 'undefined') return '';
683 return ifs_match[N];
684 });
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);
688 return msg;
689 }
690
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];
697 });
698 return name + " did not equal " + name2 +".";
699 }
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*/,'')+")";
702
703 return name + (
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));
718 }
719
720 //
721
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;
727 }
728
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:');
732
733 var has_headings;
734 if (title) has_headings = 1;
735 else for (var i = 0; i < errors.length; i++) if (typeof(errors[i]) == 'string') has_headings = 1;
736
737 var prefix = v_find_val('as_array_prefix', extra, this.extra, (has_headings ? ' ' : ''));
738
739 var arr = [];
740 if (title && title.length) arr.push(title);
741
742 var found = {};
743 for (var i = 0; i < errors.length; i++) {
744 if (typeof(errors[i]) == 'string') {
745 arr.push(errors[i]);
746 found = {};
747 } else {
748 var text = v_get_error_text(errors[i], extra, this.extra);
749 if (found[text]) continue;
750 found[text] = 1;
751 arr.push(prefix + text);
752 }
753 }
754
755 return arr;
756 }
757
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/>');
762
763 var found = {};
764 var ret = {};
765 for (var i = 0; i < errors.length; i++) {
766 if (typeof(errors[i]) == 'string') continue;
767 if (! errors[i].length) continue;
768
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];
773
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 ''
780 return ifs_match[N];
781 });
782 }
783
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;
788
789 field += suffix;
790 if (! ret[field]) ret[field] = [];
791 ret[field].push(text);
792 }
793
794 if (joiner) {
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;
800 }
801 }
802
803 return ret;
804 }
805
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];
811 }
812 return;
813 }
814
815 //
816
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;
820
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);
825 }
826 }
827
828 var err_obj = v_validate(form, val_hash);
829 if (! err_obj) return true;
830
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();
835 }
836
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);
842 }
843 }
844
845 if (v_event == 'load') {
846 return false;
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());
851 return false;
852 } else if (! val_hash['group no_inline']) {
853 return false;
854 } else {
855 return true;
856 }
857 }
858
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];
864 }
865
866 if (form.val_hash) return form.val_hash;
867
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') {
872 var el;
873 if (typeof(document.validation) != 'undefined') {
874 val_hash = document.validation;
875 } else if (el = document.getElementById('validation')) {
876 val_hash = el.innerHTML.replace(/&lt;/ig,'<').replace(/&gt;/ig,'>').replace(/&amp;/ig,'&');
877 } else {
878 var order = [];
879 var str = '';
880 var yaml = form.getAttribute('validation');
881 if (yaml) {
882 if (m = yaml.match(/^( +)/)) yaml = yaml.replace(new RegExp('^'+m[1], 'g'), '');
883 yaml = yaml.replace(/\s*$/,'\n');
884 str += yaml;
885 }
886 var m;
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;
893 order.push(name);
894 }
895 if (str) val_hash = str + "group order: [" + order.join(', ') + "]\n";
896 }
897 }
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];
906 }
907 }
908
909 form.val_hash = val_hash;
910 return form.val_hash;
911 }
912
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];
918 }
919
920 val_hash = document.load_val_hash(form, val_hash);
921 if (! val_hash) return;
922
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') {
926 var t = {};
927 for (var i = 0; i < types.length; i++) t[types[i]] = 1;
928 types = t;
929 }
930 val_hash['group onevent'] = types;
931
932 if (types.change || types.blur) {
933 var clean = v_clean_val_hash(val_hash);
934 if (clean.error) return clean.error;
935 var h = {};
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]);
940 }
941 for (var k in h) {
942 if (k == 'extend') continue; // Protoype Array()
943 var el = form[k];
944 if (! el) return v_error("No form element by the name "+k);
945 v_el_attach(el, h[k], form, val_hash);
946 }
947 }
948
949 if (types.submit) {
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) };
952 }
953
954 if (types.load) { v_event = 'load'; document.validate(form) }
955 }
956
957 function v_el_attach (el, fvs, form, val_hash) {
958 if (! el.type) {
959 if (el.length) for (var i = 0; i < el.length; i++) v_el_attach(el[i], fvs, form, val_hash);
960 return;
961 }
962 var types = val_hash['group onevent'];
963 var func = function () {
964 v_event = 'change';
965 var e = [];
966 var f = {};
967 var chk = {};
968 for (var i = 0; i < fvs.length; i++) {
969 var field_val = fvs[i];
970 var k = field_val.field;
971 if (! chk[k]) {
972 chk[k] = 1;
973 val_hash['group was_checked'][k] = 1;
974 val_hash['group was_valid'][k] = 1;
975 val_hash['group had_error'][k] = 0;
976 }
977 var _e = v_validate_buddy(form, k, field_val, val_hash);
978 if (_e.length) {
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]);
982 }
983 f[field_val.delegate_error || field_val.field] = _e.length ? 0 : 1;
984 }
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)});
989 for (var k in e) {
990 if (k == 'extend') continue; // Protoype Array()
991 v_inline_error_set(k, e[k], val_hash, form);
992 }
993 };
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;
1000 }
1001 }
1002
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 = '';
1010 }
1011
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;
1018 }
This page took 0.119891 seconds and 4 git commands to generate.