[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/tinymce/utils/ -> validate.js (source)

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


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref