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