]> Dogcows Code - chaz/p5-CGI-Ex/blobdiff - lib/CGI/Ex/validate.js
CGI::Ex 2.20
[chaz/p5-CGI-Ex] / lib / CGI / Ex / validate.js
index 8ac047976a473c7fc9ab25ca4e8a3906a58958a4..40b4c04d6d7f92d615df5c69fe65c9e5e7c9849e 100644 (file)
@@ -4,7 +4,7 @@
 *  Based upon CGI/Ex/Validate.pm v1.14 from Perl                     *
 *  For instructions on usage, see perldoc of CGI::Ex::Validate       *
 ***----------------------------------------------------------------**/
-// $Revision: 1.41 $
+// $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
@@ -373,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;
  }
 
@@ -382,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;
    }
  }
@@ -396,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;
    }
  }
@@ -427,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;
      }
    }
@@ -448,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
@@ -466,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
@@ -492,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]+")");
@@ -505,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);
          }
        }
      }
@@ -541,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);
      }
    }
 
@@ -549,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
@@ -558,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
@@ -581,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') {
This page took 0.021863 seconds and 4 git commands to generate.