annotate static/js/tiny_mce/utils/validate.js @ 333:0bf5a5677067

Import from future with_statement for Python 2.5 in import_old_links and podcasts scripts.
author Brian Neal <bgneal@gmail.com>
date Fri, 25 Feb 2011 22:03:08 +0000
parents 88b2b9cb8c1f
children 6c182ceb7147
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@312 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@312 99 for (i=0; i<nl.length; i++)
bgneal@312 100 this.removeClass(nl[i], s.invalid_cls);
bgneal@312 101
bgneal@312 102 c += this.validateElms(f, 'input');
bgneal@312 103 c += this.validateElms(f, 'select');
bgneal@312 104 c += this.validateElms(f, 'textarea');
bgneal@312 105
bgneal@312 106 return c == 3;
bgneal@312 107 },
bgneal@312 108
bgneal@312 109 invalidate : function(n) {
bgneal@312 110 this.mark(n.form, n);
bgneal@312 111 },
bgneal@312 112
bgneal@312 113 reset : function(e) {
bgneal@312 114 var t = ['label', 'input', 'select', 'textarea'];
bgneal@312 115 var i, j, nl, s = this.settings;
bgneal@312 116
bgneal@312 117 if (e == null)
bgneal@312 118 return;
bgneal@312 119
bgneal@312 120 for (i=0; i<t.length; i++) {
bgneal@312 121 nl = this.tags(e.form ? e.form : e, t[i]);
bgneal@312 122 for (j=0; j<nl.length; j++)
bgneal@312 123 this.removeClass(nl[j], s.invalid_cls);
bgneal@312 124 }
bgneal@312 125 },
bgneal@312 126
bgneal@312 127 validateElms : function(f, e) {
bgneal@312 128 var nl, i, n, s = this.settings, st = true, va = Validator, v;
bgneal@312 129
bgneal@312 130 nl = this.tags(f, e);
bgneal@312 131 for (i=0; i<nl.length; i++) {
bgneal@312 132 n = nl[i];
bgneal@312 133
bgneal@312 134 this.removeClass(n, s.invalid_cls);
bgneal@312 135
bgneal@312 136 if (this.hasClass(n, s.required_cls) && va.isEmpty(n))
bgneal@312 137 st = this.mark(f, n);
bgneal@312 138
bgneal@312 139 if (this.hasClass(n, s.number_cls) && !va.isNumber(n))
bgneal@312 140 st = this.mark(f, n);
bgneal@312 141
bgneal@312 142 if (this.hasClass(n, s.int_cls) && !va.isNumber(n, true))
bgneal@312 143 st = this.mark(f, n);
bgneal@312 144
bgneal@312 145 if (this.hasClass(n, s.url_cls) && !va.isAbsUrl(n))
bgneal@312 146 st = this.mark(f, n);
bgneal@312 147
bgneal@312 148 if (this.hasClass(n, s.email_cls) && !va.isEmail(n))
bgneal@312 149 st = this.mark(f, n);
bgneal@312 150
bgneal@312 151 if (this.hasClass(n, s.size_cls) && !va.isSize(n))
bgneal@312 152 st = this.mark(f, n);
bgneal@312 153
bgneal@312 154 if (this.hasClass(n, s.id_cls) && !va.isId(n))
bgneal@312 155 st = this.mark(f, n);
bgneal@312 156
bgneal@312 157 if (this.hasClass(n, s.min_cls, true)) {
bgneal@312 158 v = this.getNum(n, s.min_cls);
bgneal@312 159
bgneal@312 160 if (isNaN(v) || parseInt(n.value) < parseInt(v))
bgneal@312 161 st = this.mark(f, n);
bgneal@312 162 }
bgneal@312 163
bgneal@312 164 if (this.hasClass(n, s.max_cls, true)) {
bgneal@312 165 v = this.getNum(n, s.max_cls);
bgneal@312 166
bgneal@312 167 if (isNaN(v) || parseInt(n.value) > parseInt(v))
bgneal@312 168 st = this.mark(f, n);
bgneal@312 169 }
bgneal@312 170 }
bgneal@312 171
bgneal@312 172 return st;
bgneal@312 173 },
bgneal@312 174
bgneal@312 175 hasClass : function(n, c, d) {
bgneal@312 176 return new RegExp('\\b' + c + (d ? '[0-9]+' : '') + '\\b', 'g').test(n.className);
bgneal@312 177 },
bgneal@312 178
bgneal@312 179 getNum : function(n, c) {
bgneal@312 180 c = n.className.match(new RegExp('\\b' + c + '([0-9]+)\\b', 'g'))[0];
bgneal@312 181 c = c.replace(/[^0-9]/g, '');
bgneal@312 182
bgneal@312 183 return c;
bgneal@312 184 },
bgneal@312 185
bgneal@312 186 addClass : function(n, c, b) {
bgneal@312 187 var o = this.removeClass(n, c);
bgneal@312 188 n.className = b ? c + (o != '' ? (' ' + o) : '') : (o != '' ? (o + ' ') : '') + c;
bgneal@312 189 },
bgneal@312 190
bgneal@312 191 removeClass : function(n, c) {
bgneal@312 192 c = n.className.replace(new RegExp("(^|\\s+)" + c + "(\\s+|$)"), ' ');
bgneal@312 193 return n.className = c != ' ' ? c : '';
bgneal@312 194 },
bgneal@312 195
bgneal@312 196 tags : function(f, s) {
bgneal@312 197 return f.getElementsByTagName(s);
bgneal@312 198 },
bgneal@312 199
bgneal@312 200 mark : function(f, n) {
bgneal@312 201 var s = this.settings;
bgneal@312 202
bgneal@312 203 this.addClass(n, s.invalid_cls);
bgneal@312 204 this.markLabels(f, n, s.invalid_cls);
bgneal@312 205
bgneal@312 206 return false;
bgneal@312 207 },
bgneal@312 208
bgneal@312 209 markLabels : function(f, n, ic) {
bgneal@312 210 var nl, i;
bgneal@312 211
bgneal@312 212 nl = this.tags(f, "label");
bgneal@312 213 for (i=0; i<nl.length; i++) {
bgneal@312 214 if (nl[i].getAttribute("for") == n.id || nl[i].htmlFor == n.id)
bgneal@312 215 this.addClass(nl[i], ic);
bgneal@312 216 }
bgneal@312 217
bgneal@312 218 return null;
bgneal@312 219 }
bgneal@312 220 };