]>
Dogcows Code - chaz/p5-CGI-Ex/blob - lib/CGI/Ex/validate.js
8d4c296aee4d37ab42bc9221799dd53121164813
1 /**----------------------------------------------------------------***
2 * Copyright 2004 - 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
) order
[order
.length
] = key
;
66 if (typeof(fields
) != 'object' || ! fields
.length
)
67 return this.error("'group fields' must be a non-empty array");
70 var _order
= (group_val
['group order']) ? group_val
['group order'] : order
;
71 if (typeof(_order
) != 'object' || ! _order
.length
)
72 return this.error("'group order' must be a non-empty array");
73 for (var j
= 0; j
< _order
.length
; j
++) {
74 var field
= _order
[j
];
75 if (field
.match('^(group|general)\\s')) continue;
76 var field_val
= group_val
[field
];
78 if (field
== 'OR') field_val
= 'OR';
79 else return this.error('No element found in group for '+field
);
81 if (typeof(field_val
) == 'object' && ! field_val
['field']) field_val
['field'] = field
;
82 fields
[fields
.length
] = field_val
;
86 /// check which fields have been used
87 var found
= new Array();
88 for (var j
= 0; j
< fields
.length
; j
++) {
89 var field_val
= fields
[j
];
90 var field
= field_val
['field'];
91 if (! field
) return this.error("Missing field key in validation");
92 // if (found[field]) return this.error('Duplicate order found for '+field+' in group order or fields');
96 /// add any remaining fields from the order
97 for (var j
= 0; j
< order
.length
; j
++) {
99 if (found
[field
] || field
.match('^(group|general)\\s')) continue;
100 var field_val
= group_val
[field
];
101 if (typeof(field_val
) != 'object' || field_val
.length
) return this.error('Found a non-hash value on field '+field
);
102 if (! field_val
['field']) field_val
['field'] = field
;
103 fields
[fields
.length
] = field_val
;
106 /// now lets do the validation
108 var errors
= new Array();
111 for (var j
= 0; j
< fields
.length
; j
++) {
113 if (typeof(ref
) != 'object' && ref
== 'OR') {
119 if (! ref
['field']) return this.error("Missing field key during normal validation");
120 var err
= this.validate_buddy(form
, ref
['field'], ref
);
122 /// test the error - if errors occur allow for OR - if OR fails use errors from first fail
124 if (j
<= fields
.length
&& typeof(fields
[j
+ 1] != 'object') && fields
[j
+ 1] == 'OR') {
127 if (hold_error
) err
= hold_error
;
128 for (var k
= 0; k
< err
.length
; k
++) errors
[errors
.length
] = err
[k
];
136 /// add on errors as requested
138 if (title
) ERRORS
[ERRORS
.length
] = title
;
139 for (var j
= 0; j
< errors
.length
; j
++) ERRORS
[ERRORS
.length
] = errors
[j
];
142 /// add on general options, and group options if errors in group occurred
144 for (var j
= 0; j
< order
.length
; j
++) {
145 var field
= order
[j
];
146 if (! (m
= field
.match('^(general|group)\\s+(\\w+)$'))) continue;
147 if (m
[1] == 'group' && (errors
.length
== 0 || m
[2].match('^(field|order|title)$'))) continue;
148 EXTRA
[m
[2]] = group_val
[field
];
152 /// store any extra items from self
153 for (var key
in this) {
154 if (! key
.match('_error$')
155 && ! key
.match('^(raise_error|as_hash_\\w+|as_array_\\w+|as_string_\\w+)$')) continue;
156 EXTRA
[key
] = this[key
];
159 /// allow for checking for unused keys
160 // if (EXTRA['no_extra_fields'])
161 // won't do anything about this for now - let the server handle it
163 /// return what they want
164 if (ERRORS
.length
) return new ValidateError(ERRORS
, EXTRA
);
169 /// allow for optional validation on groups and on individual items
170 function vob_check_conditional (form
, ifs
, N_level
, ifs_match
) {
172 if (! N_level
) N_level
= 0;
175 /// can pass a single hash - or an array ref of hashes
177 return this.error("Need reference passed to check_conditional");
178 } else if (typeof(ifs
) != 'object') {
179 ifs
= new Array(ifs
);
180 } else if (! ifs
.length
) { // turn hash into array of hash
181 ifs
= new Array(ifs
);
184 /// run the if options here
185 /// multiple items can be passed - all are required unless OR is used to separate
188 for (var i
= 0; i
< ifs
.length
; i
++) {
190 if (typeof(ref
) != 'object') {
198 if (m
= field
.match('^(\\s*!\\s*)')) {
199 field
= field
.substring(m
[1].length
);
200 ref
['max_in_set'] = '0 of ' + field
;
204 ref
['field'] = field
;
207 if (! is_found
) break;
209 /// get the field - allow for custom variables based upon a match
210 var field
= ref
['field'];
211 if (! field
) return this.error("Missing field key during validate_if");
212 field
= field
.replace(new RegExp('\\$(\\d+)','g'), function (all
, N
) {
213 if (typeof(ifs_match
) != 'object'
214 || typeof(ifs_match
[N
]) == 'undefined') return ''
218 var err
= this.validate_buddy(form
, field
, ref
, N_level
);
219 if (err
.length
) is_found
= 0;
224 function vob_filter_types (type
, types
) {
225 var values
= new Array();
226 var regexp
= new RegExp('^'+type
+'_?\\d*$');
227 for (var i
= 0; i
< types
.length
; i
++)
228 if (types
[i
].match(regexp
)) values
[values
.length
] = types
[i
];
232 function vob_add_error (errors
,field
,type
,field_val
,ifs_match
) {
233 errors
[errors
.length
] = new Array(field
, type
, field_val
, ifs_match
);
236 /// this is where the main checking goes on
237 function vob_validate_buddy (form
, field
, field_val
, N_level
, ifs_match
) {
238 if (! N_level
) N_level
= 0;
239 if (++ N_level
> 10) return this.error("Max dependency level reached " + N_level
);
240 if (! form
.elements
) return;
242 var errors
= new Array();
243 var types
= new Array();
244 for (var key
in field_val
) types
[types
.length
] = key
;
245 types
= types
.sort();
247 /// allow for not running some tests in the cgi
248 if (this.filter_types('exclude_js', types
).length
) return errors
;
250 /// allow for field names that contain regular expressions
252 if (m
= field
.match('^(!\\s*|)m([^\\s\\w])(.*)\\2([eigsmx]*)$')) {
256 if (opt
.indexOf('e') != -1) return this.error("The e option cannot be used on field "+field
);
257 opt
= opt
.replace(new RegExp('[sg]','g'),'');
258 var reg
= new RegExp(pat
, opt
);
260 var keys
= new Array();
261 for (var i
= 0; i
< form
.elements
.length
; i
++) {
262 var _field
= form
.elements
[i
].name
;
263 if (! _field
) continue;
264 if ( (not
&& ! (m
= _field
.match(reg
))) || (m
= _field
.match(reg
))) {
265 var err
= this.validate_buddy(form
, _field
, field_val
, N_level
, m
);
266 for (var j
= 0; j
< err
.length
; j
++) errors
[errors
.length
] = err
[j
];
272 var _value
= this.get_form_value(form
[field
]);
274 if (typeof(_value
) == 'object') {
277 values
= new Array();
278 values
[values
.length
] = _value
;
280 var n_values
= (typeof(_value
) == 'undefined') ? 0 : values
.length
;
282 /// allow for default value
283 var tests
= this.filter_types('default', types
);
284 if (n_values
== 0 || (n_values
== 1 && values
[0].length
== 0)) {
285 for (var i
= 0; i
< tests
.length
; i
++) {
286 var el
= form
[field
];
288 if (type
&& (type
== 'hidden' || type
== 'password' || type
== 'text' || type
== 'textarea' || type
== 'submit')) el
.value
= values
[0] = field_val
[tests
[i
]];
292 /// allow for a few form modifiers
294 for (var i
= 0; i
< values
.length
; i
++) {
295 if (typeof(values
[i
]) == 'undefined') continue;
296 if (! this.filter_types('do_not_trim',types
).length
)
297 values
[i
] = values
[i
].replace('^\\s+','').replace(new RegExp('\\s+$',''),'');
298 if (this.filter_types('to_upper_case',types
).length
) {
299 values
[i
] = values
[i
].toUpperCase();
300 } else if (this.filter_types('to_lower_case',types
).length
) {
301 values
[i
] = values
[i
].toLowerCase();
304 var tests
= this.filter_types('replace', types
);
305 for (var i
= 0; i
< tests
.length
; i
++) {
306 var ref
= field_val
[tests
[i
]];
307 ref
= (typeof(ref
) == 'object') ? ref : ref
.split(new RegExp('\\s*\\|\\|\\s*'));
308 for (var j
= 0; j
< ref
.length
; j
++) {
309 if (! (m
= ref
[j
].match('^\\s*s([^\\s\\w])(.+)\\1(.*)\\1([eigmx]*)$')))
310 return this.error("Not sure how to parse that replace "+ref
[j
]);
314 if (opt
.indexOf('e') != -1)
315 return this.error("The e option cannot be used on field "+field
+", replace "+tests
[i
]);
316 var regexp
= new RegExp(pat
, opt
);
317 for (var k
= 0; k
< values
.length
; k
++) {
318 if (values
[k
].match(regexp
)) modified
= 1;
319 values
[k
] = values
[k
].replace(regexp
,swap
);
323 if (modified
&& n_values
== 1) {
324 var el
= form
[field
];
326 if (! type
) return '';
327 if (type
== 'hidden' || type
== 'password' || type
== 'text' || type
== 'textarea' || type
== 'submit')
328 el
.value
= values
[0];
331 /// only continue if a validate_if is not present or passes test
334 var tests
= this.filter_types('validate_if', types
);
335 for (var i
= 0; i
< tests
.length
; i
++) {
337 var ifs
= field_val
[tests
[i
]];
338 var ret
= this.check_conditional(form
, ifs
, N_level
, ifs_match
);
339 if (ret
) needs_val
++;
341 if (! needs_val
&& n_vif
) return errors
;
344 /// check for simple existence
345 /// optionally check only if another condition is met
346 var is_required
= '';
347 var tests
= this.filter_types('required', types
);
348 for (var i
= 0; i
< tests
.length
; i
++) {
349 if (! field_val
[tests
[i
]] || field_val
[tests
[i
]] == 0) continue;
350 is_required
= tests
[i
];
354 var tests
= this.filter_types('required_if', types
);
355 for (var i
= 0; i
< tests
.length
; i
++) {
356 var ifs
= field_val
[tests
[i
]];
357 if (! this.check_conditional(form
, ifs
, N_level
, ifs_match
)) continue;
358 is_required
= tests
[i
];
362 if (is_required
&& (typeof(_value
) == 'undefined'
363 || ((typeof(_value
) == 'object' && _value
.length
== 0)
364 || ! _value
.length
))) {
365 this.add_error(errors
, field
, is_required
, field_val
, ifs_match
);
370 var tests
= this.filter_types('min_values', types
);
371 for (var i
= 0; i
< tests
.length
; i
++) {
372 var n
= field_val
[tests
[i
]];
374 this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
380 var tests
= this.filter_types('max_values', types
);
381 if (! tests
.length
) {
382 tests
[tests
.length
] = 'max_values';
383 field_val
['max_values'] = 1;
385 for (var i
= 0; i
< tests
.length
; i
++) {
386 var n
= field_val
[tests
[i
]];
388 this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
393 /// min_in_set and max_in_set check
394 for (var h
= 0; h
< 2 ; h
++) {
395 var minmax
= (h
== 0) ? 'min' : 'max';
396 var tests
= this.filter_types(minmax
+'_in_set', types
);
397 for (var i
= 0; i
< tests
.length
; i
++) {
398 if (! (m
= field_val
[tests
[i
]].match('^\\s*(\\d+)(?:\\s*[oO][fF])?\\s+(.+)\\s*$')))
399 return this.error("Invalid in_set check "+field_val
[tests
[i
]]);
401 var _fields
= m
[2].split(new RegExp('[\\s,]+'));
402 for (var k
= 0; k
< _fields
.length
; k
++) {
403 var _value
= this.get_form_value(form
[_fields
[k
]]);
405 if (typeof(_value
) == 'undefined') continue;
406 if (typeof(_value
) == 'object') {
409 _values
= new Array();
410 _values
[_values
.length
] = _value
;
412 for (var l
= 0; l
< _values
.length
; l
++) {
413 var _value
= _values
[l
];
414 if (typeof(_value
) != 'undefined' && _value
.length
) n
--;
417 if ( (minmax
== 'min' && n
> 0)
418 || (minmax
== 'max' && n
< 0)) {
419 this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
425 // the remaining tests operate on each value of a field
426 for (var n
= 0; n
< values
.length
; n
++) {
427 var value
= values
[n
];
429 /// allow for enum types
430 var tests
= this.filter_types('enum', types
);
431 for (var i
= 0; i
< tests
.length
; i
++) {
432 var hold
= field_val
[tests
[i
]];
433 var _enum
= (typeof(hold
) == 'object') ? hold : hold
.split(new RegExp('\\s*\\|\\|\\s*'));
435 for (var j
= 0; j
< _enum
.length
; j
++) {
436 if (value
!= _enum
[j
]) continue;
440 if (! is_found
) this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
443 /// field equality test
444 var tests
= this.filter_types('equals', types
);
445 for (var i
= 0; i
< tests
.length
; i
++) {
446 var field2
= field_val
[tests
[i
]];
447 var not
= field2
.match('^!\\s*');
448 if (not
) field2
= field2
.substring(not
[0].length
);
450 if (m
= field2
.match('^(["\'])(.*)\\1$')) {
451 if (value
== m
[2]) success
= 1;
453 var value2
= this.get_form_value(form
[field2
]);
454 if (typeof(value2
) == 'undefined') value2
= '';
455 if (value
== value2
) success
= 1;
457 if (not
&& success
|| ! not
&& ! success
)
458 this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
462 var tests
= this.filter_types('min_len', types
);
463 for (var i
= 0; i
< tests
.length
; i
++) {
464 var n
= field_val
[tests
[i
]];
465 if (value
.length
< n
) this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
469 var tests
= this.filter_types('max_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
);
475 /// now do match types
476 var tests
= this.filter_types('match', types
);
477 for (var i
= 0; i
< tests
.length
; i
++) {
478 var ref
= field_val
[tests
[i
]];
479 ref
= (typeof(ref
) == 'object') ? ref
480 : (typeof(ref
) == 'function') ? new Array(ref
)
481 : ref
.split(new RegExp('\\s*\\|\\|\\s*'));
482 for (var j
= 0; j
< ref
.length
; j
++) {
483 if (typeof(ref
[j
]) == 'function') {
484 if (! value
.match(ref
[j
])) this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
486 if (! (m
= ref
[j
].match('^\\s*(!\\s*|)m([^\\s\\w])(.*)\\2([eigsmx]*)\\s*$')))
487 return this.error("Not sure how to parse that match ("+ref
[j
]+")");
491 if (opt
.indexOf('e') != -1)
492 return this.error("The e option cannot be used on field "+field
+", test "+tests
[i
]);
493 opt
= opt
.replace(new RegExp('[sg]','g'),'');
494 var regexp
= new RegExp(pat
, opt
);
495 if ( ( not
&& value
.match(regexp
))
496 || (! not
&& ! value
.match(regexp
))) {
497 this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
503 /// allow for comparison checks
504 var tests
= this.filter_types('compare', types
);
505 for (var i
= 0; i
< tests
.length
; i
++) {
506 var ref
= field_val
[tests
[i
]];
507 ref
= (typeof(ref
) == 'object') ? ref : ref
.split(new RegExp('\\s*\\|\\|\\s*'));
508 for (var j
= 0; j
< ref
.length
; j
++) {
510 if (! comp
) continue;
513 if (m
= comp
.match('^\\s*(>|<|[><!=]=)\\s*([\\d\.\-]+)\\s*$')) {
514 if (! copy
) copy
= 0;
516 if (m
[1] == '>' ) hold
= (copy
> m
[2])
517 else if (m
[1] == '<' ) hold
= (copy
< m
[2])
518 else if (m
[1] == '>=') hold
= (copy
>= m
[2])
519 else if (m
[1] == '<=') hold
= (copy
<= m
[2])
520 else if (m
[1] == '!=') hold
= (copy
!= m
[2])
521 else if (m
[1] == '==') hold
= (copy
== m
[2])
522 } else if (m
= comp
.match('^\\s*(eq|ne|gt|ge|lt|le)\\s+(.+?)\\s*$')) {
523 m
[2] = m
[2].replace('^(["\'])(.*)\\1$','$1');
524 if (m
[1] == 'gt') hold
= (copy
> m
[2])
525 else if (m
[1] == 'lt') hold
= (copy
< m
[2])
526 else if (m
[1] == 'ge') hold
= (copy
>= m
[2])
527 else if (m
[1] == 'le') hold
= (copy
<= m
[2])
528 else if (m
[1] == 'ne') hold
= (copy
!= m
[2])
529 else if (m
[1] == 'eq') hold
= (copy
== m
[2])
531 return this.error("Not sure how to compare \""+comp
+"\"");
533 if (! hold
) this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
537 /// do specific type checks
538 var tests
= this.filter_types('type',types
);
539 for (var i
= 0; i
< tests
.length
; i
++)
540 if (! this.check_type(value
, field_val
[tests
[i
]], field
, form
))
541 this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
543 /// do custom_js type checks
544 // this will allow for a custom piece of javascript
545 // the js is evaluated and should return 1 for success
546 // or 0 for failure - the variables field, value, and field_val (the hash) are available
547 var tests
= this.filter_types('custom_js',types
);
548 for (var i
= 0; i
< tests
.length
; i
++)
549 if (! eval(field_val
[tests
[i
]]))
550 this.add_error(errors
, field
, tests
[i
], field_val
, ifs_match
);
553 /// all done - time to return
557 /// used to validate specific types
558 function vob_check_type (value
, type
, field
, form
) {
561 /// do valid email address for our system
562 if (type
== 'EMAIL') {
563 if (! value
) return 0;
564 if (! (m
= value
.match('^(.+)\@(.+?)$'))) return 0;
565 if (m
[1].length
> 60) return 0;
566 if (m
[2].length
> 100) return 0;
567 if (! this.check_type(m
[2],'DOMAIN') && ! this.check_type(m
[2],'IP')) return 0;
568 if (! this.check_type(m
[1],'LOCAL_PART')) return 0;
570 /// the "username" portion of an email address
571 } else if (type
== 'LOCAL_PART') {
572 if (typeof(value
) == 'undefined' || ! value
.length
) return 0;
573 if (! value
.match('[^a-z0-9.\\-!&]')) return 0;
574 if (! value
.match('^[.\\-]')) return 0;
575 if (! value
.match('[.\\-&]$')) return 0;
576 if (! value
.match('(\\.-|-\\.|\\.\\.)')) return 0;
578 /// standard IP address
579 } else if (type
== 'IP') {
580 if (! value
) return 0;
581 var dig
= value
.split(new RegExp('\\.'));
582 if (dig
.length
!= 4) return 0;
583 for (var i
= 0; i
< 4; i
++)
584 if (typeof(dig
[i
]) == 'undefined' || dig
[i
].match('\\D') || dig
[i
] > 255) return 0;
586 /// domain name - including tld and subdomains (which are all domains)
587 } else if (type
== 'DOMAIN') {
588 if (! value
) return 0;
589 if (! value
.match('^[a-z0-9.-]{4,255}$')) return 0;
590 if (value
.match('^[.\\-]')) return 0;
591 if (value
.match('(\\.-|-\\.|\\.\\.)')) return 0;
592 if (! (m
= value
.match('\.([a-z]+)$'))) return 0;
593 value
= value
.substring(0,value
.lastIndexOf('.'));
595 if (m
[1] == 'name') {
596 if (! value
.match('^[a-z0-9][a-z0-9\\-]{0,62}\\.[a-z0-9][a-z0-9\\-]{0,62}$')) return 0;
598 if (! value
.match('^([a-z0-9][a-z0-9\\-]{0,62}\\.)*[a-z0-9][a-z0-9\\-]{0,62}$')) return 0;
601 } else if (type
== 'URL') {
602 if (! value
) return 0;
603 if (! (m
= value
.match(new RegExp('^https?://([^/]+)','i'),''))) return 0;
604 value
= value
.substring(m
[0].length
);
605 if (! this.check_type(m
[1],'DOMAIN') && ! this.check_type(m
[1],'IP')) return 0;
606 if (value
&& ! this.check_type(value
,'URI')) return 0;
608 /// validate a uri - the path portion of a request
609 } else if (type
== 'URI') {
610 if (! value
) return 0;
611 if (value
.match('\\s')) return 0;
613 } else if (type
== 'CC') {
614 if (! value
) return 0;
615 if (value
.match('[^\\d\\- ]') || value
.length
> 16 || value
.length
< 13) return;
616 /// simple mod10 check
617 value
= value
.replace(new RegExp('[\\- ]','g'), '');
621 for (var i
= value
.length
- 1; i
>= 0; i
--) {
622 if (++ swc
> 2) swc
= 1;
623 var y
= value
.charAt(i
) * swc
;
627 if (sum
% 10) return 0;
634 // little routine that will get the values from the form
635 // it will return multiple values as an array
636 function vob_get_form_value (el
) {
638 if (el
.disabled
) return '';
639 var type
= el
.type
? el
.type
.toLowerCase() : '';
640 if (el
.length
&& type
!= 'select-one') {
642 for (var j
=0;j
<el
.length
;j
++) {
643 if (type
.indexOf('multiple') != -1) {
644 if (el
[j
].selected
) a
[a
.length
] = el
[j
].value
;
646 if (el
[j
].checked
) a
[a
.length
] = vob_get_form_value(el
[j
]);
649 if (a
.length
== 0) return '';
650 if (a
.length
== 1) return a
[0];
653 if (! type
) return '';
654 if (type
== 'hidden' || type
== 'password' || type
== 'text' || type
== 'textarea' || type
== 'submit')
656 if (type
.indexOf('select') != -1) {
657 if (! el
.length
) return '';
658 return el
[el
.selectedIndex
].value
;
660 if (type
== 'checkbox' || type
== 'radio') {
661 return el
.checked
? el
.value : '';
663 if (type
== 'file') {
664 return el
.value
; // hope this works
666 alert('Unknown form type for '+el
.name
+': '+type
);
670 ///----------------------------------------------------------------///
672 function eob_get_val (key
, extra2
, extra1
, _default
) {
673 if (typeof(extra2
[key
]) != 'undefined') return extra2
[key
];
674 if (typeof(extra1
[key
]) != 'undefined') return extra1
[key
];
678 function eob_as_string (extra2
) {
679 var extra1
= this.extra
;
680 if (! extra2
) extra2
= new Array();
682 var joiner
= eob_get_val('as_string_join', extra2
, extra1
, '\n');
683 var header
= eob_get_val('as_string_header', extra2
, extra1
, '');
684 var footer
= eob_get_val('as_string_footer', extra2
, extra1
, '');
686 return header
+ this.as_array(extra2
).join(joiner
) + footer
;
689 /// return an array of applicable errors
690 function eob_as_array (extra2
) {
691 var errors
= this.errors
;
692 var extra1
= this.extra
;
693 if (! extra2
) extra2
= new Array();
695 var title
= eob_get_val('as_array_title', extra2
, extra1
, 'Please correct the following items:');
697 /// if there are heading items then we may end up needing a prefix
699 if (title
) has_headings
= 1;
701 for (var i
= 0; i
< errors
.length
; i
++) {
702 if (typeof(errors
[i
]) != 'string') continue;
708 var prefix
= eob_get_val('as_array_prefix', extra2
, extra1
, has_headings
? ' ' : '');
710 /// get the array ready
711 var arr
= new Array();
712 if (title
&& title
.length
) arr
[arr
.length
] = title
;
714 var found
= new Array();
715 for (var i
= 0; i
< errors
.length
; i
++) {
716 if (typeof(errors
[i
]) == 'string') {
717 arr
[arr
.length
] = errors
[i
];
720 var text
= this.get_error_text(errors
[i
]);
721 if (found
[text
]) continue;
723 arr
[arr
.length
] = prefix
+ text
;
730 /// return a hash of applicable errors
731 function eob_as_hash (extra2
) {
732 var errors
= this.errors
;
733 var extra1
= this.extra
;
734 if (! extra2
) extra2
= new Array();
735 var suffix
= eob_get_val('as_hash_suffix', extra2
, extra1
, '_error');
736 var joiner
= eob_get_val('as_hash_join', extra2
, extra1
, '<br />');
738 /// now add to the hash
739 var found
= new Array();
740 var ret
= new Array();
741 for (var i
= 0; i
< errors
.length
; i
++) {
742 if (typeof(errors
[i
]) == 'string') continue;
743 if (! errors
[i
].length
) continue;
745 var field
= errors
[i
][0];
746 var type
= errors
[i
][1];
747 var field_val
= errors
[i
][2];
748 var ifs_match
= errors
[i
][3];
750 if (! field
) return alert("Missing field name");
751 if (field_val
['delegate_error']) {
752 field
= field_val
['delegate_error'];
753 field
= field
.replace(new RegExp('\\$(\\d+)','g'), function (all
, N
) {
754 if (typeof(ifs_match
) != 'object'
755 || typeof(ifs_match
[N
]) == 'undefined') return ''
760 var text
= this.get_error_text(errors
[i
]);
761 if (! found
[field
]) found
[field
] = new Array();
762 if (found
[field
][text
]) continue;
763 found
[field
][text
] = 1;
766 if (! ret
[field
]) ret
[field
] = new Array();
767 ret
[field
].push(text
);
770 /// allow for elements returned as
772 var header
= eob_get_val('as_hash_header', extra2
, extra1
, '');
773 var footer
= eob_get_val('as_hash_footer', extra2
, extra1
, '');
774 for (var key
in ret
) ret
[key
] = header
+ ret
[key
].join(joiner
) + footer
;
780 /// return a user friendly error message
781 function eob_get_error_text (err
) {
782 var extra
= this.extra
;
785 var field_val
= err
[2];
786 var ifs_match
= err
[3];
789 var dig
= (m
= type
.match('(_?\\d+)$')) ? m
[1] : '';
790 var type_lc
= type
.toLowerCase();
792 /// allow for delegated field names - only used for defaults
793 if (field_val
['delegate_error']) {
794 field
= field_val
['delegate_error'];
795 field
= field
.replace(new RegExp('\\$(\\d+)','g'), function (all
, N
) {
796 if (typeof(ifs_match
) != 'object'
797 || typeof(ifs_match
[N
]) == 'undefined') return ''
802 /// the the name of this thing
803 var name
= (field_val
['name']) ? field_val
['name'] : "The field " +field
;
804 name
= name
.replace(new RegExp('\\$(\\d+)','g'), function (all
, N
) {
805 if (typeof(ifs_match
) != 'object'
806 || typeof(ifs_match
[N
]) == 'undefined') return ''
811 /// type can look like "required" or "required2" or "required100023"
812 /// allow for fallback from required100023_error through required_error
813 var possible_keys
= new Array(type
+ '_error');
814 if (dig
.length
) possible_keys
.unshift(type
+ dig
+ '_error');
816 /// look in the passed hash or self first
817 for (var i
= 0; i
< possible_keys
.length
; i
++) {
818 var key
= possible_keys
[i
];
819 var ret
= field_val
[key
];
821 if (extra
[key
]) ret
= extra
[key
];
824 ret
= ret
.replace(new RegExp('\\$(\\d+)','g'), function (all
, N
) {
825 if (typeof(ifs_match
) != 'object'
826 || typeof(ifs_match
[N
]) == 'undefined') return ''
829 ret
= ret
.replace(new RegExp('\\$field','g'), field
);
830 ret
= ret
.replace(new RegExp('\\$name' ,'g'), name
);
831 if (field_val
[type
+ dig
] && typeof(field_val
[type
+ dig
]) == 'string')
832 ret
= ret
.replace(new RegExp('\\$value' ,'g'), field_val
[type
+ dig
]);
836 /// set default messages
837 if (type
== 'required' || type
== 'required_if') {
838 return name
+ " is required.";
840 } else if (type
== 'min_values') {
841 var n
= field_val
["min_values" + dig
];
842 var values
= (n
== 1) ? 'value' : 'values';
843 return name
+ " had less than "+n
+" "+values
+".";
845 } else if (type
== 'max_values') {
846 var n
= field_val
["max_values" + dig
];
847 var values
= (n
== 1) ? 'value' : 'values';
848 return name
+ " had more than "+n
+" "+values
+".";
850 } else if (type
== 'min_in_set') {
851 var set = field_val
["min_in_set" + dig
];
852 return "Not enough fields were chosen from the set ("+set+")";
853 return "Too many fields were chosen from the set ("+set+")";
855 } else if (type
== 'max_in_set') {
856 var set = field_val
["max_in_set" + dig
];
857 return "Too many fields were chosen from the set ("+set+")";
859 } else if (type
== 'enum') {
860 return name
+ " is not in the given list.";
862 } else if (type
== 'equals') {
863 var field2
= field_val
["equals" + dig
];
864 var name2
= field_val
["equals" +dig
+ "_name"];
865 if (! name2
) name2
= "the field " +field2
;
866 name2
= name2
.replace(new RegExp('\\$(\\d+)','g'), function (all
, N
) {
867 if (typeof(ifs_match
) != 'object'
868 || typeof(ifs_match
[N
]) == 'undefined') return ''
871 return name
+ " did not equal " + name2
+".";
873 } else if (type
== 'min_len') {
874 var n
= field_val
["min_len"+dig
];
875 var chars
= (n
== 1) ? 'character' : 'characters';
876 return name
+ " was less than "+n
+" "+chars
+".";
878 } else if (type
== 'max_len') {
879 var n
= field_val
["max_len"+dig
];
880 var chars
= (n
== 1) ? 'character' : 'characters';
881 return name
+ " was more than "+n
+" "+chars
+".";
883 } else if (type
== 'match') {
884 return name
+ " contains invalid characters.";
886 } else if (type
== 'compare') {
887 return name
+ " did not fit comparison.";
889 } else if (type
== 'type') {
890 var _type
= field_val
["type"+dig
];
891 return name
+ " did not match type "+_type
+".";
893 } else if (type
== 'custom_js') {
894 return name
+ " did not match custom_js"+dig
+" check.";
898 return alert("Missing error on field "+field
+" for type "+type
+dig
);
901 function eob_first_field () {
902 for (var i
= 0; i
< this.errors
.length
; i
++) {
903 if (typeof(this.errors
[i
]) != 'object') continue;
904 if (! this.errors
[i
][0]) continue;
905 return this.errors
[i
][0];
910 ///----------------------------------------------------------------///
912 document
.validate = function (form
, val_hash
) {
913 // undo previous inline
914 if (document
.did_inline
) {
915 for (var key
in document
.did_inline
) {
916 var el
= document
.getElementById(key
);
917 if (el
) el
.innerHTML
= '';
919 document
.did_inline
= undefined;
923 val_hash
= document
.load_val_hash(form
, val_hash
);
924 if (typeof(val_hash
) == 'undefined') return true;
925 if (! document
.val_obj
) document
.val_obj
= new Validate();
926 var err_obj
= document
.val_obj
.validate(form
, val_hash
);
929 if (! err_obj
) return true;
932 var field
= err_obj
.first_field();
933 if (field
&& form
[field
] && form
[field
].focus
) form
[field
].focus();
936 if (! err_obj
.extra
.no_inline
) {
937 var d
= document
.did_inline
= new Array();
938 var hash
= err_obj
.as_hash();
939 for (var key
in hash
) {
940 var el
= document
.getElementById(key
);
941 if (el
) el
.innerHTML
= hash
[key
];
947 if (! err_obj
.extra
.no_confirm
) {
948 return confirm(err_obj
.as_string()) ? false : true;
949 } else if (! err_obj
.extra
.no_alert
) {
950 alert(err_obj
.as_string());
952 } else if (! err_obj
.extra
.no_inline
) {
959 document
.load_val_hash = function (form
, val_hash
) {
960 // check the form we are using
961 if (! form
) return alert('Missing form or form name');
962 if (typeof(form
) == 'string') {
963 if (! document
[form
]) return alert('No form by name '+form
);
964 form
= document
[form
];
967 // if we already have validation - use it
968 if (form
.val_hash
) return form
.val_hash
;
970 // load in the validation and save it for future use
971 if (typeof(val_hash
) != 'object') {
972 // get the hash from a javascript function
973 if (typeof(val_hash
) == 'function') {
974 val_hash
= val_hash(formname
);
975 } else if (typeof(val_hash
) == 'undefined') {
977 // get hash from a global js variable
978 if (typeof(document
.validation
) != 'undefined') {
979 val_hash
= document
.validation
;
980 // get hash from a element by if of validation
981 } else if (el
= document
.getElementById('validation')) {
982 val_hash
= el
.innerHTML
;
983 val_hash
= val_hash
.replace(new RegExp('<', 'ig'),'<');
984 val_hash
= val_hash
.replace(new RegExp('>', 'ig'),'>');
985 val_hash
= val_hash
.replace(new RegExp('&','ig'),'&');
986 // read hash from <input name=foo validation="">
988 var order
= new Array();
990 var yaml
= form
.getAttribute('validation');
992 if (m
= yaml
.match('^( +)')) yaml
= yaml
.replace(new RegExp('^'+m
[1], 'g'), ''); //unindent
993 yaml
= yaml
.replace(new RegExp('\\s*$',''),'\n'); // add trailing
997 for (var i
= 0; i
< form
.elements
.length
; i
++) {
998 var name
= form
.elements
[i
].name
;
999 var yaml
= form
.elements
[i
].getAttribute('validation');
1000 if (! name
|| ! yaml
) continue;
1001 yaml
= yaml
.replace(new RegExp('\\s*$',''),'\n'); // add trailing
1002 yaml
= yaml
.replace(new RegExp('^(.)','mg'),' $1'); // indent all
1003 yaml
= yaml
.replace(new RegExp('^( *[^\\s&*\\[\\{])',''),'\n$1'); // add newline
1004 str
+= name
+':' + yaml
;
1005 order
[order
.length
] = name
;
1007 if (str
) val_hash
= str
+ "group order: [" + order
.join(', ') + "]\n";
1010 if (typeof(val_hash
) == 'string') {
1011 if (! document
.yaml_load
) return;
1012 document
.hide_yaml_errors
= (! document
.show_yaml_errors
);
1013 if (location
.search
&& location
.search
.indexOf('show_yaml_errors') != -1)
1014 document
.hide_yaml_errors
= 0;
1015 val_hash
= document
.yaml_load(val_hash
);
1016 if (document
.yaml_error_occured
) return;
1020 // attach to the form
1021 form
.val_hash
= val_hash
;
1022 return form
.val_hash
;
1026 document
.check_form = function (form
, val_hash
) {
1027 // check the form we are using
1028 if (! form
) return alert('Missing form or form name');
1029 if (typeof(form
) == 'string') {
1030 if (! document
[form
]) return alert('No form by name '+form
);
1031 form
= document
[form
];
1034 // void call - allow for getting it at run time rather than later
1035 document
.load_val_hash(form
, val_hash
);
1038 form
.onsubmit = function () {return document
.validate(this)};
This page took 0.124465 seconds and 4 git commands to generate.