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