New test.
[mono.git] / mcs / class / System.Web / resources / WebUIValidation.js
1 /*
2  * WebUIValidation.js
3  *
4  * Authors:
5  *   Chris Toshok (toshok@ximian.com)
6  *
7  * (c) 2005 Novell, Inc. (http://www.novell.com)
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28
29 var have_validation_summaries = false;
30
31 function ValidatorOnLoad ()
32 {
33         if (typeof (Page_ValidationSummaries) != 'undefined' && Page_ValidationSummaries != null)
34                 have_validation_summaries = true;
35
36         for (var v in Page_Validators) {
37                 var vo = Page_Validators [v];
38                 var funcname = vo.getAttribute ("evaluationfunction");
39
40                 func = this[funcname];
41
42                 vo["evaluationfunction"] = func;
43
44                 if (vo.getAttribute ("isvalid") == null)
45                         vo.setAttribute ("isvalid", "true");
46
47                 if (vo.getAttribute ("enabled") == null)
48                         vo.setAttribute ("enabled", "true");
49
50                 func = vo ["evaluationfunction"];
51         }
52
53         Page_ValidationActive = true;
54 }
55
56 var validation_result = true;
57
58 function ValidatorCommonOnSubmit ()
59 {
60         /* handle validation summaries here */
61         if (validation_result == false && have_validation_summaries) {
62
63                 for (var vi in Page_ValidationSummaries)  {
64                         var vs = Page_ValidationSummaries[vi];
65
66                         var header = vs.getAttribute ("headertext");
67                         if (header == null)
68                                 header = "";
69
70                         attr = vs.getAttribute ("showsummary");
71                         if (attr == null || attr.toLowerCase() == "true") {
72                                 var displaymode = vs.getAttribute ("displaymode");
73                                 if (displaymode == null) displaymode = "Bulleted";
74
75                                 var html = "";
76
77                                 if (displaymode == "List") {
78                                         list_pre = "";
79                                         list_post = "";
80                                         item_pre = "";
81                                         item_post = "<br>";
82                                 }
83                                 else if (displaymode == "SingleParagraph") {
84                                         list_pre = "";
85                                         list_post = "<br>";
86                                         item_pre = "";
87                                         item_post = " ";
88                                 }
89                                 else {
90                                         list_pre = "<ul>";
91                                         list_post = "</ul>";
92                                         item_pre = "\n<li>";
93                                         item_post = "</li>";
94                                 }
95
96                                 html += header;
97                                 html += list_pre;
98                                 for (var v in Page_Validators) {
99                                         var vo = Page_Validators [v];
100
101                                         if (vo.getAttribute ("isvalid").toLowerCase() == "false") {
102                                                 var text = ValidatorGetErrorMessage (vo);
103                                                 if (text != null && text != "") {
104                                                         html += item_pre + text + item_post;
105                                                 }
106                                         }
107                                 }
108                                 html += list_post;
109
110                                 vs.innerHTML = html;
111                                 vs.style.display = "block";
112                         }
113
114                         attr = vs.getAttribute ("showmessagebox");
115                         if (attr != null && attr.toLowerCase() == "true") {
116                                 var v_contents = "";
117
118                                 for (var v in Page_Validators) {
119                                         var vo = Page_Validators [v];
120
121                                         if (vo.getAttribute ("isvalid").toLowerCase() == "false") {
122                                                 var text = ValidatorGetErrorMessage (vo);
123                                                 if (text != null && text != "") {
124                                                         v_contents += "-" + text + "\n";
125                                                 }
126                                         }
127                                 }
128
129                                 var alert_header = header;
130                                 if (alert_header != "")
131                                         alert_header += "\n";
132                                 summary_contents = alert_header + v_contents;
133                                 alert (summary_contents);
134                         }
135                 }
136         }
137
138         rv = validation_result;
139         validation_result = true;
140         return rv;
141 }
142
143 function ValidatorGetValue (controlname)
144 {
145         var el = GetElement (controlname);
146
147         /* if the element has a 'value' attribute, return it */
148         if (typeof (el.value) != 'undefined' && el.value != null) {
149                 return el.value;
150         }
151
152         /* if it's a select, loop over the options looking for the
153          * selected one. */
154         if (typeof (el.selectedIndex) != 'undefined') {
155                 return el.options[el.selectedIndex].value;
156         }
157 }
158
159 function ValidatorTrim (s)
160 {
161         s = s.replace (/^\s+/g, "");
162         s = s.replace (/\s+$/g, "");
163
164         return s;
165 }
166
167 function Page_ClientValidate()
168 {
169         validation_result = true;
170
171         /* clear out the existing text from all our summaries */
172         if (have_validation_summaries) {
173                 for (var vi in Page_ValidationSummaries) {
174                         var vs = Page_ValidationSummaries[vi];
175                         vs.style.display = "none";
176                         vs.innerHTML = "";
177                 }
178         }
179
180         for (var v in Page_Validators) {
181                 var vo = Page_Validators [v];
182                 var evalfunc = vo["evaluationfunction"];
183                 var result = false;
184
185                 if (vo.getAttribute ("enabled").toLowerCase() == "false") {
186                         result = true;
187                         ValidatorSucceeded (vo);
188                 }
189                 else {
190                         result = evalfunc (vo);
191                 }
192
193                 if (!result)
194                         validation_result = false;
195
196                 vo.setAttribute("isvalid", result ? "true" : "false");
197         }
198
199         return validation_result;
200 }
201
202 /*******************/
203 /* type converters */
204
205 function ToInteger (s)
206 {
207         if ((v = parseInt(s, 10)) != s - 0)
208                 return null;
209         else
210                 return v;
211 }
212
213 function ToString (s)
214 {
215         return s;
216 }
217
218 function ToDouble (s)
219 {
220         if ((v = parseFloat(s)) != s - 0)
221                 return null;
222         else
223                 return v;
224 }
225
226 function ToDate (s)
227 {
228         /* NYI */
229         return null;
230 }
231
232 function ToCurrency (s)
233 {
234         /* NYI */
235         return null;
236 }
237
238 /*******************/
239 /* validators     */
240
241 function CompareValidatorEvaluateIsValid (validator)
242 {
243         var ControlToCompare = validator.getAttribute ("controltocompare");
244         var ValueToCompare = validator.getAttribute ("valuetocompare");
245         var Operator = validator.getAttribute ("operator").toLowerCase();
246         var ControlToValidate = validator.getAttribute ("controltovalidate");
247         var DataType = validator.getAttribute ("datatype");
248
249         var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
250         var compare = (ControlToCompare != null && ControlToCompare != "") ? ValidatorTrim (ValidatorGetValue (ControlToCompare)) : ValueToCompare;
251
252         var left = Convert (ctrl_value, DataType);
253         if (left == null) {
254                 ValidatorFailed (validator);
255                 return false;
256         }
257       
258         var right = Convert (compare, DataType);
259         if (right == null) {
260                 ValidatorSucceeded (validator);
261                  return true;
262         }
263
264         var result = false;
265    
266         if (Operator == "equal") {
267                 result = (left == right);
268         }
269         else if (Operator == "notequal") {
270                 result = (left != right);
271         }
272         else if (Operator == "lessthan") {
273                 result = (left < right);
274         }
275         else if (Operator == "lessthanequal") {
276                 result = (left <= right);
277         }
278         else if (Operator == "greaterthan") {
279                 result = (left > right);
280         }
281         else if (Operator == "greaterthanequal") {
282                 result = (left >= right);
283         }
284
285         if (result == false) {
286                 ValidatorFailed (validator);
287                 return false;
288         }
289         else {
290                 ValidatorSucceeded (validator);
291                 return true;
292         }
293 }
294
295 function RangeValidatorEvaluateIsValid (validator)
296 {
297         var MinimumValue = parseInt (validator.getAttribute ("minimumvalue"));
298         var MaximumValue = parseInt (validator.getAttribute ("maximumvalue"));
299         var ControlToValidate = validator.getAttribute ("controltovalidate");
300         var DataType = validator.getAttribute ("datatype");
301
302         var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
303
304         if (ctrl_value == "") {
305                 ValidatorSucceeded (validator);
306                 return true;
307         }
308
309         var val = Convert (ctrl_value, DataType);
310         if (val == null || val < MinimumValue || val > MaximumValue) {
311                 ValidatorFailed (validator);
312                 return false;
313         }
314         else {
315                 ValidatorSucceeded (validator);
316                 return true;
317         }
318 }
319
320 function RegularExpressionValidatorEvaluateIsValid (validator)
321 {
322         var ValidationExpression = validator.getAttribute ("validationexpression");
323         var ControlToValidate = validator.getAttribute ("controltovalidate");
324
325         var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
326
327         if (ctrl_value == "") {
328                 ValidatorSucceeded (validator);
329                 return true;
330         }
331
332         var r = new RegExp (ValidationExpression);
333         match = r.exec (ctrl_value);
334         if (match == null || match[0] == "") {
335                 ValidatorFailed (validator);
336                 return false;
337         }
338         else {
339                 ValidatorSucceeded (validator);
340                 return true;
341         }
342 }
343
344 function RequiredFieldValidatorEvaluateIsValid (validator)
345 {
346         var InitialValue = validator.getAttribute ("initialvalue");
347         var ControlToValidate = validator.getAttribute ("controltovalidate");
348
349         var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
350
351         if (ctrl_value == ValidatorTrim (InitialValue)) {
352                 ValidatorFailed (validator);
353                 return false;
354         }
355         else {
356                 ValidatorSucceeded (validator);
357                 return true;
358         }
359 }
360
361 function CustomValidatorEvaluateIsValid (validator)
362 {
363         var InitialValue = validator.getAttribute ("initialvalue");
364         var ControlToValidate = validator.getAttribute ("controltovalidate");
365
366         if (!ControlToValidate) {
367                 ValidatorSucceeded (validator);
368                 return true;
369         }
370
371         var evaluationfunc = validator.getAttribute ("clientvalidationfunction");
372
373         var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
374
375         var result = true;
376
377         if (evaluationfunc && evaluationfunc != "") {
378                 args = {Value:ctrl_value, IsValid:false};
379                 eval (evaluationfunc + "(validator, args)");
380                 result = args.IsValid;
381         }
382
383         if (result) {
384                 ValidatorSucceeded (validator);
385                 return true;
386         }
387         else {
388                 ValidatorFailed (validator);
389                 return false;
390         }
391 }
392
393 /*********************/
394 /* utility functions */
395
396 function Convert (s, ty)
397 {
398         var cvt = this ["To" + ty];
399         if (typeof (cvt) == 'function')
400                 return cvt (s);
401         else
402                 return null;
403 }
404
405 function ValidatorUpdateDisplay (v, valid)
406 {
407         var display = v.getAttribute ("display");
408
409         /* for validators that aren't displayed, do nothing */
410         if (display == "None") {
411                 return;
412         }
413
414         v.style.visibility = (valid ? "hidden" : "visible");
415         if (display == "Dynamic") {
416                 v.style.display = (valid ? "none" : "inline");
417         }
418 }
419
420 function ValidatorGetErrorMessage (v)
421 {
422         var text = v.getAttribute ("errormessage");
423         if (text == null || text == "")
424                 text = v.getAttribute ("text"); 
425         if (text == null)
426                 text = "";
427         return text;
428 }
429
430 function ValidatorGetText (v)
431 {
432         var text = v.getAttribute ("text");     
433         if (text == null || text == "")
434                 text = v.getAttribute ("errormessage");
435         if (text == null)
436                 text = "";
437         return text;
438 }
439
440 function ValidatorFailed (v)
441 {
442         var text = ValidatorGetText (v);
443         v.innerHTML = text;
444
445         ValidatorUpdateDisplay (v, false);
446 }
447
448 function ValidatorSucceeded (v)
449 {
450         v.innerHTML = "";
451
452         ValidatorUpdateDisplay (v, true);
453 }
454
455 function GetElement(id)
456 {
457         var x = document.getElementById ? document.getElementById (id) :
458                                           ((document.all) ? document.all [id] : null);
459         return x;
460 }