annotate static/js/tiny_mce/utils/validate.js @ 557:7247a406f92b

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