2007-02-04 Igor Zelmanovich <igorz@mainsoft.com>
[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 = 0; v < Page_ValidationSummaries.length; v++) {
36                     var vs = Page_ValidationSummaries [v];
37                     if (vs.getAttribute ("validationgroup") == null)
38                             vs.setAttribute ("validationgroup", "");
39             }
40         }
41
42         for (var v = 0; v < Page_Validators.length; v++) {
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 = 0; vi < Page_ValidationSummaries.length; vi++) {
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 = 0; v < Page_Validators.length; v++) {
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 = 0; v < Page_Validators.length; v++) {
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 = 0; vi < Page_ValidationSummaries.length; vi++) {
189                         var vs = Page_ValidationSummaries[vi];
190                         vs.style.display = "none";
191                         vs.innerHTML = "";
192                 }
193         }
194
195         for (var v = 0; v < Page_Validators.length; v++) {
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, validator)
229 {
230         if ((v = parseInt(s, 10)) != s - 0)
231                 return null;
232         else
233                 return v;
234 }
235
236 function ToString (s, validator)
237 {
238         return s;
239 }
240
241 function ToDouble (s, validator)
242 {
243         if ((v = parseFloat(s)) != s - 0)
244                 return null;
245         else
246                 return v;
247 }
248
249 function ToDate (s, validator)
250 {
251     var m, day, month, year;
252     var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\s*$");
253     m = s.match(yearFirstExp);
254     if (m != null && (m[2].length == 4 || validator.dateorder == "ymd")) {
255         day = m[6];
256         month = m[5];
257         year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10), validator.cutoffyear)
258     }
259     else {
260         if (validator.dateorder == "ymd") return null;
261         var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
262         m = s.match(yearLastExp);
263         if (m == null) return null;
264         if (validator.dateorder == "mdy") {
265             day = m[3];
266             month = m[1];
267         }
268         else {
269             day = m[1];
270             month = m[3];
271         }
272         year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10), validator.cutoffyear)
273     }
274     month -= 1;
275     var date = new Date(year, month, day);
276     return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
277 }
278
279 function ToCurrency (s, validator)
280 {
281         /* NYI */
282         return null;
283 }
284
285 function GetFullYear(year, maxYear)
286 {
287     var twoDigitMaxYear = maxYear % 100;
288     var centure = maxYear - twoDigitMaxYear;
289     return ((year > twoDigitMaxYear) ? (centure - 100 + year) : (centure + year));
290 }
291
292 /*******************/
293 /* validators     */
294
295 function CompareValidatorEvaluateIsValid (validator)
296 {
297         var ControlToCompare = validator.getAttribute ("controltocompare");
298         var ValueToCompare = validator.getAttribute ("valuetocompare");
299         var Operator = validator.getAttribute ("operator").toLowerCase();
300         var ControlToValidate = validator.getAttribute ("controltovalidate");
301         var DataType = validator.getAttribute ("datatype");
302
303         var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
304         var compare = (ControlToCompare != null && ControlToCompare != "") ? ValidatorTrim (ValidatorGetValue (ControlToCompare)) : ValueToCompare;
305
306         var left = Convert (ctrl_value, DataType, validator);
307         if (left == null) {
308                 ValidatorFailed (validator);
309                 return false;
310         }
311       
312         var right = compare != null ? Convert (compare, DataType, validator) : null;
313         if (right == null) {
314                 ValidatorSucceeded (validator);
315                  return true;
316         }
317
318         var result = false;
319    
320         if (Operator == "equal") {
321                 result = (left == right);
322         }
323         else if (Operator == "notequal") {
324                 result = (left != right);
325         }
326         else if (Operator == "lessthan") {
327                 result = (left < right);
328         }
329         else if (Operator == "lessthanequal") {
330                 result = (left <= right);
331         }
332         else if (Operator == "greaterthan") {
333                 result = (left > right);
334         }
335         else if (Operator == "greaterthanequal") {
336                 result = (left >= right);
337         }
338
339         if (result == false) {
340                 ValidatorFailed (validator);
341                 return false;
342         }
343         else {
344                 ValidatorSucceeded (validator);
345                 return true;
346         }
347 }
348
349 function RangeValidatorEvaluateIsValid (validator)
350 {
351         var MinimumValue = parseInt (validator.getAttribute ("minimumvalue"));
352         var MaximumValue = parseInt (validator.getAttribute ("maximumvalue"));
353         var ControlToValidate = validator.getAttribute ("controltovalidate");
354         var DataType = validator.getAttribute ("datatype");
355
356         var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
357
358         if (ctrl_value == "") {
359                 ValidatorSucceeded (validator);
360                 return true;
361         }
362
363         var val = Convert (ctrl_value, DataType, validator);
364         if (val == null || val < MinimumValue || val > MaximumValue) {
365                 ValidatorFailed (validator);
366                 return false;
367         }
368         else {
369                 ValidatorSucceeded (validator);
370                 return true;
371         }
372 }
373
374 function RegularExpressionValidatorEvaluateIsValid (validator)
375 {
376         var ValidationExpression = validator.getAttribute ("validationexpression");
377         var ControlToValidate = validator.getAttribute ("controltovalidate");
378
379         var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
380
381         if (ctrl_value == "") {
382                 ValidatorSucceeded (validator);
383                 return true;
384         }
385
386         var r = new RegExp (ValidationExpression);
387         match = r.exec (ctrl_value);
388         if (match == null || match[0] == "") {
389                 ValidatorFailed (validator);
390                 return false;
391         }
392         else {
393                 ValidatorSucceeded (validator);
394                 return true;
395         }
396 }
397
398 function RequiredFieldValidatorEvaluateIsValid (validator)
399 {
400         var InitialValue = validator.getAttribute ("initialvalue");
401         var ControlToValidate = validator.getAttribute ("controltovalidate");
402
403         var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
404
405         if (ctrl_value == ValidatorTrim (InitialValue)) {
406                 ValidatorFailed (validator);
407                 return false;
408         }
409         else {
410                 ValidatorSucceeded (validator);
411                 return true;
412         }
413 }
414
415 function CustomValidatorEvaluateIsValid (validator)
416 {
417         var InitialValue = validator.getAttribute ("initialvalue");
418         var ControlToValidate = validator.getAttribute ("controltovalidate");
419
420         if (!ControlToValidate) {
421                 ValidatorSucceeded (validator);
422                 return true;
423         }
424
425         var evaluationfunc = validator.getAttribute ("clientvalidationfunction");
426
427         var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
428         
429     if ((ctrl_value.length == 0) && ((typeof(validator.validateemptytext) != "string") || (validator.validateemptytext != "true"))) {
430                 ValidatorSucceeded (validator);
431                 return true;
432         }
433
434         var result = true;
435
436         if (evaluationfunc && evaluationfunc != "") {
437                 args = {Value:ctrl_value, IsValid:true};
438                 eval (evaluationfunc + "(validator, args)");
439                 result = args.IsValid;
440         }
441
442         if (result) {
443                 ValidatorSucceeded (validator);
444                 return true;
445         }
446         else {
447                 ValidatorFailed (validator);
448                 return false;
449         }
450 }
451
452 /*********************/
453 /* utility functions */
454
455 function Convert (s, ty, validator)
456 {
457         var cvt = this ["To" + ty];
458         if (typeof (cvt) == 'function')
459                 return cvt (s, validator);
460         else
461                 return null;
462 }
463
464 function ValidatorUpdateDisplay (v, valid)
465 {
466         var display = v.getAttribute ("display");
467
468         /* for validators that aren't displayed, do nothing */
469         if (display == "None") {
470                 return;
471         }
472
473         v.style.visibility = (valid ? "hidden" : "visible");
474         if (display == "Dynamic") {
475                 v.style.display = (valid ? "none" : "inline");
476         }
477 }
478
479 function ValidatorGetErrorMessage (v)
480 {
481         var text = v.getAttribute ("errormessage");
482         if (text == null || text == "")
483                 text = v.getAttribute ("text"); 
484         if (text == null)
485                 text = "";
486         return text;
487 }
488
489 function ValidatorGetText (v)
490 {
491         var text = v.getAttribute ("text");     
492         if (text == null || text == "")
493                 text = v.getAttribute ("errormessage");
494         if (text == null)
495                 text = "";
496         return text;
497 }
498
499 function ValidatorFailed (v)
500 {
501         var text = ValidatorGetText (v);
502         v.innerHTML = text;
503
504         ValidatorUpdateDisplay (v, false);
505 }
506
507 function ValidatorSucceeded (v)
508 {
509         v.innerHTML = "";
510
511         ValidatorUpdateDisplay (v, true);
512 }
513
514 function GetElement(id)
515 {
516         var x = document.getElementById ? document.getElementById (id) :
517                                           ((document.all) ? document.all [id] : null);
518         return x;
519 }