]> Dogcows Code - chaz/p5-CGI-Ex/blob - lib/CGI/Ex/validate.js
124cad9dc00bfa64bd4f2f6009af8445f8c76c8a
[chaz/p5-CGI-Ex] / lib / CGI / Ex / validate.js
1 /**----------------------------------------------------------------***
2 * Copyright 2007 - Paul Seamons *
3 * Distributed under the Perl Artistic License without warranty *
4 * Based upon CGI/Ex/Validate.pm v1.14 from Perl *
5 * For instructions on usage, see perldoc of CGI::Ex::Validate *
6 ***----------------------------------------------------------------**/
7 // $Revision: 1.38 $
8
9 function Validate () {
10 this.error = vob_error;
11 this.validate = vob_validate;
12 this.check_conditional = vob_check_conditional;
13 this.filter_types = vob_filter_types;
14 this.add_error = vob_add_error;
15 this.validate_buddy = vob_validate_buddy;
16 this.check_type = vob_check_type;
17 this.get_form_value = vob_get_form_value;
18 }
19
20 function ValidateError (errors, extra) {
21 this.errors = errors;
22 this.extra = extra;
23
24 this.as_string = eob_as_string;
25 this.as_array = eob_as_array;
26 this.as_hash = eob_as_hash;
27 this.get_error_text = eob_get_error_text;
28 this.first_field = eob_first_field;
29 }
30
31 ///----------------------------------------------------------------///
32
33 function vob_error (err) {
34 alert (err);
35 }
36
37 function vob_validate (form, val_hash) {
38 if (typeof(val_hash) == 'string') {
39 if (! document.yaml_load)
40 return this.error("Cannot parse yaml string - document.yaml_load is not loaded");
41 val_hash = document.yaml_load(val_hash);
42 }
43
44 var ERRORS = new Array ();
45 var EXTRA = new Array ();
46 // var USED_GROUPS = new Array();
47
48 // distinguishing between associative and index based arrays is harder than in perl
49 if (! val_hash.length) val_hash = new Array(val_hash);
50 for (var i = 0; i < val_hash.length; i ++) {
51 var group_val = val_hash[i];
52 if (typeof(group_val) != 'object' || group_val.length) return this.error("Validation groups must be a hash");
53 var title = group_val['group title'];
54 var validate_if = group_val['group validate_if'];
55
56 if (validate_if && ! this.check_conditional(form, validate_if)) continue;
57 // USED_GROUPS.push(group_val);
58
59 /// if the validation items were not passed as an arrayref
60 /// look for a group order and then fail back to the keys of the group
61 var fields = group_val['group fields'];
62 var order = new Array();
63 for (var key in group_val) {
64 if (key == 'extend') continue; // Protoype Array() fix
65 order[order.length] = key;
66 }
67 order = order.sort();
68 if (fields) {
69 if (typeof(fields) != 'object' || ! fields.length)
70 return this.error("'group fields' must be a non-empty array");
71 } else {
72 fields = new Array();
73 var _order = (group_val['group order']) ? group_val['group order'] : order;
74 if (typeof(_order) != 'object' || ! _order.length)
75 return this.error("'group order' must be a non-empty array");
76 for (var j = 0; j < _order.length; j ++) {
77 var field = _order[j];
78 if (field.match('^(group|general)\\s')) continue;
79 var field_val = group_val[field];
80 if (! field_val) {
81 if (field == 'OR') field_val = 'OR';
82 else return this.error('No element found in group for '+field);
83 }
84 if (typeof(field_val) == 'object' && ! field_val['field']) field_val['field'] = field;
85 fields[fields.length] = field_val;
86 }
87 }
88
89 /// check which fields have been used
90 var found = new Array();
91 for (var j = 0; j < fields.length; j ++) {
92 var field_val = fields[j];
93 var field = field_val['field'];
94 if (! field) return this.error("Missing field key in validation");
95 // if (found[field]) return this.error('Duplicate order found for '+field+' in group order or fields');
96 found[field] = 1;
97 }
98
99 /// add any remaining fields from the order
100 for (var j = 0; j < order.length; j ++) {
101 var field = order[j];
102 if (found[field] || field.match('^(group|general)\\s')) continue;
103 var field_val = group_val[field];
104 if (typeof(field_val) != 'object' || field_val.length) return this.error('Found a non-hash value on field '+field);
105 if (! field_val['field']) field_val['field'] = field;
106 fields[fields.length] = field_val;
107 }
108
109 /// now lets do the validation
110 var is_found = 1;
111 var errors = new Array();
112 var hold_error;
113
114 for (var j = 0; j < fields.length; j ++) {
115 var ref = fields[j];
116 if (typeof(ref) != 'object' && ref == 'OR') {
117 if (is_found) j ++;
118 is_found = 1;
119 continue;
120 }
121 is_found = 1;
122 if (! ref['field']) return this.error("Missing field key during normal validation");
123 var err = this.validate_buddy(form, ref['field'], ref);
124
125 /// test the error - if errors occur allow for OR - if OR fails use errors from first fail
126 if (err.length) {
127 if (j <= fields.length && typeof(fields[j + 1] != 'object') && fields[j + 1] == 'OR') {
128 hold_error = err;
129 } else {
130 if (hold_error) err = hold_error;
131 for (var k = 0; k < err.length; k ++) errors[errors.length] = err[k];
132 hold_error = '';
133 }
134 } else {
135 hold_error = '';
136 }
137 }
138
139 /// add on errors as requested
140 if (errors.length) {
141 if (title) ERRORS[ERRORS.length] = title;
142 for (var j = 0; j < errors.length; j ++) ERRORS[ERRORS.length] = errors[j];
143 }
144
145 /// add on general options, and group options if errors in group occurred
146 var m;
147 for (var j = 0; j < order.length; j ++) {
148 var field = order[j];
149 if (! (m = field.match('^(general|group)\\s+(\\w+)$'))) continue;
150 if (m[1] == 'group' && (errors.length == 0 || m[2].match('^(field|order|title)$'))) continue;
151 EXTRA[m[2]] = group_val[field];
152 }
153 }
154
155 /// store any extra items from self
156 for (var key in this) {
157 if (key == 'extend') continue; // Protoype Array() fix
158 if (! key.match('_error$')
159 && ! key.match('^(raise_error|as_hash_\\w+|as_array_\\w+|as_string_\\w+)$')) continue;
160 EXTRA[key] = this[key];
161 }
162
163 /// allow for checking for unused keys
164 // if (EXTRA['no_extra_fields'])
165 // won't do anything about this for now - let the server handle it
166
167 /// return what they want
168 if (ERRORS.length) return new ValidateError(ERRORS, EXTRA);
169 return;
170 }
171
172
173 /// allow for optional validation on groups and on individual items
174 function vob_check_conditional (form, ifs, N_level, ifs_match) {
175
176 if (! N_level) N_level = 0;
177 N_level ++;
178
179 /// can pass a single hash - or an array ref of hashes
180 if (! ifs) {
181 return this.error("Need reference passed to check_conditional");
182 } else if (typeof(ifs) != 'object') {
183 ifs = new Array(ifs);
184 } else if (! ifs.length) { // turn hash into array of hash
185 ifs = new Array(ifs);
186 }
187
188 /// run the if options here
189 /// multiple items can be passed - all are required unless OR is used to separate
190 var is_found = 1;
191 var m;
192 for (var i = 0; i < ifs.length; i ++) {
193 var ref = ifs[i];
194 if (typeof(ref) != 'object') {
195 if (ref == 'OR') {
196 if (is_found) i++;
197 is_found = 1;
198 continue;
199 } else {
200 var field = ref;
201 ref = new Array();
202 if (m = field.match('^(\\s*!\\s*)')) {
203 field = field.substring(m[1].length);
204 ref['max_in_set'] = '0 of ' + field;
205 } else {
206 ref['required'] = 1;
207 }
208 ref['field'] = field;
209 }
210 }
211 if (! is_found) break;
212
213 /// get the field - allow for custom variables based upon a match
214 var field = ref['field'];
215 if (! field) return this.error("Missing field key during validate_if");
216 field = field.replace(new RegExp('\\$(\\d+)','g'), function (all, N) {
217 if (typeof(ifs_match) != 'object'
218 || typeof(ifs_match[N]) == 'undefined') return ''
219 return ifs_match[N];
220 });
221
222 var err = this.validate_buddy(form, field, ref, N_level);
223 if (err.length) is_found = 0;
224 }
225 return is_found;
226 }
227
228 function vob_filter_types (type, types) {
229 var values = new Array();
230 var regexp = new RegExp('^'+type+'_?\\d*$');
231 for (var i = 0; i < types.length; i++)
232 if (types[i].match(regexp)) values[values.length] = types[i];
233 return values;
234 }
235
236 function vob_add_error (errors,field,type,field_val,ifs_match) {
237 errors[errors.length] = new Array(field, type, field_val, ifs_match);
238 }
239
240 /// this is where the main checking goes on
241 function vob_validate_buddy (form, field, field_val, N_level, ifs_match) {
242 if (! N_level) N_level = 0;
243 if (++ N_level > 10) return this.error("Max dependency level reached " + N_level);
244 if (! form.elements) return;
245
246 var errors = new Array();
247 var types = new Array();
248 for (var key in field_val) {
249 if (key == 'extend') continue; // Protoype Array() fix
250 types[types.length] = key;
251 }
252 types = types.sort();
253
254 /// allow for not running some tests in the cgi
255 if (this.filter_types('exclude_js', types).length) return errors;
256
257 /// allow for field names that contain regular expressions
258 var m;
259 if (m = field.match('^(!\\s*|)m([^\\s\\w])(.*)\\2([eigsmx]*)$')) {
260 var not = m[1];
261 var pat = m[3];
262 var opt = m[4];
263 if (opt.indexOf('e') != -1) return this.error("The e option cannot be used on field "+field);
264 opt = opt.replace(new RegExp('[sg]','g'),'');
265 var reg = new RegExp(pat, opt);
266
267 var keys = new Array();
268 for (var i = 0; i < form.elements.length; i ++) {
269 var _field = form.elements[i].name;
270 if (! _field) continue;
271 if ( (not && ! (m = _field.match(reg))) || (m = _field.match(reg))) {
272 var err = this.validate_buddy(form, _field, field_val, N_level, m);
273 for (var j = 0; j < err.length; j ++) errors[errors.length] = err[j];
274 }
275 }
276 return errors;
277 }
278
279 var _value = this.get_form_value(form[field]);
280 var values;
281 if (typeof(_value) == 'object') {
282 values = _value;
283 } else {
284 values = new Array();
285 values[values.length] = _value;
286 }
287 var n_values = (typeof(_value) == 'undefined') ? 0 : values.length;
288
289 /// allow for default value
290 var tests = this.filter_types('default', types);
291 if (n_values == 0 || (n_values == 1 && values[0].length == 0)) {
292 for (var i = 0; i < tests.length; i ++) {
293 var el = form[field];
294 var type = el.type;
295 if (type && (type == 'hidden' || type == 'password' || type == 'text' || type == 'textarea' || type == 'submit'))
296 el.value = values[0] = '' + field_val[tests[i]];
297 }
298 }
299
300 /// allow for a few form modifiers
301 var modified = 0;
302 for (var i = 0; i < values.length; i ++) {
303 if (typeof(values[i]) == 'undefined') continue;
304 if (! this.filter_types('do_not_trim',types).length)
305 values[i] = values[i].replace('^\\s+','').replace(new RegExp('\\s+$',''),'');
306 if (this.filter_types('trim_control_chars',types).length)
307 values[i] = values[i].replace(new RegExp('\t', 'g'),' ').replace(new RegExp('[^\x00-\x1F]+','g'),'');
308 if (this.filter_types('to_upper_case',types).length) {
309 values[i] = values[i].toUpperCase();
310 } else if (this.filter_types('to_lower_case',types).length) {
311 values[i] = values[i].toLowerCase();
312 }
313 }
314 var tests = this.filter_types('replace', types);
315 for (var i = 0; i < tests.length; i ++) {
316 var ref = field_val[tests[i]];
317 ref = (typeof(ref) == 'object') ? ref : ref.split(new RegExp('\\s*\\|\\|\\s*'));
318 for (var j = 0; j < ref.length; j ++) {
319 if (! (m = ref[j].match('^\\s*s([^\\s\\w])(.+)\\1(.*)\\1([eigmx]*)$')))
320 return this.error("Not sure how to parse that replace "+ref[j]);
321 var pat = m[2];
322 var swap = m[3];
323 var opt = m[4];
324 if (opt.indexOf('e') != -1)
325 return this.error("The e option cannot be used on field "+field+", replace "+tests[i]);
326 var regexp = new RegExp(pat, opt);
327 for (var k = 0; k < values.length; k ++) {
328 if (values[k].match(regexp)) modified = 1;
329 values[k] = values[k].replace(regexp,swap);
330 }
331 }
332 }
333 if (modified && n_values == 1) {
334 var el = form[field];
335 var type = el.type;
336 if (! type) return '';
337 if (type == 'hidden' || type == 'password' || type == 'text' || type == 'textarea' || type == 'submit')
338 el.value = values[0];
339 }
340
341 /// only continue if a validate_if is not present or passes test
342 var needs_val = 0;
343 var n_vif = 0;
344 var tests = this.filter_types('validate_if', types);
345 for (var i = 0; i < tests.length; i ++) {
346 n_vif ++;
347 var ifs = field_val[tests[i]];
348 var ret = this.check_conditional(form, ifs, N_level, ifs_match);
349 if (ret) needs_val ++;
350 }
351 if (! needs_val && n_vif) return errors;
352
353
354 /// check for simple existence
355 /// optionally check only if another condition is met
356 var is_required = '';
357 var tests = this.filter_types('required', types);
358 for (var i = 0; i < tests.length; i ++) {
359 if (! field_val[tests[i]] || field_val[tests[i]] == 0) continue;
360 is_required = tests[i];
361 break;
362 }
363 if (! is_required) {
364 var tests = this.filter_types('required_if', types);
365 for (var i = 0; i < tests.length; i ++) {
366 var ifs = field_val[tests[i]];
367 if (! this.check_conditional(form, ifs, N_level, ifs_match)) continue;
368 is_required = tests[i];
369 break;
370 }
371 }
372 if (is_required && (typeof(_value) == 'undefined'
373 || ((typeof(_value) == 'object' && _value.length == 0)
374 || ! _value.length))) {
375 this.add_error(errors, field, is_required, field_val, ifs_match);
376 return errors;
377 }
378
379 /// min values check
380 var tests = this.filter_types('min_values', types);
381 for (var i = 0; i < tests.length; i ++) {
382 var n = field_val[tests[i]];
383 if (n_values < n) {
384 this.add_error(errors, field, tests[i], field_val, ifs_match);
385 return errors;
386 }
387 }
388
389 /// max values check
390 var tests = this.filter_types('max_values', types);
391 if (! tests.length) {
392 tests[tests.length] = 'max_values';
393 field_val['max_values'] = 1;
394 }
395 for (var i = 0; i < tests.length; i ++) {
396 var n = field_val[tests[i]];
397 if (n_values > n) {
398 this.add_error(errors, field, tests[i], field_val, ifs_match);
399 return errors;
400 }
401 }
402
403 /// min_in_set and max_in_set check
404 for (var h = 0; h < 2 ; h++) {
405 var minmax = (h == 0) ? 'min' : 'max';
406 var tests = this.filter_types(minmax+'_in_set', types);
407 for (var i = 0; i < tests.length; i ++) {
408 if (! (m = field_val[tests[i]].match('^\\s*(\\d+)(?:\\s*[oO][fF])?\\s+(.+)\\s*$')))
409 return this.error("Invalid in_set check "+field_val[tests[i]]);
410 var n = m[1];
411 var _fields = m[2].split(new RegExp('[\\s,]+'));
412 for (var k = 0; k < _fields.length; k ++) {
413 var _value = this.get_form_value(form[_fields[k]]);
414 var _values;
415 if (typeof(_value) == 'undefined') continue;
416 if (typeof(_value) == 'object') {
417 _values = _value;
418 } else {
419 _values = new Array();
420 _values[_values.length] = _value;
421 }
422 for (var l = 0; l < _values.length; l ++) {
423 var _value = _values[l];
424 if (typeof(_value) != 'undefined' && _value.length) n --;
425 }
426 }
427 if ( (minmax == 'min' && n > 0)
428 || (minmax == 'max' && n < 0)) {
429 this.add_error(errors, field, tests[i], field_val, ifs_match);
430 return errors;
431 }
432 }
433 }
434
435 // the remaining tests operate on each value of a field
436 for (var n = 0; n < values.length; n ++) {
437 var value = values[n];
438
439 /// allow for enum types
440 var tests = this.filter_types('enum', types);
441 for (var i = 0; i < tests.length; i ++) {
442 var hold = field_val[tests[i]];
443 var _enum = (typeof(hold) == 'object') ? hold : hold.split(new RegExp('\\s*\\|\\|\\s*'));
444 var is_found = 0;
445 for (var j = 0; j < _enum.length; j ++) {
446 if (value != _enum[j]) continue;
447 is_found = 1;
448 break;
449 }
450 if (! is_found) this.add_error(errors, field, tests[i], field_val, ifs_match);
451 }
452
453 /// field equality test
454 var tests = this.filter_types('equals', types);
455 for (var i = 0; i < tests.length; i ++) {
456 var field2 = field_val[tests[i]];
457 var not = field2.match('^!\\s*');
458 if (not) field2 = field2.substring(not[0].length);
459 var success = 0;
460 if (m = field2.match('^(["\'])(.*)\\1$')) {
461 if (value == m[2]) success = 1;
462 } else {
463 var value2 = this.get_form_value(form[field2]);
464 if (typeof(value2) == 'undefined') value2 = '';
465 if (value == value2) success = 1;
466 }
467 if (not && success || ! not && ! success)
468 this.add_error(errors, field, tests[i], field_val, ifs_match);
469 }
470
471 /// length min check
472 var tests = this.filter_types('min_len', types);
473 for (var i = 0; i < tests.length; i ++) {
474 var n = field_val[tests[i]];
475 if (value.length < n) this.add_error(errors, field, tests[i], field_val, ifs_match);
476 }
477
478 /// length max check
479 var tests = this.filter_types('max_len', types);
480 for (var i = 0; i < tests.length; i ++) {
481 var n = field_val[tests[i]];
482 if (value.length > n) this.add_error(errors, field, tests[i], field_val, ifs_match);
483 }
484
485 /// now do match types
486 var tests = this.filter_types('match', types);
487 for (var i = 0; i < tests.length; i ++) {
488 var ref = field_val[tests[i]];
489 ref = (typeof(ref) == 'object') ? ref
490 : (typeof(ref) == 'function') ? new Array(ref)
491 : ref.split(new RegExp('\\s*\\|\\|\\s*'));
492 for (var j = 0; j < ref.length; j ++) {
493 if (typeof(ref[j]) == 'function') {
494 if (! value.match(ref[j])) this.add_error(errors, field, tests[i], field_val, ifs_match);
495 } else {
496 if (! (m = ref[j].match('^\\s*(!\\s*|)m([^\\s\\w])(.*)\\2([eigsmx]*)\\s*$')))
497 return this.error("Not sure how to parse that match ("+ref[j]+")");
498 var not = m[1];
499 var pat = m[3];
500 var opt = m[4];
501 if (opt.indexOf('e') != -1)
502 return this.error("The e option cannot be used on field "+field+", test "+tests[i]);
503 opt = opt.replace(new RegExp('[sg]','g'),'');
504 var regexp = new RegExp(pat, opt);
505 if ( ( not && value.match(regexp))
506 || (! not && ! value.match(regexp))) {
507 this.add_error(errors, field, tests[i], field_val, ifs_match);
508 }
509 }
510 }
511 }
512
513 /// allow for comparison checks
514 var tests = this.filter_types('compare', types);
515 for (var i = 0; i < tests.length; i ++) {
516 var ref = field_val[tests[i]];
517 ref = (typeof(ref) == 'object') ? ref : ref.split(new RegExp('\\s*\\|\\|\\s*'));
518 for (var j = 0; j < ref.length; j ++) {
519 var comp = ref[j];
520 if (! comp) continue;
521 var hold = false;
522 var copy = value;
523 if (m = comp.match('^\\s*(>|<|[><!=]=)\\s*([\\d\.\-]+)\\s*$')) {
524 if (! copy) copy = 0;
525 copy *= 1;
526 if (m[1] == '>' ) hold = (copy > m[2])
527 else if (m[1] == '<' ) hold = (copy < m[2])
528 else if (m[1] == '>=') hold = (copy >= m[2])
529 else if (m[1] == '<=') hold = (copy <= m[2])
530 else if (m[1] == '!=') hold = (copy != m[2])
531 else if (m[1] == '==') hold = (copy == m[2])
532 } else if (m = comp.match('^\\s*(eq|ne|gt|ge|lt|le)\\s+(.+?)\\s*$')) {
533 m[2] = m[2].replace('^(["\'])(.*)\\1$','$1');
534 if (m[1] == 'gt') hold = (copy > m[2])
535 else if (m[1] == 'lt') hold = (copy < m[2])
536 else if (m[1] == 'ge') hold = (copy >= m[2])
537 else if (m[1] == 'le') hold = (copy <= m[2])
538 else if (m[1] == 'ne') hold = (copy != m[2])
539 else if (m[1] == 'eq') hold = (copy == m[2])
540 } else {
541 return this.error("Not sure how to compare \""+comp+"\"");
542 }
543 if (! hold) this.add_error(errors, field, tests[i], field_val, ifs_match);
544 }
545 }
546
547 /// do specific type checks
548 var tests = this.filter_types('type',types);
549 for (var i = 0; i < tests.length; i ++)
550 if (! this.check_type(value, field_val[tests[i]], field, form))
551 this.add_error(errors, field, tests[i], field_val, ifs_match);
552
553 /// do custom_js type checks
554 // this will allow for a custom piece of javascript
555 // the js is evaluated and should return 1 for success
556 // or 0 for failure - the variables field, value, and field_val (the hash) are available
557 var tests = this.filter_types('custom_js',types);
558 for (var i = 0; i < tests.length; i ++)
559 if (! eval(field_val[tests[i]]))
560 this.add_error(errors, field, tests[i], field_val, ifs_match);
561 }
562
563 /// all done - time to return
564 return errors;
565 }
566
567 /// used to validate specific types
568 function vob_check_type (value, type, field, form) {
569 var m;
570
571 /// do valid email address for our system
572 if (type == 'EMAIL') {
573 if (! value) return 0;
574 if (! (m = value.match('^(.+)\@(.+?)$'))) return 0;
575 if (m[1].length > 60) return 0;
576 if (m[2].length > 100) return 0;
577 if (! this.check_type(m[2],'DOMAIN') && ! this.check_type(m[2],'IP')) return 0;
578 if (! this.check_type(m[1],'LOCAL_PART')) return 0;
579
580 /// the "username" portion of an email address
581 } else if (type == 'LOCAL_PART') {
582 if (typeof(value) == 'undefined' || ! value.length) return 0;
583 if (! value.match('[^a-z0-9.\\-!&+]')) return 0;
584 if (! value.match('^[.\\-]')) return 0;
585 if (! value.match('[.\\-&]$')) return 0;
586 if (! value.match('(\\.-|-\\.|\\.\\.)')) return 0;
587
588 /// standard IP address
589 } else if (type == 'IP') {
590 if (! value) return 0;
591 var dig = value.split(new RegExp('\\.'));
592 if (dig.length != 4) return 0;
593 for (var i = 0; i < 4; i ++)
594 if (typeof(dig[i]) == 'undefined' || dig[i].match('\\D') || dig[i] > 255) return 0;
595
596 /// domain name - including tld and subdomains (which are all domains)
597 } else if (type == 'DOMAIN') {
598 if (! value) return 0;
599 if (! value.match('^[a-z0-9.-]{4,255}$')) return 0;
600 if (value.match('^[.\\-]')) return 0;
601 if (value.match('(\\.-|-\\.|\\.\\.)')) return 0;
602 if (! (m = value.match('\.([a-z]+)$'))) return 0;
603 value = value.substring(0,value.lastIndexOf('.'));
604
605 if (m[1] == 'name') {
606 if (! value.match('^[a-z0-9][a-z0-9\\-]{0,62}\\.[a-z0-9][a-z0-9\\-]{0,62}$')) return 0;
607 } else
608 if (! value.match('^([a-z0-9][a-z0-9\\-]{0,62}\\.)*[a-z0-9][a-z0-9\\-]{0,62}$')) return 0;
609
610 /// validate a url
611 } else if (type == 'URL') {
612 if (! value) return 0;
613 if (! (m = value.match(new RegExp('^https?://([^/]+)','i'),''))) return 0;
614 value = value.substring(m[0].length);
615 if (! this.check_type(m[1],'DOMAIN') && ! this.check_type(m[1],'IP')) return 0;
616 if (value && ! this.check_type(value,'URI')) return 0;
617
618 /// validate a uri - the path portion of a request
619 } else if (type == 'URI') {
620 if (! value) return 0;
621 if (value.match('\\s')) return 0;
622
623 } else if (type == 'CC') {
624 if (! value) return 0;
625 if (value.match('[^\\d\\- ]') || value.length > 16 || value.length < 13) return;
626 /// simple mod10 check
627 value = value.replace(new RegExp('[\\- ]','g'), '');
628 var sum = 0;
629 var swc = 0;
630
631 for (var i = value.length - 1; i >= 0; i --) {
632 if (++ swc > 2) swc = 1;
633 var y = value.charAt(i) * swc;
634 if (y > 9) y -= 9;
635 sum += y;
636 }
637 if (sum % 10) return 0;
638
639 }
640
641 return 1;
642 }
643
644 // little routine that will get the values from the form
645 // it will return multiple values as an array
646 function vob_get_form_value (el) {
647 if (! el) return '';
648 if (el.disabled) return '';
649 var type = el.type ? el.type.toLowerCase() : '';
650 if (el.length && type != 'select-one') {
651 var a = new Array();
652 for (var j=0;j<el.length;j++) {
653 if (type.indexOf('multiple') != -1) {
654 if (el[j].selected) a[a.length] = el[j].value;
655 } else {
656 if (el[j].checked) a[a.length] = vob_get_form_value(el[j]);
657 }
658 }
659 if (a.length == 0) return '';
660 if (a.length == 1) return a[0];
661 return a;
662 }
663 if (! type) return '';
664 if (type == 'hidden' || type == 'password' || type == 'text' || type == 'textarea' || type == 'submit')
665 return el.value;
666 if (type.indexOf('select') != -1) {
667 if (! el.length) return '';
668 return el[el.selectedIndex].value;
669 }
670 if (type == 'checkbox' || type == 'radio') {
671 return el.checked ? el.value : '';
672 }
673 if (type == 'file') {
674 return el.value; // hope this works
675 }
676 alert('Unknown form type for '+el.name+': '+type);
677 return '';
678 }
679
680 ///----------------------------------------------------------------///
681
682 function eob_get_val (key, extra2, extra1, _default) {
683 if (typeof(extra2[key]) != 'undefined') return extra2[key];
684 if (typeof(extra1[key]) != 'undefined') return extra1[key];
685 return _default;
686 }
687
688 function eob_as_string (extra2) {
689 var extra1 = this.extra;
690 if (! extra2) extra2 = new Array();
691
692 var joiner = eob_get_val('as_string_join', extra2, extra1, '\n');
693 var header = eob_get_val('as_string_header', extra2, extra1, '');
694 var footer = eob_get_val('as_string_footer', extra2, extra1, '');
695
696 return header + this.as_array(extra2).join(joiner) + footer;
697 }
698
699 /// return an array of applicable errors
700 function eob_as_array (extra2) {
701 var errors = this.errors;
702 var extra1 = this.extra;
703 if (! extra2) extra2 = new Array();
704
705 var title = eob_get_val('as_array_title', extra2, extra1, 'Please correct the following items:');
706
707 /// if there are heading items then we may end up needing a prefix
708 var has_headings;
709 if (title) has_headings = 1;
710 else {
711 for (var i = 0; i < errors.length; i ++) {
712 if (typeof(errors[i]) != 'string') continue;
713 has_headings = 1;
714 break;
715 }
716 }
717
718 var prefix = eob_get_val('as_array_prefix', extra2, extra1, has_headings ? ' ' : '');
719
720 /// get the array ready
721 var arr = new Array();
722 if (title && title.length) arr[arr.length] = title;
723 /// add the errors
724 var found = new Array();
725 for (var i = 0; i < errors.length; i ++) {
726 if (typeof(errors[i]) == 'string') {
727 arr[arr.length] = errors[i];
728 found = new Array();
729 } else {
730 var text = this.get_error_text(errors[i]);
731 if (found[text]) continue;
732 found[text] = 1;
733 arr[arr.length] = prefix + text;
734 }
735 }
736
737 return arr;
738 }
739
740 /// return a hash of applicable errors
741 function eob_as_hash (extra2) {
742 var errors = this.errors;
743 var extra1 = this.extra;
744 if (! extra2) extra2 = new Array();
745 var suffix = eob_get_val('as_hash_suffix', extra2, extra1, '_error');
746 var joiner = eob_get_val('as_hash_join', extra2, extra1, '<br />');
747
748 /// now add to the hash
749 var found = new Array();
750 var ret = new Array();
751 for (var i = 0; i < errors.length; i ++) {
752 if (typeof(errors[i]) == 'string') continue;
753 if (! errors[i].length) continue;
754
755 var field = errors[i][0];
756 var type = errors[i][1];
757 var field_val = errors[i][2];
758 var ifs_match = errors[i][3];
759
760 if (! field) return alert("Missing field name");
761 if (field_val['delegate_error']) {
762 field = field_val['delegate_error'];
763 field = field.replace(new RegExp('\\$(\\d+)','g'), function (all, N) {
764 if (typeof(ifs_match) != 'object'
765 || typeof(ifs_match[N]) == 'undefined') return ''
766 return ifs_match[N];
767 });
768 }
769
770 var text = this.get_error_text(errors[i]);
771 if (! found[field]) found[field] = new Array();
772 if (found[field][text]) continue;
773 found[field][text] = 1;
774
775 field += suffix;
776 if (! ret[field]) ret[field] = new Array();
777 ret[field].push(text);
778 }
779
780 /// allow for elements returned as
781 if (joiner) {
782 var header = eob_get_val('as_hash_header', extra2, extra1, '');
783 var footer = eob_get_val('as_hash_footer', extra2, extra1, '');
784 for (var key in ret) {
785 if (key == 'extend') continue; // Protoype Array() fix
786 ret[key] = header + ret[key].join(joiner) + footer;
787 }
788 }
789
790 return ret;
791 }
792
793 /// return a user friendly error message
794 function eob_get_error_text (err) {
795 var extra = this.extra;
796 var field = err[0];
797 var type = err[1];
798 var field_val = err[2];
799 var ifs_match = err[3];
800 var m;
801
802 var dig = (m = type.match('(_?\\d+)$')) ? m[1] : '';
803 var type_lc = type.toLowerCase();
804
805 /// allow for delegated field names - only used for defaults
806 if (field_val['delegate_error']) {
807 field = field_val['delegate_error'];
808 field = field.replace(new RegExp('\\$(\\d+)','g'), function (all, N) {
809 if (typeof(ifs_match) != 'object'
810 || typeof(ifs_match[N]) == 'undefined') return ''
811 return ifs_match[N];
812 });
813 }
814
815 /// the the name of this thing
816 var name = (field_val['name']) ? field_val['name'] : "The field " +field;
817 name = name.replace(new RegExp('\\$(\\d+)','g'), function (all, N) {
818 if (typeof(ifs_match) != 'object'
819 || typeof(ifs_match[N]) == 'undefined') return ''
820 return ifs_match[N];
821 });
822
823
824 /// type can look like "required" or "required2" or "required100023"
825 /// allow for fallback from required100023_error through required_error
826 var possible_keys = new Array(type + '_error');
827 if (dig.length) possible_keys.unshift(type + dig + '_error');
828
829 /// look in the passed hash or self first
830 for (var i = 0; i < possible_keys.length; i ++) {
831 var key = possible_keys[i];
832 var ret = field_val[key];
833 if (! ret) {
834 if (extra[key]) ret = extra[key];
835 else continue;
836 }
837 ret = ret.replace(new RegExp('\\$(\\d+)','g'), function (all, N) {
838 if (typeof(ifs_match) != 'object'
839 || typeof(ifs_match[N]) == 'undefined') return ''
840 return ifs_match[N];
841 });
842 ret = ret.replace(new RegExp('\\$field','g'), field);
843 ret = ret.replace(new RegExp('\\$name' ,'g'), name);
844 if (field_val[type + dig] && typeof(field_val[type + dig]) == 'string')
845 ret = ret.replace(new RegExp('\\$value' ,'g'), field_val[type + dig]);
846 return ret;
847 }
848
849 /// set default messages
850 if (type == 'required' || type == 'required_if') {
851 return name + " is required.";
852
853 } else if (type == 'min_values') {
854 var n = field_val["min_values" + dig];
855 var values = (n == 1) ? 'value' : 'values';
856 return name + " had less than "+n+" "+values+".";
857
858 } else if (type == 'max_values') {
859 var n = field_val["max_values" + dig];
860 var values = (n == 1) ? 'value' : 'values';
861 return name + " had more than "+n+" "+values+".";
862
863 } else if (type == 'min_in_set') {
864 var set = field_val["min_in_set" + dig];
865 return "Not enough fields were chosen from the set ("+set+")";
866 return "Too many fields were chosen from the set ("+set+")";
867
868 } else if (type == 'max_in_set') {
869 var set = field_val["max_in_set" + dig];
870 return "Too many fields were chosen from the set ("+set+")";
871
872 } else if (type == 'enum') {
873 return name + " is not in the given list.";
874
875 } else if (type == 'equals') {
876 var field2 = field_val["equals" + dig];
877 var name2 = field_val["equals" +dig+ "_name"];
878 if (! name2) name2 = "the field " +field2;
879 name2 = name2.replace(new RegExp('\\$(\\d+)','g'), function (all, N) {
880 if (typeof(ifs_match) != 'object'
881 || typeof(ifs_match[N]) == 'undefined') return ''
882 return ifs_match[N];
883 });
884 return name + " did not equal " + name2 +".";
885
886 } else if (type == 'min_len') {
887 var n = field_val["min_len"+dig];
888 var chars = (n == 1) ? 'character' : 'characters';
889 return name + " was less than "+n+" "+chars+".";
890
891 } else if (type == 'max_len') {
892 var n = field_val["max_len"+dig];
893 var chars = (n == 1) ? 'character' : 'characters';
894 return name + " was more than "+n+" "+chars+".";
895
896 } else if (type == 'match') {
897 return name + " contains invalid characters.";
898
899 } else if (type == 'compare') {
900 return name + " did not fit comparison.";
901
902 } else if (type == 'type') {
903 var _type = field_val["type"+dig];
904 return name + " did not match type "+_type+".";
905
906 } else if (type == 'custom_js') {
907 return name + " did not match custom_js"+dig+" check.";
908
909 }
910
911 return alert("Missing error on field "+field+" for type "+type+dig);
912 }
913
914 function eob_first_field () {
915 for (var i = 0; i < this.errors.length; i++) {
916 if (typeof(this.errors[i]) != 'object') continue;
917 if (! this.errors[i][0]) continue;
918 return this.errors[i][0];
919 }
920 return;
921 }
922
923 ///----------------------------------------------------------------///
924
925 document.validate = function (form, val_hash) {
926 // undo previous inline
927 if (document.did_inline) {
928 for (var key in document.did_inline) {
929 if (key == 'extend') continue; // Protoype Array() fix
930 var el = document.getElementById(key);
931 if (el) el.innerHTML = '';
932 }
933 document.did_inline = undefined;
934 }
935
936 // do the validate
937 val_hash = document.load_val_hash(form, val_hash);
938 if (typeof(val_hash) == 'undefined') return true;
939 if (! document.val_obj) document.val_obj = new Validate();
940 var err_obj = document.val_obj.validate(form, val_hash);
941
942 // return success
943 if (! err_obj) return true;
944
945 // focus
946 var field = err_obj.first_field();
947 if (field && form[field] && form[field].focus) form[field].focus();
948
949 // inline
950 if (! err_obj.extra.no_inline) {
951 var d = document.did_inline = new Array();
952 var hash = err_obj.as_hash();
953 for (var key in hash) {
954 if (key == 'extend') continue; // Protoype Array() fix
955 var el = document.getElementById(key);
956 if (el) el.innerHTML = hash[key];
957 d[key] = 1;
958 }
959 }
960
961 // alert
962 if (! err_obj.extra.no_confirm) {
963 return confirm(err_obj.as_string()) ? false : true;
964 } else if (! err_obj.extra.no_alert) {
965 alert(err_obj.as_string());
966 return false;
967 } else if (! err_obj.extra.no_inline) {
968 return false;
969 } else {
970 return true;
971 }
972 }
973
974 document.load_val_hash = function (form, val_hash) {
975 // check the form we are using
976 if (! form) return alert('Missing form or form name');
977 if (typeof(form) == 'string') {
978 if (! document[form]) return alert('No form by name '+form);
979 form = document[form];
980 }
981
982 // if we already have validation - use it
983 if (form.val_hash) return form.val_hash;
984
985 // load in the validation and save it for future use
986 if (typeof(val_hash) != 'object') {
987 // get the hash from a javascript function
988 if (typeof(val_hash) == 'function') {
989 val_hash = val_hash(formname);
990 } else if (typeof(val_hash) == 'undefined') {
991 var el;
992 // get hash from a global js variable
993 if (typeof(document.validation) != 'undefined') {
994 val_hash = document.validation;
995 // get hash from a element by if of validation
996 } else if (el = document.getElementById('validation')) {
997 val_hash = el.innerHTML;
998 val_hash = val_hash.replace(new RegExp('&lt;', 'ig'),'<');
999 val_hash = val_hash.replace(new RegExp('&gt;', 'ig'),'>');
1000 val_hash = val_hash.replace(new RegExp('&amp;','ig'),'&');
1001 // read hash from <input name=foo validation="">
1002 } else {
1003 var order = new Array();
1004 var str = '';
1005 var yaml = form.getAttribute('validation');
1006 if (yaml) {
1007 if (m = yaml.match('^( +)')) yaml = yaml.replace(new RegExp('^'+m[1], 'g'), ''); //unindent
1008 yaml = yaml.replace(new RegExp('\\s*$',''),'\n'); // add trailing
1009 str += yaml;
1010 }
1011 var m;
1012 for (var i = 0; i < form.elements.length; i ++) {
1013 var name = form.elements[i].name;
1014 var yaml = form.elements[i].getAttribute('validation');
1015 if (! name || ! yaml) continue;
1016 yaml = yaml.replace(new RegExp('\\s*$',''),'\n'); // add trailing
1017 yaml = yaml.replace(new RegExp('^(.)','mg'),' $1'); // indent all
1018 yaml = yaml.replace(new RegExp('^( *[^\\s&*\\[\\{])',''),'\n$1'); // add newline
1019 str += name +':' + yaml;
1020 order[order.length] = name;
1021 }
1022 if (str) val_hash = str + "group order: [" + order.join(', ') + "]\n";
1023 }
1024 }
1025 if (typeof(val_hash) == 'string') {
1026 if (! document.yaml_load) return;
1027 document.hide_yaml_errors = (! document.show_yaml_errors);
1028 if (location.search && location.search.indexOf('show_yaml_errors') != -1)
1029 document.hide_yaml_errors = 0;
1030 val_hash = document.yaml_load(val_hash);
1031 if (document.yaml_error_occured) return;
1032 }
1033 }
1034
1035 // attach to the form
1036 form.val_hash = val_hash;
1037 return form.val_hash;
1038 }
1039
1040
1041 document.check_form = function (form, val_hash) {
1042 // check the form we are using
1043 if (! form) return alert('Missing form or form name');
1044 if (typeof(form) == 'string') {
1045 if (! document[form]) return alert('No form by name '+form);
1046 form = document[form];
1047 }
1048
1049 // void call - allow for getting it at run time rather than later
1050 document.load_val_hash(form, val_hash);
1051
1052 // attach handler
1053 var orig_submit = form.onsubmit || function () { return true };
1054 form.onsubmit = function (e) { return document.validate(this) && orig_submit(e, this) };
1055 }
1056
1057 // the end //
This page took 0.098145 seconds and 3 git commands to generate.