New test.
[mono.git] / mcs / class / System.Web / resources / WebUIValidation_2.0.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             for (var v in Page_ValidationSummaries) {
36                     var vs = Page_ValidationSummaries [v];
37                     if (vs.getAttribute ("validationgroup") == null)
38                             vs.setAttribute ("validationgroup", "");
39             }
40         }
41
42         for (var v in Page_Validators) {
43                 var vo = Page_Validators [v];
44                 var funcname = vo.getAttribute ("evaluationfunction");
45
46                 func = this[funcname];
47
48                 vo["evaluationfunction"] = func;
49
50                 if (vo.getAttribute ("isvalid") == null)
51                         vo.setAttribute ("isvalid", "true");
52
53                 if (vo.getAttribute ("enabled") == null)
54                         vo.setAttribute ("enabled", "true");
55
56                 if (vo.getAttribute ("validationgroup") == null)
57                         vo.setAttribute ("validationgroup", "");
58
59                 func = vo ["evaluationfunction"];
60         }
61
62         Page_ValidationActive = true;
63 }
64
65 var validation_result = true;
66
67 function ValidationSummaryOnSubmit (group)
68 {
69         /* handle validation summaries here */
70         if (validation_result == false && have_validation_summaries) {
71
72                 for (var vi in Page_ValidationSummaries)  {
73                         var vs = Page_ValidationSummaries[vi];
74                     
75                     if(IsValidationGroupMatch(vs, group)) {
76
77                             var header = vs.getAttribute ("headertext");
78                             if (header == null)
79                                     header = "";
80
81                             attr = vs.getAttribute ("showsummary");
82                             if (attr == null || attr.toLowerCase() == "true") {
83                                     var displaymode = vs.getAttribute ("displaymode");
84                                     if (displaymode == null) displaymode = "Bulleted";
85
86                                     var html = "";
87
88                                     if (displaymode == "List") {
89                                             list_pre = "";
90                                             list_post = "";
91                                             item_pre = "";
92                                             item_post = "<br>";
93                                     }
94                                     else if (displaymode == "SingleParagraph") {
95                                             list_pre = "";
96                                             list_post = "<br>";
97                                             item_pre = "";
98                                             item_post = " ";
99                                     }
100                                     else {
101                                             list_pre = "<ul>";
102                                             list_post = "</ul>";
103                                             item_pre = "\n<li>";
104                                             item_post = "</li>";
105                                     }
106
107                                     html += header;
108                                     html += list_pre;
109                                     for (var v in Page_Validators) {
110                                             var vo = Page_Validators [v];
111
112                                             if (vo.getAttribute ("isvalid").toLowerCase() == "false") {
113                                                     var text = ValidatorGetErrorMessage (vo);
114                                                     if (text != null && text != "") {
115                                                             html += item_pre + text + item_post;
116                                                     }
117                                             }
118                                     }
119                                     html += list_post;
120
121                                     vs.innerHTML = html;
122                                     vs.style.display = "block";
123                             }
124
125                             attr = vs.getAttribute ("showmessagebox");
126                             if (attr != null && attr.toLowerCase() == "true") {
127                                     var v_contents = "";
128
129                                     for (var v in Page_Validators) {
130                                             var vo = Page_Validators [v];
131
132                                             if (vo.getAttribute ("isvalid").toLowerCase() == "false") {
133                                                     var text = ValidatorGetErrorMessage (vo);
134                                                     if (text != null && text != "") {
135                                                             v_contents += "-" + text + "\n";
136                                                     }
137                                             }
138                                     }
139
140                                     var alert_header = header;
141                                     if (alert_header != "")
142                                             alert_header += "\n";
143                                     summary_contents = alert_header + v_contents;
144                                     alert (summary_contents);
145                             }
146                         }
147                 }
148         }
149 }
150
151 function ValidatorCommonOnSubmit ()
152 {
153         rv = validation_result;
154         validation_result = true;
155         return rv;
156 }
157
158 function ValidatorGetValue (controlname)
159 {
160         var el = GetElement (controlname);
161
162         /* if the element has a 'value' attribute, return it */
163         if (typeof (el.value) != 'undefined' && el.value != null) {
164                 return el.value;
165         }
166
167         /* if it's a select, loop over the options looking for the
168          * selected one. */
169         if (typeof (el.selectedIndex) != 'undefined') {
170                 return el.options[el.selectedIndex].value;
171         }
172 }
173
174 function ValidatorTrim (s)
175 {
176         s = s.replace (/^\s+/g, "");
177         s = s.replace (/\s+$/g, "");
178
179         return s;
180 }
181
182 function Page_ClientValidate(group)
183 {
184         validation_result = true;
185
186         /* clear out the existing text from all our summaries */
187         if (have_validation_summaries) {
188                 for (var vi in Page_ValidationSummaries) {
189                         var vs = Page_ValidationSummaries[vi];
190                         vs.style.display = "none";
191                         vs.innerHTML = "";
192                 }
193         }
194
195         for (var v in Page_Validators) {
196                 var vo = Page_Validators [v];
197                 var evalfunc = vo["evaluationfunction"];
198                 var result = false;
199
200                 if (vo.getAttribute ("enabled").toLowerCase() == "false" || !IsValidationGroupMatch(vo, group)) {
201                         result = true;
202                         ValidatorSucceeded (vo);
203                 }
204                 else {
205                         result = evalfunc (vo);
206                 }
207
208                 if (!result)
209                         validation_result = false;
210
211                 vo.setAttribute("isvalid", result ? "true" : "false");
212         }
213     ValidationSummaryOnSubmit(group);
214         return validation_result;
215 }
216
217 function IsValidationGroupMatch(vo, group) {
218     var valGroup = vo.getAttribute ("validationgroup");
219     if ((typeof(group) == "undefined") || (group == null)) {
220         return (valGroup == "");
221     }
222     return (valGroup == group);
223 }
224
225 /*******************/
226 /* type converters */
227
228 function ToInteger (s)
229 {
230         if ((v = parseInt(s, 10)) != s - 0)
231                 return null;
232         else
233                 return v;
234 }
235
236 function ToString (s)
237 {
238         return s;
239 }
240
241 function ToDouble (s)
242 {
243         if ((v = parseFloat(s)) != s - 0)
244                 return null;
245         else
246                 return v;
247 }
248
249 function ToDate (s)
250 {
251         /* NYI */
252         return null;
253 }
254
255 function ToCurrency (s)
256 {
257         /* NYI */
258         return null;
259 }
260
261 /*******************/
262 /* validators     */
263
264 function CompareValidatorEvaluateIsValid (validator)
265 {
266         var ControlToCompare = validator.getAttribute ("controltocompare");
267         var ValueToCompare = validator.getAttribute ("valuetocompare");
268         var Operator = validator.getAttribute ("operator").toLowerCase();
269         var ControlToValidate = validator.getAttribute ("controltovalidate");
270         var DataType = validator.getAttribute ("datatype");
271
272         var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
273         var compare = (ControlToCompare != null && ControlToCompare != "") ? ValidatorTrim (ValidatorGetValue (ControlToCompare)) : ValueToCompare;
274
275         var left = Convert (ctrl_value, DataType);
276         if (left == null) {
277                 ValidatorFailed (validator);
278                 return false;
279         }
280       
281         var right = Convert (compare, DataType);
282         if (right == null) {
283                 ValidatorSucceeded (validator);
284                  return true;
285         }
286
287         var result = false;
288    
289         if (Operator == "equal") {
290                 result = (left == right);
291         }
292         else if (Operator == "notequal") {
293                 result = (left != right);
294         }
295         else if (Operator == "lessthan") {
296                 result = (left < right);
297         }
298         else if (Operator == "lessthanequal") {
299                 result = (left <= right);
300         }
301         else if (Operator == "greaterthan") {
302                 result = (left > right);
303         }
304         else if (Operator == "greaterthanequal") {
305                 result = (left >= right);
306         }
307
308         if (result == false) {
309                 ValidatorFailed (validator);
310                 return false;
311         }
312         else {
313                 ValidatorSucceeded (validator);
314                 return true;
315         }
316 }
317
318 function RangeValidatorEvaluateIsValid (validator)
319 {
320         var MinimumValue = parseInt (validator.getAttribute ("minimumvalue"));
321         var MaximumValue = parseInt (validator.getAttribute ("maximumvalue"));
322         var ControlToValidate = validator.getAttribute ("controltovalidate");
323         var DataType = validator.getAttribute ("datatype");
324
325         var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
326
327         if (ctrl_value == "") {
328                 ValidatorSucceeded (validator);
329                 return true;
330         }
331
332         var val = Convert (ctrl_value, DataType);
333         if (val == null || val < MinimumValue || val > MaximumValue) {
334                 ValidatorFailed (validator);
335                 return false;
336         }
337         else {
338                 ValidatorSucceeded (validator);
339                 return true;
340         }
341 }
342
343 function RegularExpressionValidatorEvaluateIsValid (validator)
344 {
345         var ValidationExpression = validator.getAttribute ("validationexpression");
346         var ControlToValidate = validator.getAttribute ("controltovalidate");
347
348         var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
349
350         if (ctrl_value == "") {
351                 ValidatorSucceeded (validator);
352                 return true;
353         }
354
355         var r = new RegExp (ValidationExpression);
356         match = r.exec (ctrl_value);
357         if (match == null || match[0] == "") {
358                 ValidatorFailed (validator);
359                 return false;
360         }
361         else {
362                 ValidatorSucceeded (validator);
363                 return true;
364         }
365 }
366
367 function RequiredFieldValidatorEvaluateIsValid (validator)
368 {
369         var InitialValue = validator.getAttribute ("initialvalue");
370         var ControlToValidate = validator.getAttribute ("controltovalidate");
371
372         var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
373
374         if (ctrl_value == ValidatorTrim (InitialValue)) {
375                 ValidatorFailed (validator);
376                 return false;
377         }
378         else {
379                 ValidatorSucceeded (validator);
380                 return true;
381         }
382 }
383
384 function CustomValidatorEvaluateIsValid (validator)
385 {
386         var InitialValue = validator.getAttribute ("initialvalue");
387         var ControlToValidate = validator.getAttribute ("controltovalidate");
388
389         if (!ControlToValidate) {
390                 ValidatorSucceeded (validator);
391                 return true;
392         }
393
394         var evaluationfunc = validator.getAttribute ("clientvalidationfunction");
395
396         var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
397
398         var result = true;
399
400         if (evaluationfunc && evaluationfunc != "") {
401                 args = {Value:ctrl_value, IsValid:false};
402                 eval (evaluationfunc + "(validator, args)");
403                 result = args.IsValid;
404         }
405
406         if (result) {
407                 ValidatorSucceeded (validator);
408                 return true;
409         }
410         else {
411                 ValidatorFailed (validator);
412                 return false;
413         }
414 }
415
416 /*********************/
417 /* utility functions */
418
419 function Convert (s, ty)
420 {
421         var cvt = this ["To" + ty];
422         if (typeof (cvt) == 'function')
423                 return cvt (s);
424         else
425                 return null;
426 }
427
428 function ValidatorUpdateDisplay (v, valid)
429 {
430         var display = v.getAttribute ("display");
431
432         /* for validators that aren't displayed, do nothing */
433         if (display == "None") {
434                 return;
435         }
436
437         v.style.visibility = (valid ? "hidden" : "visible");
438         if (display == "Dynamic") {
439                 v.style.display = (valid ? "none" : "inline");
440         }
441 }
442
443 function ValidatorGetErrorMessage (v)
444 {
445         var text = v.getAttribute ("errormessage");
446         if (text == null || text == "")
447                 text = v.getAttribute ("text"); 
448         if (text == null)
449                 text = "";
450         return text;
451 }
452
453 function ValidatorGetText (v)
454 {
455         var text = v.getAttribute ("text");     
456         if (text == null || text == "")
457                 text = v.getAttribute ("errormessage");
458         if (text == null)
459                 text = "";
460         return text;
461 }
462
463 function ValidatorFailed (v)
464 {
465         var text = ValidatorGetText (v);
466         v.innerHTML = text;
467
468         ValidatorUpdateDisplay (v, false);
469 }
470
471 function ValidatorSucceeded (v)
472 {
473         v.innerHTML = "";
474
475         ValidatorUpdateDisplay (v, true);
476 }
477
478 function GetElement(id)
479 {
480         var x = document.getElementById ? document.getElementById (id) :
481                                           ((document.all) ? document.all [id] : null);
482         return x;
483 }