b2evolution PHP Cross Reference Blogging Systems

Source: /plugins/tinymce_plugin/tiny_mce/utils/validate.js - 254 lines - 5925 bytes - Text - Print

Description: Various form validation methods. Copyright 2009, Moxiecode Systems AB Released under LGPL License.

   1  /**
   2   * $Id: validate.js 829 2012-02-14 07:33:03Z attila $
   3   *
   4   * Various form validation methods.
   5   *
   6   * Copyright 2009, Moxiecode Systems AB
   7   * Released under LGPL License.
   8   *
   9   * License: http://tinymce.moxiecode.com/license
  10   * Contributing: http://tinymce.moxiecode.com/contributing
  11   */
  12  
  13  /**
  14      // String validation:
  15  
  16      if (!Validator.isEmail('myemail'))
  17          alert('Invalid email.');
  18  
  19      // Form validation:
  20  
  21      var f = document.forms['myform'];
  22  
  23      if (!Validator.isEmail(f.myemail))
  24          alert('Invalid email.');
  25  */
  26  
  27  var Validator = {
  28      isEmail : function(s) {
  29          return this.test(s, '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$');
  30      },
  31  
  32      isAbsUrl : function(s) {
  33          return this.test(s, '^(news|telnet|nttp|file|http|ftp|https)://[-A-Za-z0-9\\.]+\\/?.*$');
  34      },
  35  
  36      isSize : function(s) {
  37          return this.test(s, '^[0-9.]+(%|in|cm|mm|em|ex|pt|pc|px)?$');
  38      },
  39  
  40      isId : function(s) {
  41          return this.test(s, '^[A-Za-z_]([A-Za-z0-9_])*$');
  42      },
  43  
  44      isEmpty : function(s) {
  45          var nl, i;
  46  
  47          if (s.nodeName == 'SELECT' && s.selectedIndex < 1)
  48              return true;
  49  
  50          if (s.type == 'checkbox' && !s.checked)
  51              return true;
  52  
  53          if (s.type == 'radio') {
  54              for (i=0, nl = s.form.elements; i<nl.length; i++) {
  55                  if (nl[i].type == "radio" && nl[i].name == s.name && nl[i].checked)
  56                      return false;
  57              }
  58  
  59              return true;
  60          }
  61  
  62          return new RegExp('^\\s*$').test(s.nodeType == 1 ? s.value : s);
  63      },
  64  
  65      isNumber : function(s, d) {
  66          return !isNaN(s.nodeType == 1 ? s.value : s) && (!d || !this.test(s, '^-?[0-9]*\\.[0-9]*$'));
  67      },
  68  
  69      test : function(s, p) {
  70          s = s.nodeType == 1 ? s.value : s;
  71  
  72          return s == '' || new RegExp(p).test(s);
  73      }
  74  };
  75  
  76  var AutoValidator = {
  77      settings : {
  78          id_cls : 'id',
  79          int_cls : 'int',
  80          url_cls : 'url',
  81          number_cls : 'number',
  82          email_cls : 'email',
  83          size_cls : 'size',
  84          required_cls : 'required',
  85          invalid_cls : 'invalid',
  86          min_cls : 'min',
  87          max_cls : 'max'
  88      },
  89  
  90      init : function(s) {
  91          var n;
  92  
  93          for (n in s)
  94              this.settings[n] = s[n];
  95      },
  96  
  97      validate : function(f) {
  98          var i, nl, s = this.settings, c = 0;
  99  
 100          nl = this.tags(f, 'label');
 101          for (i=0; i<nl.length; i++) {
 102              this.removeClass(nl[i], s.invalid_cls);
 103              nl[i].setAttribute('aria-invalid', false);
 104          }
 105  
 106          c += this.validateElms(f, 'input');
 107          c += this.validateElms(f, 'select');
 108          c += this.validateElms(f, 'textarea');
 109  
 110          return c == 3;
 111      },
 112  
 113      invalidate : function(n) {
 114          this.mark(n.form, n);
 115      },
 116  
 117      getErrorMessages : function(f) {
 118          var nl, i, s = this.settings, field, msg, values, messages = [], ed = tinyMCEPopup.editor;
 119          nl = this.tags(f, "label");
 120          for (i=0; i<nl.length; i++) {
 121              if (this.hasClass(nl[i], s.invalid_cls)) {
 122                  field = document.getElementById(nl[i].getAttribute("for"));
 123                  values = { field: nl[i].textContent };
 124                  if (this.hasClass(field, s.min_cls, true)) {
 125                      message = ed.getLang('invalid_data_min');
 126                      values.min = this.getNum(field, s.min_cls);
 127                  } else if (this.hasClass(field, s.number_cls)) {
 128                      message = ed.getLang('invalid_data_number');
 129                  } else if (this.hasClass(field, s.size_cls)) {
 130                      message = ed.getLang('invalid_data_size');
 131                  } else {
 132                      message = ed.getLang('invalid_data');
 133                  }
 134                  
 135                  message = message.replace(/{\#([^}]+)\}/g, function(a, b) {
 136                      return values[b] || '{#' + b + '}';
 137                  });
 138                  messages.push(message);
 139              }
 140          }
 141          return messages;
 142      },
 143  
 144      reset : function(e) {
 145          var t = ['label', 'input', 'select', 'textarea'];
 146          var i, j, nl, s = this.settings;
 147  
 148          if (e == null)
 149              return;
 150  
 151          for (i=0; i<t.length; i++) {
 152              nl = this.tags(e.form ? e.form : e, t[i]);
 153              for (j=0; j<nl.length; j++) {
 154                  this.removeClass(nl[j], s.invalid_cls);
 155                  nl[j].setAttribute('aria-invalid', false);
 156              }
 157          }
 158      },
 159  
 160      validateElms : function(f, e) {
 161          var nl, i, n, s = this.settings, st = true, va = Validator, v;
 162  
 163          nl = this.tags(f, e);
 164          for (i=0; i<nl.length; i++) {
 165              n = nl[i];
 166  
 167              this.removeClass(n, s.invalid_cls);
 168  
 169              if (this.hasClass(n, s.required_cls) && va.isEmpty(n))
 170                  st = this.mark(f, n);
 171  
 172              if (this.hasClass(n, s.number_cls) && !va.isNumber(n))
 173                  st = this.mark(f, n);
 174  
 175              if (this.hasClass(n, s.int_cls) && !va.isNumber(n, true))
 176                  st = this.mark(f, n);
 177  
 178              if (this.hasClass(n, s.url_cls) && !va.isAbsUrl(n))
 179                  st = this.mark(f, n);
 180  
 181              if (this.hasClass(n, s.email_cls) && !va.isEmail(n))
 182                  st = this.mark(f, n);
 183  
 184              if (this.hasClass(n, s.size_cls) && !va.isSize(n))
 185                  st = this.mark(f, n);
 186  
 187              if (this.hasClass(n, s.id_cls) && !va.isId(n))
 188                  st = this.mark(f, n);
 189  
 190              if (this.hasClass(n, s.min_cls, true)) {
 191                  v = this.getNum(n, s.min_cls);
 192  
 193                  if (isNaN(v) || parseInt(n.value) < parseInt(v))
 194                      st = this.mark(f, n);
 195              }
 196  
 197              if (this.hasClass(n, s.max_cls, true)) {
 198                  v = this.getNum(n, s.max_cls);
 199  
 200                  if (isNaN(v) || parseInt(n.value) > parseInt(v))
 201                      st = this.mark(f, n);
 202              }
 203          }
 204  
 205          return st;
 206      },
 207  
 208      hasClass : function(n, c, d) {
 209          return new RegExp('\\b' + c + (d ? '[0-9]+' : '') + '\\b', 'g').test(n.className);
 210      },
 211  
 212      getNum : function(n, c) {
 213          c = n.className.match(new RegExp('\\b' + c + '([0-9]+)\\b', 'g'))[0];
 214          c = c.replace(/[^0-9]/g, '');
 215  
 216          return c;
 217      },
 218  
 219      addClass : function(n, c, b) {
 220          var o = this.removeClass(n, c);
 221          n.className = b ? c + (o != '' ? (' ' + o) : '') : (o != '' ? (o + ' ') : '') + c;
 222      },
 223  
 224      removeClass : function(n, c) {
 225          c = n.className.replace(new RegExp("(^|\\s+)" + c + "(\\s+|$)"), ' ');
 226          return n.className = c != ' ' ? c : '';
 227      },
 228  
 229      tags : function(f, s) {
 230          return f.getElementsByTagName(s);
 231      },
 232  
 233      mark : function(f, n) {
 234          var s = this.settings;
 235  
 236          this.addClass(n, s.invalid_cls);
 237          n.setAttribute('aria-invalid', 'true');
 238          this.markLabels(f, n, s.invalid_cls);
 239  
 240          return false;
 241      },
 242  
 243      markLabels : function(f, n, ic) {
 244          var nl, i;
 245  
 246          nl = this.tags(f, "label");
 247          for (i=0; i<nl.length; i++) {
 248              if (nl[i].getAttribute("for") == n.id || nl[i].htmlFor == n.id)
 249                  this.addClass(nl[i], ic);
 250          }
 251  
 252          return null;
 253      }
 254  };

title

Description

title

Description

title

Description

title

title

Body