X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fp5-CGI-Ex;a=blobdiff_plain;f=lib%2FCGI%2FEx%2Fvalidate.js;h=40b4c04d6d7f92d615df5c69fe65c9e5e7c9849e;hp=d8a62704c4bfdfe04b9ac0f212f0187d46e5fe94;hb=a9adfe5a3786f860510fcf10792958a25e1a7727;hpb=d710d6cd21be21c0ab2df3566c2bd61d9015cac6 diff --git a/lib/CGI/Ex/validate.js b/lib/CGI/Ex/validate.js index d8a6270..40b4c04 100644 --- a/lib/CGI/Ex/validate.js +++ b/lib/CGI/Ex/validate.js @@ -1,10 +1,10 @@ /**----------------------------------------------------------------*** -* Copyright 2006 - 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.36 $ +// $Revision: 1.42 $ function Validate () { this.error = vob_error; @@ -233,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 @@ -291,6 +299,7 @@ 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]]; @@ -303,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) { @@ -370,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; } @@ -379,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; } } @@ -393,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; } } @@ -424,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; } } @@ -445,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 @@ -463,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 @@ -489,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]+")"); @@ -502,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); } } } @@ -538,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); } } @@ -546,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 @@ -555,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 @@ -578,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') {