annotate media/js/tiny_mce/utils/validate.js @ 177:9b63ad1f2ad2

Fixing #59, again. Django ticket 13093 was fixed allowing cache_page to work in the URLconf.
author Brian Neal <bgneal@gmail.com>
date Mon, 15 Mar 2010 03:26:38 +0000
parents a5b4c5ce0658
children 149c3567fec1
rev   line source
bgneal@45 1 /**
bgneal@45 2 * $Id: validate.js 758 2008-03-30 13:53:29Z spocke $
bgneal@45 3 *
bgneal@45 4 * Various form validation methods.
bgneal@45 5 *
bgneal@45 6 * @author Moxiecode
bgneal@45 7 * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved.
bgneal@45 8 */
bgneal@45 9
bgneal@45 10 /**
bgneal@45 11 // String validation:
bgneal@45 12
bgneal@45 13 if (!Validator.isEmail('myemail'))
bgneal@45 14 alert('Invalid email.');
bgneal@45 15
bgneal@45 16 // Form validation:
bgneal@45 17
bgneal@45 18 var f = document.forms['myform'];
bgneal@45 19
bgneal@45 20 if (!Validator.isEmail(f.myemail))
bgneal@45 21 alert('Invalid email.');
bgneal@45 22 */
bgneal@45 23
bgneal@45 24 var Validator = {
bgneal@45 25 isEmail : function(s) {
bgneal@45 26 return this.test(s, '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$');
bgneal@45 27 },
bgneal@45 28
bgneal@45 29 isAbsUrl : function(s) {
bgneal@45 30 return this.test(s, '^(news|telnet|nttp|file|http|ftp|https)://[-A-Za-z0-9\\.]+\\/?.*$');
bgneal@45 31 },
bgneal@45 32
bgneal@45 33 isSize : function(s) {
bgneal@45 34 return this.test(s, '^[0-9]+(%|in|cm|mm|em|ex|pt|pc|px)?$');
bgneal@45 35 },
bgneal@45 36
bgneal@45 37 isId : function(s) {
bgneal@45 38 return this.test(s, '^[A-Za-z_]([A-Za-z0-9_])*$');
bgneal@45 39 },
bgneal@45 40
bgneal@45 41 isEmpty : function(s) {
bgneal@45 42 var nl, i;
bgneal@45 43
bgneal@45 44 if (s.nodeName == 'SELECT' && s.selectedIndex < 1)
bgneal@45 45 return true;
bgneal@45 46
bgneal@45 47 if (s.type == 'checkbox' && !s.checked)
bgneal@45 48 return true;
bgneal@45 49
bgneal@45 50 if (s.type == 'radio') {
bgneal@45 51 for (i=0, nl = s.form.elements; i<nl.length; i++) {
bgneal@45 52 if (nl[i].type == "radio" && nl[i].name == s.name && nl[i].checked)
bgneal@45 53 return false;
bgneal@45 54 }
bgneal@45 55
bgneal@45 56 return true;
bgneal@45 57 }
bgneal@45 58
bgneal@45 59 return new RegExp('^\\s*$').test(s.nodeType == 1 ? s.value : s);
bgneal@45 60 },
bgneal@45 61
bgneal@45 62 isNumber : function(s, d) {
bgneal@45 63 return !isNaN(s.nodeType == 1 ? s.value : s) && (!d || !this.test(s, '^-?[0-9]*\\.[0-9]*$'));
bgneal@45 64 },
bgneal@45 65
bgneal@45 66 test : function(s, p) {
bgneal@45 67 s = s.nodeType == 1 ? s.value : s;
bgneal@45 68
bgneal@45 69 return s == '' || new RegExp(p).test(s);
bgneal@45 70 }
bgneal@45 71 };
bgneal@45 72
bgneal@45 73 var AutoValidator = {
bgneal@45 74 settings : {
bgneal@45 75 id_cls : 'id',
bgneal@45 76 int_cls : 'int',
bgneal@45 77 url_cls : 'url',
bgneal@45 78 number_cls : 'number',
bgneal@45 79 email_cls : 'email',
bgneal@45 80 size_cls : 'size',
bgneal@45 81 required_cls : 'required',
bgneal@45 82 invalid_cls : 'invalid',
bgneal@45 83 min_cls : 'min',
bgneal@45 84 max_cls : 'max'
bgneal@45 85 },
bgneal@45 86
bgneal@45 87 init : function(s) {
bgneal@45 88 var n;
bgneal@45 89
bgneal@45 90 for (n in s)
bgneal@45 91 this.settings[n] = s[n];
bgneal@45 92 },
bgneal@45 93
bgneal@45 94 validate : function(f) {
bgneal@45 95 var i, nl, s = this.settings, c = 0;
bgneal@45 96
bgneal@45 97 nl = this.tags(f, 'label');
bgneal@45 98 for (i=0; i<nl.length; i++)
bgneal@45 99 this.removeClass(nl[i], s.invalid_cls);
bgneal@45 100
bgneal@45 101 c += this.validateElms(f, 'input');
bgneal@45 102 c += this.validateElms(f, 'select');
bgneal@45 103 c += this.validateElms(f, 'textarea');
bgneal@45 104
bgneal@45 105 return c == 3;
bgneal@45 106 },
bgneal@45 107
bgneal@45 108 invalidate : function(n) {
bgneal@45 109 this.mark(n.form, n);
bgneal@45 110 },
bgneal@45 111
bgneal@45 112 reset : function(e) {
bgneal@45 113 var t = ['label', 'input', 'select', 'textarea'];
bgneal@45 114 var i, j, nl, s = this.settings;
bgneal@45 115
bgneal@45 116 if (e == null)
bgneal@45 117 return;
bgneal@45 118
bgneal@45 119 for (i=0; i<t.length; i++) {
bgneal@45 120 nl = this.tags(e.form ? e.form : e, t[i]);
bgneal@45 121 for (j=0; j<nl.length; j++)
bgneal@45 122 this.removeClass(nl[j], s.invalid_cls);
bgneal@45 123 }
bgneal@45 124 },
bgneal@45 125
bgneal@45 126 validateElms : function(f, e) {
bgneal@45 127 var nl, i, n, s = this.settings, st = true, va = Validator, v;
bgneal@45 128
bgneal@45 129 nl = this.tags(f, e);
bgneal@45 130 for (i=0; i<nl.length; i++) {
bgneal@45 131 n = nl[i];
bgneal@45 132
bgneal@45 133 this.removeClass(n, s.invalid_cls);
bgneal@45 134
bgneal@45 135 if (this.hasClass(n, s.required_cls) && va.isEmpty(n))
bgneal@45 136 st = this.mark(f, n);
bgneal@45 137
bgneal@45 138 if (this.hasClass(n, s.number_cls) && !va.isNumber(n))
bgneal@45 139 st = this.mark(f, n);
bgneal@45 140
bgneal@45 141 if (this.hasClass(n, s.int_cls) && !va.isNumber(n, true))
bgneal@45 142 st = this.mark(f, n);
bgneal@45 143
bgneal@45 144 if (this.hasClass(n, s.url_cls) && !va.isAbsUrl(n))
bgneal@45 145 st = this.mark(f, n);
bgneal@45 146
bgneal@45 147 if (this.hasClass(n, s.email_cls) && !va.isEmail(n))
bgneal@45 148 st = this.mark(f, n);
bgneal@45 149
bgneal@45 150 if (this.hasClass(n, s.size_cls) && !va.isSize(n))
bgneal@45 151 st = this.mark(f, n);
bgneal@45 152
bgneal@45 153 if (this.hasClass(n, s.id_cls) && !va.isId(n))
bgneal@45 154 st = this.mark(f, n);
bgneal@45 155
bgneal@45 156 if (this.hasClass(n, s.min_cls, true)) {
bgneal@45 157 v = this.getNum(n, s.min_cls);
bgneal@45 158
bgneal@45 159 if (isNaN(v) || parseInt(n.value) < parseInt(v))
bgneal@45 160 st = this.mark(f, n);
bgneal@45 161 }
bgneal@45 162
bgneal@45 163 if (this.hasClass(n, s.max_cls, true)) {
bgneal@45 164 v = this.getNum(n, s.max_cls);
bgneal@45 165
bgneal@45 166 if (isNaN(v) || parseInt(n.value) > parseInt(v))
bgneal@45 167 st = this.mark(f, n);
bgneal@45 168 }
bgneal@45 169 }
bgneal@45 170
bgneal@45 171 return st;
bgneal@45 172 },
bgneal@45 173
bgneal@45 174 hasClass : function(n, c, d) {
bgneal@45 175 return new RegExp('\\b' + c + (d ? '[0-9]+' : '') + '\\b', 'g').test(n.className);
bgneal@45 176 },
bgneal@45 177
bgneal@45 178 getNum : function(n, c) {
bgneal@45 179 c = n.className.match(new RegExp('\\b' + c + '([0-9]+)\\b', 'g'))[0];
bgneal@45 180 c = c.replace(/[^0-9]/g, '');
bgneal@45 181
bgneal@45 182 return c;
bgneal@45 183 },
bgneal@45 184
bgneal@45 185 addClass : function(n, c, b) {
bgneal@45 186 var o = this.removeClass(n, c);
bgneal@45 187 n.className = b ? c + (o != '' ? (' ' + o) : '') : (o != '' ? (o + ' ') : '') + c;
bgneal@45 188 },
bgneal@45 189
bgneal@45 190 removeClass : function(n, c) {
bgneal@45 191 c = n.className.replace(new RegExp("(^|\\s+)" + c + "(\\s+|$)"), ' ');
bgneal@45 192 return n.className = c != ' ' ? c : '';
bgneal@45 193 },
bgneal@45 194
bgneal@45 195 tags : function(f, s) {
bgneal@45 196 return f.getElementsByTagName(s);
bgneal@45 197 },
bgneal@45 198
bgneal@45 199 mark : function(f, n) {
bgneal@45 200 var s = this.settings;
bgneal@45 201
bgneal@45 202 this.addClass(n, s.invalid_cls);
bgneal@45 203 this.markLabels(f, n, s.invalid_cls);
bgneal@45 204
bgneal@45 205 return false;
bgneal@45 206 },
bgneal@45 207
bgneal@45 208 markLabels : function(f, n, ic) {
bgneal@45 209 var nl, i;
bgneal@45 210
bgneal@45 211 nl = this.tags(f, "label");
bgneal@45 212 for (i=0; i<nl.length; i++) {
bgneal@45 213 if (nl[i].getAttribute("for") == n.id || nl[i].htmlFor == n.id)
bgneal@45 214 this.addClass(nl[i], ic);
bgneal@45 215 }
bgneal@45 216
bgneal@45 217 return null;
bgneal@45 218 }
bgneal@45 219 };