X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=lib%2FCGI%2FEx%2Fvalidate.js;h=40b4c04d6d7f92d615df5c69fe65c9e5e7c9849e;hb=a9adfe5a3786f860510fcf10792958a25e1a7727;hp=8d4c296aee4d37ab42bc9221799dd53121164813;hpb=85070b46d0a93ddbeef07341421adb8389a55418;p=chaz%2Fp5-CGI-Ex diff --git a/lib/CGI/Ex/validate.js b/lib/CGI/Ex/validate.js index 8d4c296..40b4c04 100644 --- a/lib/CGI/Ex/validate.js +++ b/lib/CGI/Ex/validate.js @@ -1,10 +1,10 @@ /**----------------------------------------------------------------*** -* Copyright 2004 - Paul Seamons * +* Copyright 2007 - Paul Seamons * * Distributed under the Perl Artistic License without warranty * * Based upon CGI/Ex/Validate.pm v1.14 from Perl * * For instructions on usage, see perldoc of CGI::Ex::Validate * ***----------------------------------------------------------------**/ -// $Revision: 1.31 $ +// $Revision: 1.42 $ function Validate () { this.error = vob_error; @@ -60,7 +60,10 @@ function vob_validate (form, val_hash) { /// look for a group order and then fail back to the keys of the group var fields = group_val['group fields']; var order = new Array(); - for (var key in group_val) order[order.length] = key; + for (var key in group_val) { + if (key == 'extend') continue; // Protoype Array() fix + order[order.length] = key; + } order = order.sort(); if (fields) { if (typeof(fields) != 'object' || ! fields.length) @@ -151,6 +154,7 @@ function vob_validate (form, val_hash) { /// store any extra items from self for (var key in this) { + if (key == 'extend') continue; // Protoype Array() fix if (! key.match('_error$') && ! key.match('^(raise_error|as_hash_\\w+|as_array_\\w+|as_string_\\w+)$')) continue; EXTRA[key] = this[key]; @@ -229,8 +233,16 @@ function vob_filter_types (type, types) { return values; } -function vob_add_error (errors,field,type,field_val,ifs_match) { +function vob_add_error (errors,field,type,field_val,ifs_match,form) { errors[errors.length] = new Array(field, type, field_val, ifs_match); + if (field_val['clear_on_error']) { + var el = form[field]; + if (el) { + var type = el.type; + if (type && (type == 'hidden' || type == 'password' || type == 'text' || type == 'textarea' || type == 'submit')) + el.value = ''; + } + } } /// this is where the main checking goes on @@ -241,7 +253,10 @@ function vob_validate_buddy (form, field, field_val, N_level, ifs_match) { var errors = new Array(); var types = new Array(); - for (var key in field_val) types[types.length] = key; + for (var key in field_val) { + if (key == 'extend') continue; // Protoype Array() fix + types[types.length] = key; + } types = types.sort(); /// allow for not running some tests in the cgi @@ -284,8 +299,10 @@ function vob_validate_buddy (form, field, field_val, N_level, ifs_match) { if (n_values == 0 || (n_values == 1 && values[0].length == 0)) { for (var i = 0; i < tests.length; i ++) { var el = form[field]; + if (! el) continue; var type = el.type; - if (type && (type == 'hidden' || type == 'password' || type == 'text' || type == 'textarea' || type == 'submit')) el.value = values[0] = field_val[tests[i]]; + if (type && (type == 'hidden' || type == 'password' || type == 'text' || type == 'textarea' || type == 'submit')) + el.value = values[0] = '' + field_val[tests[i]]; } } @@ -295,6 +312,8 @@ function vob_validate_buddy (form, field, field_val, N_level, ifs_match) { if (typeof(values[i]) == 'undefined') continue; if (! this.filter_types('do_not_trim',types).length) values[i] = values[i].replace('^\\s+','').replace(new RegExp('\\s+$',''),''); + if (this.filter_types('trim_control_chars',types).length) + values[i] = values[i].replace(new RegExp('\t', 'g'),' ').replace(new RegExp('[\\x00-\\x1F]+','g'),''); if (this.filter_types('to_upper_case',types).length) { values[i] = values[i].toUpperCase(); } else if (this.filter_types('to_lower_case',types).length) { @@ -362,7 +381,7 @@ function vob_validate_buddy (form, field, field_val, N_level, ifs_match) { if (is_required && (typeof(_value) == 'undefined' || ((typeof(_value) == 'object' && _value.length == 0) || ! _value.length))) { - this.add_error(errors, field, is_required, field_val, ifs_match); + this.add_error(errors, field, is_required, field_val, ifs_match, form); return errors; } @@ -371,7 +390,7 @@ function vob_validate_buddy (form, field, field_val, N_level, ifs_match) { for (var i = 0; i < tests.length; i ++) { var n = field_val[tests[i]]; if (n_values < n) { - this.add_error(errors, field, tests[i], field_val, ifs_match); + this.add_error(errors, field, tests[i], field_val, ifs_match, form); return errors; } } @@ -385,7 +404,7 @@ function vob_validate_buddy (form, field, field_val, N_level, ifs_match) { for (var i = 0; i < tests.length; i ++) { var n = field_val[tests[i]]; if (n_values > n) { - this.add_error(errors, field, tests[i], field_val, ifs_match); + this.add_error(errors, field, tests[i], field_val, ifs_match, form); return errors; } } @@ -416,7 +435,7 @@ function vob_validate_buddy (form, field, field_val, N_level, ifs_match) { } if ( (minmax == 'min' && n > 0) || (minmax == 'max' && n < 0)) { - this.add_error(errors, field, tests[i], field_val, ifs_match); + this.add_error(errors, field, tests[i], field_val, ifs_match, form); return errors; } } @@ -437,7 +456,7 @@ function vob_validate_buddy (form, field, field_val, N_level, ifs_match) { is_found = 1; break; } - if (! is_found) this.add_error(errors, field, tests[i], field_val, ifs_match); + if (! is_found) this.add_error(errors, field, tests[i], field_val, ifs_match, form); } /// field equality test @@ -455,21 +474,21 @@ function vob_validate_buddy (form, field, field_val, N_level, ifs_match) { if (value == value2) success = 1; } if (not && success || ! not && ! success) - this.add_error(errors, field, tests[i], field_val, ifs_match); + this.add_error(errors, field, tests[i], field_val, ifs_match, form); } /// length min check var tests = this.filter_types('min_len', types); for (var i = 0; i < tests.length; i ++) { var n = field_val[tests[i]]; - if (value.length < n) this.add_error(errors, field, tests[i], field_val, ifs_match); + if (value.length < n) this.add_error(errors, field, tests[i], field_val, ifs_match, form); } /// length max check var tests = this.filter_types('max_len', types); for (var i = 0; i < tests.length; i ++) { var n = field_val[tests[i]]; - if (value.length > n) this.add_error(errors, field, tests[i], field_val, ifs_match); + if (value.length > n) this.add_error(errors, field, tests[i], field_val, ifs_match, form); } /// now do match types @@ -481,7 +500,7 @@ function vob_validate_buddy (form, field, field_val, N_level, ifs_match) { : ref.split(new RegExp('\\s*\\|\\|\\s*')); for (var j = 0; j < ref.length; j ++) { if (typeof(ref[j]) == 'function') { - if (! value.match(ref[j])) this.add_error(errors, field, tests[i], field_val, ifs_match); + if (! value.match(ref[j])) this.add_error(errors, field, tests[i], field_val, ifs_match, form); } else { if (! (m = ref[j].match('^\\s*(!\\s*|)m([^\\s\\w])(.*)\\2([eigsmx]*)\\s*$'))) return this.error("Not sure how to parse that match ("+ref[j]+")"); @@ -494,7 +513,7 @@ function vob_validate_buddy (form, field, field_val, N_level, ifs_match) { var regexp = new RegExp(pat, opt); if ( ( not && value.match(regexp)) || (! not && ! value.match(regexp))) { - this.add_error(errors, field, tests[i], field_val, ifs_match); + this.add_error(errors, field, tests[i], field_val, ifs_match, form); } } } @@ -530,7 +549,7 @@ function vob_validate_buddy (form, field, field_val, N_level, ifs_match) { } else { return this.error("Not sure how to compare \""+comp+"\""); } - if (! hold) this.add_error(errors, field, tests[i], field_val, ifs_match); + if (! hold) this.add_error(errors, field, tests[i], field_val, ifs_match, form); } } @@ -538,7 +557,7 @@ function vob_validate_buddy (form, field, field_val, N_level, ifs_match) { var tests = this.filter_types('type',types); for (var i = 0; i < tests.length; i ++) if (! this.check_type(value, field_val[tests[i]], field, form)) - this.add_error(errors, field, tests[i], field_val, ifs_match); + this.add_error(errors, field, tests[i], field_val, ifs_match, form); /// do custom_js type checks // this will allow for a custom piece of javascript @@ -547,7 +566,7 @@ function vob_validate_buddy (form, field, field_val, N_level, ifs_match) { var tests = this.filter_types('custom_js',types); for (var i = 0; i < tests.length; i ++) if (! eval(field_val[tests[i]])) - this.add_error(errors, field, tests[i], field_val, ifs_match); + this.add_error(errors, field, tests[i], field_val, ifs_match, form); } /// all done - time to return @@ -570,10 +589,10 @@ function vob_check_type (value, type, field, form) { /// the "username" portion of an email address } else if (type == 'LOCAL_PART') { if (typeof(value) == 'undefined' || ! value.length) return 0; - if (! value.match('[^a-z0-9.\\-!&]')) return 0; - if (! value.match('^[.\\-]')) return 0; - if (! value.match('[.\\-&]$')) return 0; - if (! value.match('(\\.-|-\\.|\\.\\.)')) return 0; + if (value.match('[^a-z0-9.\\-!&+]')) return 0; + if (value.match('^[.\\-]')) return 0; + if (value.match('[.\\-&]$')) return 0; + if (value.match('(\\.-|-\\.|\\.\\.)')) return 0; /// standard IP address } else if (type == 'IP') { @@ -771,7 +790,10 @@ function eob_as_hash (extra2) { if (joiner) { var header = eob_get_val('as_hash_header', extra2, extra1, ''); var footer = eob_get_val('as_hash_footer', extra2, extra1, ''); - for (var key in ret) ret[key] = header + ret[key].join(joiner) + footer; + for (var key in ret) { + if (key == 'extend') continue; // Protoype Array() fix + ret[key] = header + ret[key].join(joiner) + footer; + } } return ret; @@ -913,6 +935,7 @@ document.validate = function (form, val_hash) { // undo previous inline if (document.did_inline) { for (var key in document.did_inline) { + if (key == 'extend') continue; // Protoype Array() fix var el = document.getElementById(key); if (el) el.innerHTML = ''; } @@ -937,6 +960,7 @@ document.validate = function (form, val_hash) { var d = document.did_inline = new Array(); var hash = err_obj.as_hash(); for (var key in hash) { + if (key == 'extend') continue; // Protoype Array() fix var el = document.getElementById(key); if (el) el.innerHTML = hash[key]; d[key] = 1; @@ -1035,7 +1059,8 @@ document.check_form = function (form, val_hash) { document.load_val_hash(form, val_hash); // attach handler - form.onsubmit = function () {return document.validate(this)}; + var orig_submit = form.onsubmit || function () { return true }; + form.onsubmit = function (e) { return document.validate(this) && orig_submit(e, this) }; } // the end //