]>
Dogcows Code - chaz/p5-CGI-Ex/blob - lib/CGI/Ex/validate.js
a5014afef5d789226419a9055965a986d62f71ab
1 /**----------------------------------------------------------------***
2 * Copyright 2006 - 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 ***----------------------------------------------------------------**/
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
;
20 function ValidateError (errors
, extra
) {
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
;
31 ///----------------------------------------------------------------///
33 function vob_error (err
) {
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
);
44 var ERRORS
= new Array ();
45 var EXTRA
= new Array ();
46 // var USED_GROUPS = new Array();
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'];
56 if (validate_if
&& ! this.check_conditional(form
, validate_if
)) continue;
57 // USED_GROUPS.push(group_val);
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
;
69 if (typeof(fields
) != 'object' || ! fields
.length
)
70 return this.error("'group fields' must be a non-empty 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
];
81 if (field
== 'OR') field_val
= 'OR';
82 else return this.error('No element found in group for '+field
);
84 if (typeof(field_val
) == 'object' && ! field_val
['field']) field_val
['field'] = field
;
85 fields
[fields
.length
] = field_val
;
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');
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
;
109 /// now lets do the validation
111 var errors
= new Array();
114 for (var j
= 0; j
< fields
.length
; j
++) {
116 if (typeof(ref
) != 'object' && ref
== 'OR') {
122 if (! ref
['field']) return this.error("Missing field key during normal validation");
123 var err
= this.validate_buddy(form
, ref
['field'], ref
);
125 /// test the error - if errors occur allow for OR - if OR fails use errors from first fail
127 if (j
<= fields
.length
&& typeof(fields
[j
+ 1] != 'object') && fields
[j
+ 1] == 'OR') {
130 if (hold_error
) err
= hold_error
;
131 for (var k
= 0; k
< err
.length
; k
++) errors
[errors
.length
] = err
[k
];
139 /// add on errors as requested
141 if (title
) ERRORS
[ERRORS
.length
] = title
;
142 for (var j
= 0; j
< errors
.length
; j
++) ERRORS
[ERRORS
.length
] = errors
[j
];
145 /// add on general options, and group options if errors in group occurred
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
];
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
];
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
167 /// return what they want
168 if (ERRORS
.length
) return new ValidateError(ERRORS
, EXTRA
);
173 /// allow for optional validation on groups and on individual items
174 function vob_check_conditional (form
, ifs
, N_level
, ifs_match
) {
176 if (! N_level
) N_level
= 0;
179 /// can pass a single hash - or an array ref of hashes
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
);
188 /// run the if options here
189 /// multiple items can be passed - all are required unless OR is used to separate
192 for (var i
= 0; i
< ifs
.length
; i
++) {
194 if (typeof(ref
) != 'object') {
202 if (m
= field
.match('^(\\s*!\\s*)')) {
203 field
= field
.substring(m
[1].length
);
204 ref
['max_in_set'] = '0 of ' + field
;
208 ref
['field'] = field
;
211 if (! is_found
) break;
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 ''
222 var err
= this.validate_buddy(form
, field
, ref
, N_level
);
223 if (err
.length
) is_found
= 0;
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
];
236 function vob_add_error (errors
,field
,type
,field_val
,ifs_match
) {
237 errors
[errors
.length
] = new Array(field
, type
, field_val
, ifs_match
);
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;
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
;
252 types
= types
.sort();
254 /// allow for not running some tests in the cgi
255 if (this.filter_types('exclude_js', types
).length
) return errors
;
257 /// allow for field names that contain regular expressions
259 if (m
= field
.match('^(!\\s*|)m([^\\s\\w])(.*)\\2([eigsmx]*)$')) {
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
);
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
];
279 var _value
= this.get_form_value(form
[field
]);
281 if (typeof(_value
) == 'object') {
284 values
= new Array();
285 values
[values
.length
] = _value
;
287 var n_values
= (typeof(_value
) == 'undefined') ? 0 : values
.length
;
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
];
295 if (type
&& (type
== 'hidden' || type
== 'password' || type
== 'text' || type
== 'textarea' || type
== 'submit')) el
.value
= values
[0] = field_val
[tests
[i
]];
299 /// allow for a few form modifiers
301 for (var i
= 0; i
< values
.length
; i
++) {
302 if (typeof(values
[i
]) == 'undefined') continue;
303 if (! this.filter_types('do_not_trim',types
).length
)
304 values
[i
] = values
[i
].replace('^\\s+','').replace(new RegExp('\\s+$',''),'');
305 if (this.filter_types('to_upper_case',types
).length
) {
306 values
[i
] = values
[i
].toUpperCase();
307 } else if (this.filter_types('to_lower_case',types
).length
) {
308 values
[i
] = values
[i
].toLowerCase();
311 var tests
= this.filter_types('replace', types
);
312 for (var i
= 0; i
< tests
.length
; i
++) {
313 var ref
= field_val
[tests
[i
]];
314 ref
= (typeof(ref
) == 'object') ? ref : ref
.split(new RegExp('\\s*\\|\\|\\s*'));
315 for (var j
= 0; j
< ref
.length
; j
++) {
316 if (! (m
= ref
[j
].match('^\\s*s([^\\s\\w])(.+)\\1(.*)\\1([eigmx]*)$')))
317 return this.error("Not sure how to parse that replace "+ref
[j
]);
321 if (opt
.indexOf('e') != -1)
322 return this.error("The e option cannot be used on field "+field
+", replace "+tests
[i
]);
323 var regexp
= new RegExp(pat
, opt
);
324 for (var k
= 0; k
< values
.length
; k
++) {
325 if (values
[k
].match(regexp
)) modified
= 1;
326 values
[k
] = values
[k
].replace(regexp
,swap
);
330 if (modified
&& n_values
== 1) {
331 var el
= form
[field
];
333 if (! type
) return '';
334 if (type
== 'hidden' || type
== 'password' || type
== 'text' || type
== 'textarea' || type
== 'submit')
335 el
.value
= values
[0];
338 /// only continue if a validate_if is not present or passes test
341 var tests
= this.filter_types('validate_if', types
);
342 for (var i
= 0; i
< tests
.length
; i
++) {
344 var ifs
= field_val
[tests
[i
]];
345 var ret
= this.check_conditional(form
, ifs
, N_level
, ifs_match
);
346 if (ret
) needs_val
++;
348 if (! needs_val
&& n_vif
) return errors
;
351 /// check for simple existence
352 /// optionally check only if another condition is met
353 var is_required
= '';
354 var tests
= this.filter_types('required', types
);
355 for (var i
= 0; i
< tests
.length
; i
++) {
356 if (! field_val
[tests
[i
]] || field_val
[tests
[i
]] == 0) continue;
357 is_required
= tests
[i
];
361 var tests
= this.filter_types('required_if', types
);
362 for (var i
= 0; i
< tests
.length
; i
++) {
363 var ifs
= field_val
[tests
[i
]];
364 if (! this.check_conditional(form
, ifs
, N_level
, ifs_match
)) continue;
365 is_required
= tests
[i
];
369 if (is_required
&& (typeof(_value
) == 'undefined'
370 || ((typeof(_value
) == 'object' && _value
.length
== 0)
371 || ! _value
.length
))) {
372 this.add_error(errors
, field
, is_required
, field_val
, ifs_match
);
377 var tests
= this.filter_types('min_values', types
);
378 for (var i
= 0; i
< tests
.length
; i
++) {
379 var n
= field_val
[tests
[i
]];
381 this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
387 var tests
= this.filter_types('max_values', types
);
388 if (! tests
.length
) {
389 tests
[tests
.length
] = 'max_values';
390 field_val
['max_values'] = 1;
392 for (var i
= 0; i
< tests
.length
; i
++) {
393 var n
= field_val
[tests
[i
]];
395 this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
400 /// min_in_set and max_in_set check
401 for (var h
= 0; h
< 2 ; h
++) {
402 var minmax
= (h
== 0) ? 'min' : 'max';
403 var tests
= this.filter_types(minmax
+'_in_set', types
);
404 for (var i
= 0; i
< tests
.length
; i
++) {
405 if (! (m
= field_val
[tests
[i
]].match('^\\s*(\\d+)(?:\\s*[oO][fF])?\\s+(.+)\\s*$')))
406 return this.error("Invalid in_set check "+field_val
[tests
[i
]]);
408 var _fields
= m
[2].split(new RegExp('[\\s,]+'));
409 for (var k
= 0; k
< _fields
.length
; k
++) {
410 var _value
= this.get_form_value(form
[_fields
[k
]]);
412 if (typeof(_value
) == 'undefined') continue;
413 if (typeof(_value
) == 'object') {
416 _values
= new Array();
417 _values
[_values
.length
] = _value
;
419 for (var l
= 0; l
< _values
.length
; l
++) {
420 var _value
= _values
[l
];
421 if (typeof(_value
) != 'undefined' && _value
.length
) n
--;
424 if ( (minmax
== 'min' && n
> 0)
425 || (minmax
== 'max' && n
< 0)) {
426 this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
432 // the remaining tests operate on each value of a field
433 for (var n
= 0; n
< values
.length
; n
++) {
434 var value
= values
[n
];
436 /// allow for enum types
437 var tests
= this.filter_types('enum', types
);
438 for (var i
= 0; i
< tests
.length
; i
++) {
439 var hold
= field_val
[tests
[i
]];
440 var _enum
= (typeof(hold
) == 'object') ? hold : hold
.split(new RegExp('\\s*\\|\\|\\s*'));
442 for (var j
= 0; j
< _enum
.length
; j
++) {
443 if (value
!= _enum
[j
]) continue;
447 if (! is_found
) this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
450 /// field equality test
451 var tests
= this.filter_types('equals', types
);
452 for (var i
= 0; i
< tests
.length
; i
++) {
453 var field2
= field_val
[tests
[i
]];
454 var not
= field2
.match('^!\\s*');
455 if (not
) field2
= field2
.substring(not
[0].length
);
457 if (m
= field2
.match('^(["\'])(.*)\\1$')) {
458 if (value
== m
[2]) success
= 1;
460 var value2
= this.get_form_value(form
[field2
]);
461 if (typeof(value2
) == 'undefined') value2
= '';
462 if (value
== value2
) success
= 1;
464 if (not
&& success
|| ! not
&& ! success
)
465 this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
469 var tests
= this.filter_types('min_len', types
);
470 for (var i
= 0; i
< tests
.length
; i
++) {
471 var n
= field_val
[tests
[i
]];
472 if (value
.length
< n
) this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
476 var tests
= this.filter_types('max_len', types
);
477 for (var i
= 0; i
< tests
.length
; i
++) {
478 var n
= field_val
[tests
[i
]];
479 if (value
.length
> n
) this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
482 /// now do match types
483 var tests
= this.filter_types('match', types
);
484 for (var i
= 0; i
< tests
.length
; i
++) {
485 var ref
= field_val
[tests
[i
]];
486 ref
= (typeof(ref
) == 'object') ? ref
487 : (typeof(ref
) == 'function') ? new Array(ref
)
488 : ref
.split(new RegExp('\\s*\\|\\|\\s*'));
489 for (var j
= 0; j
< ref
.length
; j
++) {
490 if (typeof(ref
[j
]) == 'function') {
491 if (! value
.match(ref
[j
])) this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
493 if (! (m
= ref
[j
].match('^\\s*(!\\s*|)m([^\\s\\w])(.*)\\2([eigsmx]*)\\s*$')))
494 return this.error("Not sure how to parse that match ("+ref
[j
]+")");
498 if (opt
.indexOf('e') != -1)
499 return this.error("The e option cannot be used on field "+field
+", test "+tests
[i
]);
500 opt
= opt
.replace(new RegExp('[sg]','g'),'');
501 var regexp
= new RegExp(pat
, opt
);
502 if ( ( not
&& value
.match(regexp
))
503 || (! not
&& ! value
.match(regexp
))) {
504 this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
510 /// allow for comparison checks
511 var tests
= this.filter_types('compare', types
);
512 for (var i
= 0; i
< tests
.length
; i
++) {
513 var ref
= field_val
[tests
[i
]];
514 ref
= (typeof(ref
) == 'object') ? ref : ref
.split(new RegExp('\\s*\\|\\|\\s*'));
515 for (var j
= 0; j
< ref
.length
; j
++) {
517 if (! comp
) continue;
520 if (m
= comp
.match('^\\s*(>|<|[><!=]=)\\s*([\\d\.\-]+)\\s*$')) {
521 if (! copy
) copy
= 0;
523 if (m
[1] == '>' ) hold
= (copy
> m
[2])
524 else if (m
[1] == '<' ) hold
= (copy
< m
[2])
525 else if (m
[1] == '>=') hold
= (copy
>= m
[2])
526 else 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
= comp
.match('^\\s*(eq|ne|gt|ge|lt|le)\\s+(.+?)\\s*$')) {
530 m
[2] = m
[2].replace('^(["\'])(.*)\\1$','$1');
531 if (m
[1] == 'gt') hold
= (copy
> m
[2])
532 else if (m
[1] == 'lt') hold
= (copy
< m
[2])
533 else if (m
[1] == 'ge') hold
= (copy
>= m
[2])
534 else if (m
[1] == 'le') hold
= (copy
<= m
[2])
535 else if (m
[1] == 'ne') hold
= (copy
!= m
[2])
536 else if (m
[1] == 'eq') hold
= (copy
== m
[2])
538 return this.error("Not sure how to compare \""+comp
+"\"");
540 if (! hold
) this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
544 /// do specific type checks
545 var tests
= this.filter_types('type',types
);
546 for (var i
= 0; i
< tests
.length
; i
++)
547 if (! this.check_type(value
, field_val
[tests
[i
]], field
, form
))
548 this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
550 /// do custom_js type checks
551 // this will allow for a custom piece of javascript
552 // the js is evaluated and should return 1 for success
553 // or 0 for failure - the variables field, value, and field_val (the hash) are available
554 var tests
= this.filter_types('custom_js',types
);
555 for (var i
= 0; i
< tests
.length
; i
++)
556 if (! eval(field_val
[tests
[i
]]))
557 this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
560 /// all done - time to return
564 /// used to validate specific types
565 function vob_check_type (value
, type
, field
, form
) {
568 /// do valid email address for our system
569 if (type
== 'EMAIL') {
570 if (! value
) return 0;
571 if (! (m
= value
.match('^(.+)\@(.+?)$'))) return 0;
572 if (m
[1].length
> 60) return 0;
573 if (m
[2].length
> 100) return 0;
574 if (! this.check_type(m
[2],'DOMAIN') && ! this.check_type(m
[2],'IP')) return 0;
575 if (! this.check_type(m
[1],'LOCAL_PART')) return 0;
577 /// the "username" portion of an email address
578 } else if (type
== 'LOCAL_PART') {
579 if (typeof(value
) == 'undefined' || ! value
.length
) return 0;
580 if (! value
.match('[^a-z0-9.\\-!&+]')) return 0;
581 if (! value
.match('^[.\\-]')) return 0;
582 if (! value
.match('[.\\-&]$')) return 0;
583 if (! value
.match('(\\.-|-\\.|\\.\\.)')) return 0;
585 /// standard IP address
586 } else if (type
== 'IP') {
587 if (! value
) return 0;
588 var dig
= value
.split(new RegExp('\\.'));
589 if (dig
.length
!= 4) return 0;
590 for (var i
= 0; i
< 4; i
++)
591 if (typeof(dig
[i
]) == 'undefined' || dig
[i
].match('\\D') || dig
[i
] > 255) return 0;
593 /// domain name - including tld and subdomains (which are all domains)
594 } else if (type
== 'DOMAIN') {
595 if (! value
) return 0;
596 if (! value
.match('^[a-z0-9.-]{4,255}$')) return 0;
597 if (value
.match('^[.\\-]')) return 0;
598 if (value
.match('(\\.-|-\\.|\\.\\.)')) return 0;
599 if (! (m
= value
.match('\.([a-z]+)$'))) return 0;
600 value
= value
.substring(0,value
.lastIndexOf('.'));
602 if (m
[1] == 'name') {
603 if (! value
.match('^[a-z0-9][a-z0-9\\-]{0,62}\\.[a-z0-9][a-z0-9\\-]{0,62}$')) return 0;
605 if (! value
.match('^([a-z0-9][a-z0-9\\-]{0,62}\\.)*[a-z0-9][a-z0-9\\-]{0,62}$')) return 0;
608 } else if (type
== 'URL') {
609 if (! value
) return 0;
610 if (! (m
= value
.match(new RegExp('^https?://([^/]+)','i'),''))) return 0;
611 value
= value
.substring(m
[0].length
);
612 if (! this.check_type(m
[1],'DOMAIN') && ! this.check_type(m
[1],'IP')) return 0;
613 if (value
&& ! this.check_type(value
,'URI')) return 0;
615 /// validate a uri - the path portion of a request
616 } else if (type
== 'URI') {
617 if (! value
) return 0;
618 if (value
.match('\\s')) return 0;
620 } else if (type
== 'CC') {
621 if (! value
) return 0;
622 if (value
.match('[^\\d\\- ]') || value
.length
> 16 || value
.length
< 13) return;
623 /// simple mod10 check
624 value
= value
.replace(new RegExp('[\\- ]','g'), '');
628 for (var i
= value
.length
- 1; i
>= 0; i
--) {
629 if (++ swc
> 2) swc
= 1;
630 var y
= value
.charAt(i
) * swc
;
634 if (sum
% 10) return 0;
641 // little routine that will get the values from the form
642 // it will return multiple values as an array
643 function vob_get_form_value (el
) {
645 if (el
.disabled
) return '';
646 var type
= el
.type
? el
.type
.toLowerCase() : '';
647 if (el
.length
&& type
!= 'select-one') {
649 for (var j
=0;j
<el
.length
;j
++) {
650 if (type
.indexOf('multiple') != -1) {
651 if (el
[j
].selected
) a
[a
.length
] = el
[j
].value
;
653 if (el
[j
].checked
) a
[a
.length
] = vob_get_form_value(el
[j
]);
656 if (a
.length
== 0) return '';
657 if (a
.length
== 1) return a
[0];
660 if (! type
) return '';
661 if (type
== 'hidden' || type
== 'password' || type
== 'text' || type
== 'textarea' || type
== 'submit')
663 if (type
.indexOf('select') != -1) {
664 if (! el
.length
) return '';
665 return el
[el
.selectedIndex
].value
;
667 if (type
== 'checkbox' || type
== 'radio') {
668 return el
.checked
? el
.value : '';
670 if (type
== 'file') {
671 return el
.value
; // hope this works
673 alert('Unknown form type for '+el
.name
+': '+type
);
677 ///----------------------------------------------------------------///
679 function eob_get_val (key
, extra2
, extra1
, _default
) {
680 if (typeof(extra2
[key
]) != 'undefined') return extra2
[key
];
681 if (typeof(extra1
[key
]) != 'undefined') return extra1
[key
];
685 function eob_as_string (extra2
) {
686 var extra1
= this.extra
;
687 if (! extra2
) extra2
= new Array();
689 var joiner
= eob_get_val('as_string_join', extra2
, extra1
, '\n');
690 var header
= eob_get_val('as_string_header', extra2
, extra1
, '');
691 var footer
= eob_get_val('as_string_footer', extra2
, extra1
, '');
693 return header
+ this.as_array(extra2
).join(joiner
) + footer
;
696 /// return an array of applicable errors
697 function eob_as_array (extra2
) {
698 var errors
= this.errors
;
699 var extra1
= this.extra
;
700 if (! extra2
) extra2
= new Array();
702 var title
= eob_get_val('as_array_title', extra2
, extra1
, 'Please correct the following items:');
704 /// if there are heading items then we may end up needing a prefix
706 if (title
) has_headings
= 1;
708 for (var i
= 0; i
< errors
.length
; i
++) {
709 if (typeof(errors
[i
]) != 'string') continue;
715 var prefix
= eob_get_val('as_array_prefix', extra2
, extra1
, has_headings
? ' ' : '');
717 /// get the array ready
718 var arr
= new Array();
719 if (title
&& title
.length
) arr
[arr
.length
] = title
;
721 var found
= new Array();
722 for (var i
= 0; i
< errors
.length
; i
++) {
723 if (typeof(errors
[i
]) == 'string') {
724 arr
[arr
.length
] = errors
[i
];
727 var text
= this.get_error_text(errors
[i
]);
728 if (found
[text
]) continue;
730 arr
[arr
.length
] = prefix
+ text
;
737 /// return a hash of applicable errors
738 function eob_as_hash (extra2
) {
739 var errors
= this.errors
;
740 var extra1
= this.extra
;
741 if (! extra2
) extra2
= new Array();
742 var suffix
= eob_get_val('as_hash_suffix', extra2
, extra1
, '_error');
743 var joiner
= eob_get_val('as_hash_join', extra2
, extra1
, '<br />');
745 /// now add to the hash
746 var found
= new Array();
747 var ret
= new Array();
748 for (var i
= 0; i
< errors
.length
; i
++) {
749 if (typeof(errors
[i
]) == 'string') continue;
750 if (! errors
[i
].length
) continue;
752 var field
= errors
[i
][0];
753 var type
= errors
[i
][1];
754 var field_val
= errors
[i
][2];
755 var ifs_match
= errors
[i
][3];
757 if (! field
) return alert("Missing field name");
758 if (field_val
['delegate_error']) {
759 field
= field_val
['delegate_error'];
760 field
= field
.replace(new RegExp('\\$(\\d+)','g'), function (all
, N
) {
761 if (typeof(ifs_match
) != 'object'
762 || typeof(ifs_match
[N
]) == 'undefined') return ''
767 var text
= this.get_error_text(errors
[i
]);
768 if (! found
[field
]) found
[field
] = new Array();
769 if (found
[field
][text
]) continue;
770 found
[field
][text
] = 1;
773 if (! ret
[field
]) ret
[field
] = new Array();
774 ret
[field
].push(text
);
777 /// allow for elements returned as
779 var header
= eob_get_val('as_hash_header', extra2
, extra1
, '');
780 var footer
= eob_get_val('as_hash_footer', extra2
, extra1
, '');
781 for (var key
in ret
) {
782 if (key
== 'extend') continue; // Protoype Array() fix
783 ret
[key
] = header
+ ret
[key
].join(joiner
) + footer
;
790 /// return a user friendly error message
791 function eob_get_error_text (err
) {
792 var extra
= this.extra
;
795 var field_val
= err
[2];
796 var ifs_match
= err
[3];
799 var dig
= (m
= type
.match('(_?\\d+)$')) ? m
[1] : '';
800 var type_lc
= type
.toLowerCase();
802 /// allow for delegated field names - only used for defaults
803 if (field_val
['delegate_error']) {
804 field
= field_val
['delegate_error'];
805 field
= field
.replace(new RegExp('\\$(\\d+)','g'), function (all
, N
) {
806 if (typeof(ifs_match
) != 'object'
807 || typeof(ifs_match
[N
]) == 'undefined') return ''
812 /// the the name of this thing
813 var name
= (field_val
['name']) ? field_val
['name'] : "The field " +field
;
814 name
= name
.replace(new RegExp('\\$(\\d+)','g'), function (all
, N
) {
815 if (typeof(ifs_match
) != 'object'
816 || typeof(ifs_match
[N
]) == 'undefined') return ''
821 /// type can look like "required" or "required2" or "required100023"
822 /// allow for fallback from required100023_error through required_error
823 var possible_keys
= new Array(type
+ '_error');
824 if (dig
.length
) possible_keys
.unshift(type
+ dig
+ '_error');
826 /// look in the passed hash or self first
827 for (var i
= 0; i
< possible_keys
.length
; i
++) {
828 var key
= possible_keys
[i
];
829 var ret
= field_val
[key
];
831 if (extra
[key
]) ret
= extra
[key
];
834 ret
= ret
.replace(new RegExp('\\$(\\d+)','g'), function (all
, N
) {
835 if (typeof(ifs_match
) != 'object'
836 || typeof(ifs_match
[N
]) == 'undefined') return ''
839 ret
= ret
.replace(new RegExp('\\$field','g'), field
);
840 ret
= ret
.replace(new RegExp('\\$name' ,'g'), name
);
841 if (field_val
[type
+ dig
] && typeof(field_val
[type
+ dig
]) == 'string')
842 ret
= ret
.replace(new RegExp('\\$value' ,'g'), field_val
[type
+ dig
]);
846 /// set default messages
847 if (type
== 'required' || type
== 'required_if') {
848 return name
+ " is required.";
850 } else if (type
== 'min_values') {
851 var n
= field_val
["min_values" + dig
];
852 var values
= (n
== 1) ? 'value' : 'values';
853 return name
+ " had less than "+n
+" "+values
+".";
855 } else if (type
== 'max_values') {
856 var n
= field_val
["max_values" + dig
];
857 var values
= (n
== 1) ? 'value' : 'values';
858 return name
+ " had more than "+n
+" "+values
+".";
860 } else if (type
== 'min_in_set') {
861 var set = field_val
["min_in_set" + dig
];
862 return "Not enough fields were chosen from the set ("+set+")";
863 return "Too many fields were chosen from the set ("+set+")";
865 } else if (type
== 'max_in_set') {
866 var set = field_val
["max_in_set" + dig
];
867 return "Too many fields were chosen from the set ("+set+")";
869 } else if (type
== 'enum') {
870 return name
+ " is not in the given list.";
872 } else if (type
== 'equals') {
873 var field2
= field_val
["equals" + dig
];
874 var name2
= field_val
["equals" +dig
+ "_name"];
875 if (! name2
) name2
= "the field " +field2
;
876 name2
= name2
.replace(new RegExp('\\$(\\d+)','g'), function (all
, N
) {
877 if (typeof(ifs_match
) != 'object'
878 || typeof(ifs_match
[N
]) == 'undefined') return ''
881 return name
+ " did not equal " + name2
+".";
883 } else if (type
== 'min_len') {
884 var n
= field_val
["min_len"+dig
];
885 var chars
= (n
== 1) ? 'character' : 'characters';
886 return name
+ " was less than "+n
+" "+chars
+".";
888 } else if (type
== 'max_len') {
889 var n
= field_val
["max_len"+dig
];
890 var chars
= (n
== 1) ? 'character' : 'characters';
891 return name
+ " was more than "+n
+" "+chars
+".";
893 } else if (type
== 'match') {
894 return name
+ " contains invalid characters.";
896 } else if (type
== 'compare') {
897 return name
+ " did not fit comparison.";
899 } else if (type
== 'type') {
900 var _type
= field_val
["type"+dig
];
901 return name
+ " did not match type "+_type
+".";
903 } else if (type
== 'custom_js') {
904 return name
+ " did not match custom_js"+dig
+" check.";
908 return alert("Missing error on field "+field
+" for type "+type
+dig
);
911 function eob_first_field () {
912 for (var i
= 0; i
< this.errors
.length
; i
++) {
913 if (typeof(this.errors
[i
]) != 'object') continue;
914 if (! this.errors
[i
][0]) continue;
915 return this.errors
[i
][0];
920 ///----------------------------------------------------------------///
922 document
.validate = function (form
, val_hash
) {
923 // undo previous inline
924 if (document
.did_inline
) {
925 for (var key
in document
.did_inline
) {
926 if (key
== 'extend') continue; // Protoype Array() fix
927 var el
= document
.getElementById(key
);
928 if (el
) el
.innerHTML
= '';
930 document
.did_inline
= undefined;
934 val_hash
= document
.load_val_hash(form
, val_hash
);
935 if (typeof(val_hash
) == 'undefined') return true;
936 if (! document
.val_obj
) document
.val_obj
= new Validate();
937 var err_obj
= document
.val_obj
.validate(form
, val_hash
);
940 if (! err_obj
) return true;
943 var field
= err_obj
.first_field();
944 if (field
&& form
[field
] && form
[field
].focus
) form
[field
].focus();
947 if (! err_obj
.extra
.no_inline
) {
948 var d
= document
.did_inline
= new Array();
949 var hash
= err_obj
.as_hash();
950 for (var key
in hash
) {
951 if (key
== 'extend') continue; // Protoype Array() fix
952 var el
= document
.getElementById(key
);
953 if (el
) el
.innerHTML
= hash
[key
];
959 if (! err_obj
.extra
.no_confirm
) {
960 return confirm(err_obj
.as_string()) ? false : true;
961 } else if (! err_obj
.extra
.no_alert
) {
962 alert(err_obj
.as_string());
964 } else if (! err_obj
.extra
.no_inline
) {
971 document
.load_val_hash = function (form
, val_hash
) {
972 // check the form we are using
973 if (! form
) return alert('Missing form or form name');
974 if (typeof(form
) == 'string') {
975 if (! document
[form
]) return alert('No form by name '+form
);
976 form
= document
[form
];
979 // if we already have validation - use it
980 if (form
.val_hash
) return form
.val_hash
;
982 // load in the validation and save it for future use
983 if (typeof(val_hash
) != 'object') {
984 // get the hash from a javascript function
985 if (typeof(val_hash
) == 'function') {
986 val_hash
= val_hash(formname
);
987 } else if (typeof(val_hash
) == 'undefined') {
989 // get hash from a global js variable
990 if (typeof(document
.validation
) != 'undefined') {
991 val_hash
= document
.validation
;
992 // get hash from a element by if of validation
993 } else if (el
= document
.getElementById('validation')) {
994 val_hash
= el
.innerHTML
;
995 val_hash
= val_hash
.replace(new RegExp('<', 'ig'),'<');
996 val_hash
= val_hash
.replace(new RegExp('>', 'ig'),'>');
997 val_hash
= val_hash
.replace(new RegExp('&','ig'),'&');
998 // read hash from <input name=foo validation="">
1000 var order
= new Array();
1002 var yaml
= form
.getAttribute('validation');
1004 if (m
= yaml
.match('^( +)')) yaml
= yaml
.replace(new RegExp('^'+m
[1], 'g'), ''); //unindent
1005 yaml
= yaml
.replace(new RegExp('\\s*$',''),'\n'); // add trailing
1009 for (var i
= 0; i
< form
.elements
.length
; i
++) {
1010 var name
= form
.elements
[i
].name
;
1011 var yaml
= form
.elements
[i
].getAttribute('validation');
1012 if (! name
|| ! yaml
) continue;
1013 yaml
= yaml
.replace(new RegExp('\\s*$',''),'\n'); // add trailing
1014 yaml
= yaml
.replace(new RegExp('^(.)','mg'),' $1'); // indent all
1015 yaml
= yaml
.replace(new RegExp('^( *[^\\s&*\\[\\{])',''),'\n$1'); // add newline
1016 str
+= name
+':' + yaml
;
1017 order
[order
.length
] = name
;
1019 if (str
) val_hash
= str
+ "group order: [" + order
.join(', ') + "]\n";
1022 if (typeof(val_hash
) == 'string') {
1023 if (! document
.yaml_load
) return;
1024 document
.hide_yaml_errors
= (! document
.show_yaml_errors
);
1025 if (location
.search
&& location
.search
.indexOf('show_yaml_errors') != -1)
1026 document
.hide_yaml_errors
= 0;
1027 val_hash
= document
.yaml_load(val_hash
);
1028 if (document
.yaml_error_occured
) return;
1032 // attach to the form
1033 form
.val_hash
= val_hash
;
1034 return form
.val_hash
;
1038 document
.check_form = function (form
, val_hash
) {
1039 // check the form we are using
1040 if (! form
) return alert('Missing form or form name');
1041 if (typeof(form
) == 'string') {
1042 if (! document
[form
]) return alert('No form by name '+form
);
1043 form
= document
[form
];
1046 // void call - allow for getting it at run time rather than later
1047 document
.load_val_hash(form
, val_hash
);
1050 form
.onsubmit = function () {return document
.validate(this)};
This page took 0.112027 seconds and 4 git commands to generate.