X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fp5-CGI-Ex;a=blobdiff_plain;f=lib%2FCGI%2FEx%2Fvalidate.js;fp=lib%2FCGI%2FEx%2Fvalidate.js;h=a3c0db05c4557f052f4c5a163444cd1f9a868466;hp=7b469115ba07c99fc76b2498ccf5f64454201544;hb=6ab8b2e8e8388d1a238148a1ee58e124855f3768;hpb=fdecaac30a1168ed894c46d61b6c95524ec62a4e diff --git a/lib/CGI/Ex/validate.js b/lib/CGI/Ex/validate.js index 7b46911..a3c0db0 100644 --- a/lib/CGI/Ex/validate.js +++ b/lib/CGI/Ex/validate.js @@ -1,4 +1,4 @@ -// Copyright 2008 - Paul Seamons - $Revision: 1.18 $ +// Copyright 2003-2012 - Paul Seamons - ver 2.37 // Distributed under the Perl Artistic License without warranty // See perldoc CGI::Ex::Validate for usage @@ -25,7 +25,7 @@ function v_get_ordered_fields (val_hash) { var field_keys = []; var m; for (var key in val_hash) { - if (key == 'extend') continue; // Protoype Array() + if (!val_hash.hasOwnProperty(key)) continue; if (m = key.match(/^(general|group)\s+(\w+)/)) { ARGS[m[2]] = val_hash[key]; continue; @@ -97,16 +97,16 @@ function v_clean_field_val (field_val, N_level) { var k = field_val.order[i]; var v = field_val[k]; if (typeof(v) == 'undefined') return {error:v_error('No matching validation found on field '+field+' for type '+k)}; - if (k.match(/^(min|max)_in_set(\d*)$/)) { + if (k.match(/^(min|max)_in_set_?(\d*)$/)) { if (typeof(v) == 'string') { if (! (m = v.match(/^\s*(\d+)(?:\s*[oO][fF])?\s+(.+)\s*$/))) return {error:v_error("Invalid "+k+" check "+v)}; field_val[k] = m[2].split(/[\s,]+/); field_val[k].unshift(m[1]); } for (var j = 1; j < field_val[k].length; j++) if (field_val[k][j] != field_val.field) field_val.deps[field_val[k][j]] = 1; - } else if (k.match(/^(enum|compare)\d*$/)) { + } else if (k.match(/^(enum|compare)_?\d*$/)) { if (typeof(v) == 'string') field_val[k] = v.split(/\s*\|\|\s*/); - } else if (k.match(/^match\d*$/)) { + } else if (k.match(/^match_?\d*$/)) { if (typeof(v) == 'string') v = field_val[k] = v.split(/\s*\|\|\s*/); for (var j = 0; j < v.length; j++) { if (typeof(v[j]) != 'string' || v[j] == '!') continue; @@ -120,9 +120,9 @@ function v_clean_field_val (field_val, N_level) { v[j] = new RegExp(pat, opt); if (not) v.splice(j, 0, '!'); } - } else if (k.match(/^custom_js\d*$/)) { + } else if (k.match(/^custom_js_?\d*$/)) { if (typeof(v) == 'string' && v.match(/^\s*function\s*\(/)) eval("field_val[k] = "+v); - } else if (k.match(/^(validate|required)_if\d*$/)) { + } else if (k.match(/^(validate|required)_if_?\d*$/)) { if (typeof(v) == 'string' || ! v.length) v = field_val[k] = [v]; var deps = v_clean_cond(v, N_level); for (var k in deps) field_val.deps[k] = 2; @@ -248,8 +248,8 @@ function v_filter_types (type, types) { return values; } -function v_add_error (errors,field,type,field_val,ifs_match,form) { - errors.push([field, type, field_val, ifs_match]); +function v_add_error (errors,field,type,field_val,ifs_match,form,custom_err) { + errors.push([field, type, field_val, ifs_match, custom_err]); if (field_val.clear_on_error) { var el = form[field]; if (el && el.type && el.type.match(/(hidden|password|text|textarea|submit)/)) el.value = ''; @@ -260,7 +260,7 @@ function v_add_error (errors,field,type,field_val,ifs_match,form) { function v_field_order (field_val) { var o = []; for (var k in field_val) - if (! k.match(/^(extend|field|name|required|was_valid|was_checked|had_error)$/) && ! k.match(/_error$/)) o.push(k); + if (field_val.hasOwnProperty(k) && ! k.match(/^(field|name|required|was_valid|was_checked|had_error)$/) && ! k.match(/_error$/)) o.push(k); return o.sort(); } @@ -408,19 +408,29 @@ function v_validate_buddy (form, field, field_val, val_hash, ifs_match) { } } - for (var i = 0; i < types.length; i++) { - var type = types[i]; - var _fv = field_val[type]; - for (var n = 0; n < values.length; n++) { - var value = values[n]; + for (var n = 0; n < values.length; n++) { + var value = values[n]; + + if (typeof field_val['enum'] != 'undefined') { + var is_found = 0; + for (var j = 0; j < field_val['enum'].length; j++) if (value == field_val['enum'][j]) { is_found = 1; break } + if (! is_found) { + v_add_error(errors, field, 'enum', field_val, ifs_match, form); + continue; + } + } - if (type.match(/^enum\d*$/)) { - var is_found = 0; - for (var j = 0; j < _fv.length; j++) if (value == _fv[j]) { is_found = 1; break } - if (! is_found) v_add_error(errors, field, type, field_val, ifs_match, form); + if (typeof field_val['type'] != 'undefined') + if (! v_check_type(value, field_val['type'], field, form)) { + v_add_error(errors, field, 'type', field_val, ifs_match, form); + continue; } - if (type.match(/^equals\d*$/)) { + for (var i = 0; i < types.length; i++) { + var type = types[i]; + var _fv = field_val[type]; + + if (type.match(/^equals_?\d*$/)) { var not = _fv.match(/^!\s*/); if (not) _fv = _fv.substring(not[0].length); var success = 0; @@ -431,14 +441,16 @@ function v_validate_buddy (form, field, field_val, val_hash, ifs_match) { if (typeof(value2) == 'undefined') value2 = ''; if (value == value2) success = 1; } - if (not && success || ! not && ! success) + if (not && success || ! not && ! success) { v_add_error(errors, field, type, field_val, ifs_match, form); + break; + } } if (type == 'min_len' && value.length < _fv) v_add_error(errors, field, 'min_len', field_val, ifs_match, form); if (type == 'max_len' && value.length > _fv) v_add_error(errors, field, 'max_len', field_val, ifs_match, form); - if (type.match(/^match\d*$/)) { + if (type.match(/^match_?\d*$/)) { for (var j = 0; j < _fv.length; j++) { if (typeof(_fv[j]) == 'string') continue; var not = (j > 0 && typeof(_fv[j-1]) == 'string' && _fv[j-1] == '!') ? 1 : 0; @@ -447,7 +459,7 @@ function v_validate_buddy (form, field, field_val, val_hash, ifs_match) { } } - if (type.match(/^compare\d*$/)) { + if (type.match(/^compare_?\d*$/)) { for (var j = 0; j < _fv.length; j++) { var comp = _fv[j]; if (! comp) continue; @@ -478,19 +490,20 @@ function v_validate_buddy (form, field, field_val, val_hash, ifs_match) { if (! hold) v_add_error(errors, field, type, field_val, ifs_match, form); } } - - if (type.match(/^type\d*$/)) - if (! v_check_type(value, _fv, field, form)) - v_add_error(errors, field, type, field_val, ifs_match, form); } + } + for (var i = 0; i < types.length; i++) { + var type = types[i]; + var _fv = field_val[type]; // the js is evaluated and should return 1 for success // or 0 for failure - the variables field, value, and field_val (the hash) are available - if (type.match(/^custom_js\d*$/)) { + if (type.match(/^custom_js_?\d*$/)) { var value = values.length == 1 ? values[0] : values; - if (typeof(_fv) == 'function' - ? ! _fv({'value':value, 'field_val':field_val, 'form':form, 'key':field_val.field, 'errors':errors, 'event':v_event}) - : ! eval(_fv)) v_add_error(errors, field, type, field_val, ifs_match, form); + var err; + var ok; + try { ok = (typeof _fv == 'function') ? _fv({'value':value, 'field_val':field_val, 'form':form, 'key':field_val.field, 'errors':errors, 'event':v_event}) : eval(_fv) } catch (e) { err = e } + if (!ok) v_add_error(errors, field, type, field_val, ifs_match, form, err); } } @@ -542,6 +555,15 @@ function v_check_type (value, type, field, form) { if (! value) return 0; if (value.match(/\s/)) return 0; + } else if (type == 'INT') { + if (!value.match(/^-?(?:0|[1-9]\d*)$/)) return 0; + if ((value < 0) ? value < -Math.pow(2,31) : value > Math.pow(2,31)-1) return 0; + } else if (type == 'UINT') { + if (!value.match(/^(?:0|[1-9]\d*)$/)) return 0; + if (value > Math.pow(2,32)-1) return 0; + } else if (type == 'NUM') { + if (!value.match(/^-?(?:0|[1-9]\d*(?:\.\d+)?|0?\.\d+)$/)) return 0; + } else if (type == 'CC') { if (! value) return 0; if (value.match(/[^\d\- ]/)) return 0; @@ -644,10 +666,11 @@ function v_get_error_text (err, extra1, extra2) { var type = err[1]; var field_val = err[2]; var ifs_match = err[3]; - var m; + if (err.length == 5 && typeof err[4] != 'undefined' && err[4].length) return err[4]; // custom error from throw in custom_js + var m; var dig = ''; - if (m = type.match(/^(.+?)(\d+)$/)) { type = m[1] ; dig = m[2] } + if (m = type.match(/^(.+?)(_?\d+)$/)) { type = m[1] ; dig = m[2] } var type_lc = type.toLowerCase(); var v = field_val[type + dig]; @@ -795,7 +818,7 @@ function eob_as_hash (extra) { var header = v_find_val('as_hash_header', extra, this.extra, ''); var footer = v_find_val('as_hash_footer', extra, this.extra, ''); for (var key in ret) { - if (key == 'extend') continue; // Protoype Array() + if (!ret.hasOwnProperty(key)) continue; ret[key] = header + ret[key].join(joiner) + footer; } } @@ -820,7 +843,7 @@ document.validate = function (form, val_hash) { if (v_event != 'load') { for (var key in v_did_inline) { - if (key == 'extend') continue; // Protoype Array() + if (!v_did_inline.hasOwnProperty(key)) continue; v_inline_error_clear(key, val_hash, form); } } @@ -844,7 +867,7 @@ document.validate = function (form, val_hash) { if (! val_hash['group no_inline']) { var hash = err_obj.as_hash({as_hash_suffix:""}); for (var key in hash) { - if (key == 'extend') continue; // Protoype Array() + if (!hash.hasOwnProperty(key)) continue; v_inline_error_set(key, hash[key], val_hash, form); } } @@ -946,7 +969,7 @@ document.check_form = function (form, val_hash) { for (var j in clean.fields[i].deps) if (j != clean.fields[i].field) _add(j, clean.fields[i]); } for (var k in h) { - if (k == 'extend') continue; // Protoype Array() + if (!h.hasOwnProperty(k)) continue; var el = form[k]; if (! el) return v_error("No form element by the name "+k); var _change = !types.change ? 0 : typeof(types.change) == 'object' ? types.change[k] : 1; @@ -997,7 +1020,7 @@ function v_el_attach (el, fvs, form, val_hash, _change, _blur) { e = new ValidateError(e, {}); e = e.as_hash({as_hash_suffix:"", first_only:(val_hash['group first_only']?1:0)}); for (var k in e) { - if (k == 'extend') continue; // Protoype Array() + if (!e.hasOwnProperty(k)) continue; v_inline_error_set(k, e[k], val_hash, form); } };