annotate media/js/tiny_mce/utils/validate.js @ 286:72fd300685d5

For #95. You can now make posts with no text in the body if you have attachments. And now if you create a new topic with an attachment, and the POST fails (say you forgot the topic title), we will now re-attach attachments. Also fixed a bug in the smiley code that would arise if it was asked to markup an empty string.
author Brian Neal <bgneal@gmail.com>
date Sat, 23 Oct 2010 20:19:46 +0000
parents 149c3567fec1
children
rev   line source
bgneal@45 1 /**
bgneal@183 2 * validate.js
bgneal@45 3 *
bgneal@183 4 * Copyright 2009, Moxiecode Systems AB
bgneal@183 5 * Released under LGPL License.
bgneal@45 6 *
bgneal@183 7 * License: http://tinymce.moxiecode.com/license
bgneal@183 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
bgneal@45 102 c += this.validateElms(f, 'input');
bgneal@45 103 c += this.validateElms(f, 'select');
bgneal@45 104 c += this.validateElms(f, 'textarea');
bgneal@45 105
bgneal@45 106 return c == 3;
bgneal@45 107 },
bgneal@45 108
bgneal@45 109 invalidate : function(n) {
bgneal@45 110 this.mark(n.form, n);
bgneal@45 111 },
bgneal@45 112
bgneal@45 113 reset : function(e) {
bgneal@45 114 var t = ['label', 'input', 'select', 'textarea'];
bgneal@45 115 var i, j, nl, s = this.settings;
bgneal@45 116
bgneal@45 117 if (e == null)
bgneal@45 118 return;
bgneal@45 119
bgneal@45 120 for (i=0; i<t.length; i++) {
bgneal@45 121 nl = this.tags(e.form ? e.form : e, t[i]);
bgneal@45 122 for (j=0; j<nl.length; j++)
bgneal@45 123 this.removeClass(nl[j], s.invalid_cls);
bgneal@45 124 }
bgneal@45 125 },
bgneal@45 126
bgneal@45 127 validateElms : function(f, e) {
bgneal@45 128 var nl, i, n, s = this.settings, st = true, va = Validator, v;
bgneal@45 129
bgneal@45 130 nl = this.tags(f, e);
bgneal@45 131 for (i=0; i<nl.length; i++) {
bgneal@45 132 n = nl[i];
bgneal@45 133
bgneal@45 134 this.removeClass(n, s.invalid_cls);
bgneal@45 135
bgneal@45 136 if (this.hasClass(n, s.required_cls) && va.isEmpty(n))
bgneal@45 137 st = this.mark(f, n);
bgneal@45 138
bgneal@45 139 if (this.hasClass(n, s.number_cls) && !va.isNumber(n))
bgneal@45 140 st = this.mark(f, n);
bgneal@45 141
bgneal@45 142 if (this.hasClass(n, s.int_cls) && !va.isNumber(n, true))
bgneal@45 143 st = this.mark(f, n);
bgneal@45 144
bgneal@45 145 if (this.hasClass(n, s.url_cls) && !va.isAbsUrl(n))
bgneal@45 146 st = this.mark(f, n);
bgneal@45 147
bgneal@45 148 if (this.hasClass(n, s.email_cls) && !va.isEmail(n))
bgneal@45 149 st = this.mark(f, n);
bgneal@45 150
bgneal@45 151 if (this.hasClass(n, s.size_cls) && !va.isSize(n))
bgneal@45 152 st = this.mark(f, n);
bgneal@45 153
bgneal@45 154 if (this.hasClass(n, s.id_cls) && !va.isId(n))
bgneal@45 155 st = this.mark(f, n);
bgneal@45 156
bgneal@45 157 if (this.hasClass(n, s.min_cls, true)) {
bgneal@45 158 v = this.getNum(n, s.min_cls);
bgneal@45 159
bgneal@45 160 if (isNaN(v) || parseInt(n.value) < parseInt(v))
bgneal@45 161 st = this.mark(f, n);
bgneal@45 162 }
bgneal@45 163
bgneal@45 164 if (this.hasClass(n, s.max_cls, true)) {
bgneal@45 165 v = this.getNum(n, s.max_cls);
bgneal@45 166
bgneal@45 167 if (isNaN(v) || parseInt(n.value) > parseInt(v))
bgneal@45 168 st = this.mark(f, n);
bgneal@45 169 }
bgneal@45 170 }
bgneal@45 171
bgneal@45 172 return st;
bgneal@45 173 },
bgneal@45 174
bgneal@45 175 hasClass : function(n, c, d) {
bgneal@45 176 return new RegExp('\\b' + c + (d ? '[0-9]+' : '') + '\\b', 'g').test(n.className);
bgneal@45 177 },
bgneal@45 178
bgneal@45 179 getNum : function(n, c) {
bgneal@45 180 c = n.className.match(new RegExp('\\b' + c + '([0-9]+)\\b', 'g'))[0];
bgneal@45 181 c = c.replace(/[^0-9]/g, '');
bgneal@45 182
bgneal@45 183 return c;
bgneal@45 184 },
bgneal@45 185
bgneal@45 186 addClass : function(n, c, b) {
bgneal@45 187 var o = this.removeClass(n, c);
bgneal@45 188 n.className = b ? c + (o != '' ? (' ' + o) : '') : (o != '' ? (o + ' ') : '') + c;
bgneal@45 189 },
bgneal@45 190
bgneal@45 191 removeClass : function(n, c) {
bgneal@45 192 c = n.className.replace(new RegExp("(^|\\s+)" + c + "(\\s+|$)"), ' ');
bgneal@45 193 return n.className = c != ' ' ? c : '';
bgneal@45 194 },
bgneal@45 195
bgneal@45 196 tags : function(f, s) {
bgneal@45 197 return f.getElementsByTagName(s);
bgneal@45 198 },
bgneal@45 199
bgneal@45 200 mark : function(f, n) {
bgneal@45 201 var s = this.settings;
bgneal@45 202
bgneal@45 203 this.addClass(n, s.invalid_cls);
bgneal@45 204 this.markLabels(f, n, s.invalid_cls);
bgneal@45 205
bgneal@45 206 return false;
bgneal@45 207 },
bgneal@45 208
bgneal@45 209 markLabels : function(f, n, ic) {
bgneal@45 210 var nl, i;
bgneal@45 211
bgneal@45 212 nl = this.tags(f, "label");
bgneal@45 213 for (i=0; i<nl.length; i++) {
bgneal@45 214 if (nl[i].getAttribute("for") == n.id || nl[i].htmlFor == n.id)
bgneal@45 215 this.addClass(nl[i], ic);
bgneal@45 216 }
bgneal@45 217
bgneal@45 218 return null;
bgneal@45 219 }
bgneal@45 220 };