annotate static/js/tinymce/jscripts/tiny_mce/utils/validate.js @ 113:8a0076d7d041

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