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