Merge pull request #2045 from BillSeurer/timezone
[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-2009 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 function WebFormValidation_Initialize(webForm) {
30
31 webForm.have_validation_summaries = false;
32
33 webForm.HaveRegexp = function ()
34 {
35   if (window.RegExp)
36     return true;
37   return false;
38 }
39
40 webForm.ValidatorOnLoad = function  ()
41 {
42         if (typeof (webForm.Page_ValidationSummaries) != 'undefined' && webForm.Page_ValidationSummaries != null) {
43                 webForm.have_validation_summaries = true;
44         }
45
46         if (typeof (webForm.Page_Validators) != 'undefined' || webForm.Page_Validators != null) {
47                 for (var v = 0; v < webForm.Page_Validators.length; v++) {
48                         var vo = webForm.Page_Validators [v];
49
50                         if (typeof(vo.isvalid) == "string" && vo.isvalid == "False")
51                                 vo._isvalid = false;
52                         else
53                                 vo._isvalid = true;
54
55                         if (typeof(vo.enabled) == "string" && vo.enabled == "False")
56                                 vo._enabled = false;
57                         else
58                                 vo._enabled = true;
59                         
60                         if (typeof(vo.evaluationfunction) == "string")
61                                 vo.evaluationfunction = webForm [vo.evaluationfunction];
62                 }
63         }
64         
65         webForm.Page_ValidationActive = true;
66 }
67
68 webForm.validation_result = true;
69
70 webForm.ValidationSummaryOnSubmit = function (group)
71 {
72         /* handle validation summaries here */
73         if (webForm.validation_result == false && webForm.have_validation_summaries) {
74
75           for (var vi = 0; vi < webForm.Page_ValidationSummaries.length; vi++) {
76                         var vs = webForm.Page_ValidationSummaries[vi];
77                     
78                     if(webForm.IsValidationGroupMatch(vs, group)) {
79
80                             var header = "";
81                             if(typeof(vs.headertext)=="string")
82                                     header = vs.headertext;
83
84                             if (vs.showsummary != "False") {
85                                         if (typeof(vs.displaymode) != "string")
86                                                 vs.displaymode = "BulletList";
87
88                                     var html = "";
89
90                                     if (vs.displaymode == "List") {
91                                             list_pre = "";
92                                             list_post = "";
93                                             item_pre = "";
94                                             item_post = "<br>";
95                                     }
96                                     else if (vs.displaymode == "SingleParagraph") {
97                                             list_pre = "";
98                                             list_post = "<br>";
99                                             item_pre = "";
100                                             item_post = " ";
101                                     }
102                                     else {
103                                             list_pre = "<ul>";
104                                             list_post = "</ul>";
105                                             item_pre = "\n<li>";
106                                             item_post = "</li>";
107                                     }
108
109                                     html += header;
110                                     html += list_pre;
111                                                 for (var v = 0; v < webForm.Page_Validators.length; v++) {
112                                       var vo = webForm.Page_Validators [v];
113
114                                             if (!vo._isvalid) {
115                                                     var text = vo.errormessage;
116                                                     if (text != null && text != "") {
117                                                             html += item_pre + text + item_post;
118                                                     }
119                                             }
120                                     }
121                                     html += list_post;
122
123                                     vs.innerHTML = html;
124                                     vs.style.display = "block";
125                             }
126
127                             if (vs.showmessagebox == "True") {
128                                     var v_contents = "";
129
130                                                 for (var v = 0; v < webForm.Page_Validators.length; v++) {
131                                       var vo = webForm.Page_Validators [v];
132
133                                             if (!vo._isvalid) {
134                                                     var text = vo.errormessage;
135                                                     if (text != null && text != "") {
136                                                             v_contents += "-" + text + "\n";
137                                                     }
138                                             }
139                                     }
140
141                                     var alert_header = header;
142                                     if (alert_header != "")
143                                             alert_header += "\n";
144                                     summary_contents = alert_header + v_contents;
145                                     alert (summary_contents);
146                             }
147                         }
148                 }
149         }
150 }
151
152 webForm.ValidatorCommonOnSubmit = function ()
153 {
154         var rv = webForm.validation_result;
155         webForm.validation_result = true;
156         return rv;
157 }
158
159 webForm.ValidatorGetValue = function (controlname)
160 {
161         var el = webForm.GetElement (controlname);
162         if (el == null)
163                 return null;
164
165         /* if the element has a 'value' attribute, return it */
166         if (typeof (el.value) != 'undefined' && el.value != null) {
167                 return el.value;
168         }
169
170         /* if it's a select, loop over the options looking for the
171          * selected one. */
172         if (typeof (el.selectedIndex) != 'undefined') {
173                 return el.options[el.selectedIndex].value;
174         }
175
176         return webForm.ValidatorGetValueRecursive(el);
177 }
178
179 webForm.ValidatorGetValueRecursive = function (el)
180 {
181         if (typeof(el.value) == "string") {
182                 if (el.type != "radio" || el.checked == true) return el.value;
183         }
184         for (var i = 0; i<el.childNodes.length; i++) {
185                 var val = webForm.ValidatorGetValueRecursive(el.childNodes[i]);
186                 if (val != "") return val;
187         }
188         return "";
189 }
190
191 webForm.ValidatorTrim = function (s)
192 {
193         if (s == null)
194                return null;
195
196         s = s.replace (/^\s+/g, "");
197         s = s.replace (/\s+$/g, "");
198
199         return s;
200 }
201
202 webForm.Page_ClientValidate = function (group)
203 {
204         webForm.validation_result = true;
205
206         /* clear out the existing text from all our summaries */
207         if (webForm.have_validation_summaries) {
208           for (var vi = 0; vi < webForm.Page_ValidationSummaries.length; vi++) {
209                         var vs = webForm.Page_ValidationSummaries[vi];
210                         vs.style.display = "none";
211                         vs.innerHTML = "";
212                 }
213         }
214         
215         var invalidControlHasBeenFocused = false;
216         if (typeof (webForm.Page_Validators) != 'undefined' || webForm.Page_Validators != null) {
217                 for (var v = 0; v < webForm.Page_Validators.length; v++) {
218                         var vo = webForm.Page_Validators [v];
219                         var evalfunc = vo.evaluationfunction;
220                         var result = false;
221                         var el = webForm.GetElement (vo.controltovalidate);
222
223                         if (el == null) {
224                                 result = true;
225                                 webForm.ValidatorSucceeded (vo);
226                         } else if (!vo._enabled || !webForm.IsValidationGroupMatch(vo, group)) {
227                                 result = true;
228                                 webForm.ValidatorSucceeded (vo);
229                         } else {
230                                 result = evalfunc.call (this, vo);
231                         }
232
233                         if (!result) {
234                                 webForm.validation_result = false;
235                                 if (!invalidControlHasBeenFocused && typeof(vo.focusOnError) == "string" && vo.focusOnError == "t") {
236                                         invalidControlHasBeenFocused = webForm.ValidatorSetFocus(vo);
237                                 }
238                         }
239                 
240                         vo._isvalid = result;
241                 }
242                 webForm.ValidationSummaryOnSubmit(group);
243         }
244         
245         return webForm.validation_result;
246 }
247
248 webForm.IsValidationGroupMatch = function (vo, group) {
249     var valGroup = "";
250     if (typeof(vo.validationGroup) == "string")
251                 valGroup = vo.validationGroup;
252     if ((typeof(group) == "undefined") || (group == null)) {
253         return (valGroup == "");
254     }
255     return (valGroup == group);
256 }
257
258 webForm.ValidatorSetFocus = function (val) {
259     var ctrl = webForm.GetElement(val.controltovalidate);
260         if ((typeof(ctrl) != "undefined") && (ctrl != null) &&
261                 ((ctrl.tagName.toLowerCase() != "input") || (ctrl.type.toLowerCase() != "hidden")) &&
262                 (typeof(ctrl.disabled) == "undefined" || ctrl.disabled == null || ctrl.disabled == false) &&
263                 (typeof(ctrl.visible) == "undefined" || ctrl.visible == null || ctrl.visible != false) &&
264                 (webForm.IsInVisibleContainer(ctrl))) {
265                 if (ctrl.tagName.toLowerCase() == "table") {
266                         var inputElements = ctrl.getElementsByTagName("input");
267                         var lastInputElement  = inputElements[inputElements.length -1];
268                         if (lastInputElement != null) {
269                                 ctrl = lastInputElement;
270                         }
271                 }
272                 if (typeof(ctrl.focus) != "undefined" && ctrl.focus != null) {
273                         ctrl.focus();
274                         return true;
275                 }
276     }
277     return false;
278 }
279
280 webForm.IsInVisibleContainer = function (ctrl) {
281         if (typeof(ctrl.style) != "undefined" && 
282                 ((typeof(ctrl.style.display) != "undefined" &&  ctrl.style.display == "none") ||
283                 (typeof(ctrl.style.visibility) != "undefined" && ctrl.style.visibility == "hidden"))) {
284                 return false;
285         }
286         else if (typeof(ctrl.parentNode) != "undefined" && ctrl.parentNode != null && ctrl.parentNode != ctrl) {
287                 return webForm.IsInVisibleContainer(ctrl.parentNode);
288         }
289         return true;
290 }
291
292 /*******************/
293 /* type converters */
294
295 webForm.ToInteger = function  (s, validator)
296 {
297         if (s.match(/^\s*[-\+]?\d+\s*$/) == null)
298                 return null;
299         var v = parseInt(s, 10);
300         return (isNaN(v) ? null : v);
301 }
302
303 webForm.ToString = function (s, validator)
304 {
305         return s;
306 }
307
308 webForm.ToDouble = function (s, validator)
309 {
310         if ((v = parseFloat(s)) != s - 0)
311                 return null;
312         else
313                 return v;
314 }
315
316 webForm.ToDate = function (s, validator)
317 {
318     if (!webForm.HaveRegexp ())
319         return null;
320     var m, day, month, year;
321     var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\s*$");
322     m = s.match(yearFirstExp);
323     if (m != null && (m[2].length == 4 || validator.dateorder == "ymd")) {
324         day = m[6];
325         month = m[5];
326         year = (m[2].length == 4) ? m[2] : webForm.GetFullYear(parseInt(m[3], 10), validator.cutoffyear)
327     }
328     else {
329         if (validator.dateorder == "ymd") return null;
330         var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
331         m = s.match(yearLastExp);
332         if (m == null) return null;
333         if (validator.dateorder == "mdy") {
334             day = m[3];
335             month = m[1];
336         }
337         else {
338             day = m[1];
339             month = m[3];
340         }
341         year = (m[5].length == 4) ? m[5] : webForm.GetFullYear(parseInt(m[6], 10), validator.cutoffyear)
342     }
343     month -= 1;
344     var date = new Date(year, month, day);
345     return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
346 }
347
348 webForm.ToCurrency = function (s, validator)
349 {
350   if (!webForm.HaveRegexp ())
351     return null;
352   
353         var hasDigits = (validator.digits > 0);
354         var beginGroupSize, subsequentGroupSize;
355         var groupSizeNum = parseInt(validator.groupsize, 10);
356         if (!isNaN(groupSizeNum) && groupSizeNum > 0) {
357                 beginGroupSize = "{1," + groupSizeNum + "}";
358                 subsequentGroupSize = "{" + groupSizeNum + "}";
359         }
360         else {
361                 beginGroupSize = subsequentGroupSize = "+";
362         }
363         var exp = new RegExp("^\\s*([-\\+])?((\\d" + beginGroupSize + "(\\" + validator.groupchar + "\\d" + subsequentGroupSize + ")+)|\\d*)"
364                                         + (hasDigits ? "\\" + validator.decimalchar + "?(\\d{0," + validator.digits + "})" : "")
365                                         + "\\s*$");
366         var m = s.match(exp);
367         if (m == null)
368                 return null;
369         if (m[2].length == 0 && hasDigits && m[5].length == 0)
370                 return null;
371         var cleanInput = (m[1] != null ? m[1] : "") + m[2].replace(new RegExp("(\\" + validator.groupchar + ")", "g"), "") + ((hasDigits && m[5].length > 0) ? "." + m[5] : "");
372         var num = parseFloat(cleanInput);
373         return (isNaN(num) ? null : num);
374 }
375
376 webForm.GetFullYear = function (year, maxYear)
377 {
378     var twoDigitMaxYear = maxYear % 100;
379     var centure = maxYear - twoDigitMaxYear;
380     return ((year > twoDigitMaxYear) ? (centure - 100 + year) : (centure + year));
381 }
382
383 /*******************/
384 /* validators     */
385
386 webForm.CompareValidatorEvaluateIsValid = function (validator)
387 {
388         var Operator = validator.operator.toLowerCase();
389         var ControlToValidate = validator.controltovalidate;
390         var DataType = validator.type;
391
392         var ctrl_value = webForm.ValidatorGetValue (ControlToValidate);
393         if (webForm.ValidatorTrim (ctrl_value) == "") {
394                 webForm.ValidatorSucceeded (validator);
395                 return true;
396         }
397         
398         var left = webForm.Convert (ctrl_value, DataType, validator);
399         if (left == null) {
400                 webForm.ValidatorFailed (validator);
401                 return false;
402         }
403         
404         if (Operator == "datatypecheck") {
405                 webForm.ValidatorSucceeded (validator);
406                 return true;
407         }
408       
409         var compare = "";
410         if (typeof(validator.controltocompare) == "string" && document.getElementById(validator.controltocompare))
411                 compare = webForm.ValidatorGetValue(validator.controltocompare);
412         else if (typeof(validator.valuetocompare) == "string")
413                 compare = validator.valuetocompare;
414
415         var right = compare != null ? webForm.Convert (compare, DataType, validator) : null;
416         if (right == null) {
417                 webForm.ValidatorSucceeded (validator);
418                  return true;
419         }
420
421         var result = false;
422    
423         if (Operator == "equal") {
424                 result = (left == right);
425         }
426         else if (Operator == "notequal") {
427                 result = (left != right);
428         }
429         else if (Operator == "lessthan") {
430                 result = (left < right);
431         }
432         else if (Operator == "lessthanequal") {
433                 result = (left <= right);
434         }
435         else if (Operator == "greaterthan") {
436                 result = (left > right);
437         }
438         else if (Operator == "greaterthanequal") {
439                 result = (left >= right);
440         }
441
442         if (result == false) {
443                 webForm.ValidatorFailed (validator);
444                 return false;
445         }
446         else {
447                 webForm.ValidatorSucceeded (validator);
448                 return true;
449         }
450 }
451
452 webForm.RangeValidatorEvaluateIsValid = function (validator)
453 {
454         var ControlToValidate = validator.controltovalidate;
455         var DataType = validator.type;
456
457         var ctrl_value = webForm.ValidatorTrim (webForm.ValidatorGetValue (ControlToValidate));
458
459         if (ctrl_value == "") {
460                 webForm.ValidatorSucceeded (validator);
461                 return true;
462         }
463
464         var MinimumValue = webForm.Convert (validator.minimumvalue, DataType, validator);
465         var MaximumValue = webForm.Convert (validator.maximumvalue, DataType, validator);
466         var val = webForm.Convert (ctrl_value, DataType, validator);
467         if (val == null || val < MinimumValue || val > MaximumValue) {
468                 webForm.ValidatorFailed (validator);
469                 return false;
470         }
471         else {
472                 webForm.ValidatorSucceeded (validator);
473                 return true;
474         }
475 }
476
477 webForm.RegularExpressionValidatorEvaluateIsValid = function (validator)
478 {
479         var ValidationExpression = validator.validationexpression;
480         var ControlToValidate = validator.controltovalidate;
481
482         var ctrl_value = webForm.ValidatorTrim (webForm.ValidatorGetValue (ControlToValidate));
483
484         if (ctrl_value == "") {
485                 webForm.ValidatorSucceeded (validator);
486                 return true;
487         }
488
489   if (!webForm.HaveRegexp ())
490     return false;
491   
492         var r = new RegExp (ValidationExpression);
493         match = r.exec (ctrl_value);
494         if (match == null || match[0] != ctrl_value) {
495                 webForm.ValidatorFailed (validator);
496                 return false;
497         }
498         else {
499                 webForm.ValidatorSucceeded (validator);
500                 return true;
501         }
502 }
503
504 webForm.RequiredFieldValidatorEvaluateIsValid = function (validator)
505 {
506         var InitialValue = validator.initialvalue;
507         var ControlToValidate = validator.controltovalidate;
508
509         var ctrl_value = webForm.ValidatorTrim (webForm.ValidatorGetValue (ControlToValidate));
510         
511         if (ctrl_value == webForm.ValidatorTrim (InitialValue)) {
512                 webForm.ValidatorFailed (validator);
513                 return false;
514         }
515         else {
516                 webForm.ValidatorSucceeded (validator);
517                 return true;
518         }
519 }
520
521 webForm.CustomValidatorEvaluateIsValid = function (validator)
522 {
523         var ControlToValidate = validator.controltovalidate;
524         var evaluationfunc = validator.clientvalidationfunction;
525         var ctrl_value;
526
527         if (ControlToValidate) {
528             ctrl_value = webForm.ValidatorTrim (webForm.ValidatorGetValue (ControlToValidate));
529
530             if ((ctrl_value.length == 0) && ((typeof(validator.validateemptytext) != "string") || (validator.validateemptytext != "true"))) {
531                 webForm.ValidatorSucceeded (validator);
532                 return true;
533             }
534         } else
535             ctrl_value = "";
536         var result = true;
537
538         if (evaluationfunc && evaluationfunc != "") {
539                 args = {Value:ctrl_value, IsValid:true};
540                 eval (evaluationfunc + "(validator, args)");
541                 result = args.IsValid;
542         }
543
544         if (result) {
545                 webForm.ValidatorSucceeded (validator);
546                 return true;
547         }
548         else {
549                 webForm.ValidatorFailed (validator);
550                 return false;
551         }
552 }
553
554 /*********************/
555 /* utility functions */
556
557 webForm.Convert = function (s, ty, validator)
558 {
559         var cvt = this ["To" + ty];
560         if (typeof (cvt) == 'function')
561                 return cvt.call (this, s, validator);
562         else
563                 return null;
564 }
565
566 webForm.ValidatorUpdateDisplay = function (v, valid)
567 {
568         var display = v.display;
569
570         /* for validators that aren't displayed, do nothing */
571         if (display == "None") {
572                 return;
573         }
574
575         v.style.visibility = (valid ? "hidden" : "visible");
576         if (display == "Dynamic") {
577                 v.style.display = (valid ? "none" : "inline");
578         }
579 }
580
581 webForm.ValidatorFailed = function  (v)
582 {
583         webForm.ValidatorUpdateDisplay (v, false);
584 }
585
586 webForm.ValidatorSucceeded = function  (v)
587 {
588         webForm.ValidatorUpdateDisplay (v, true);
589 }
590
591 webForm.GetElement = function (id)
592 {
593         var x = document.getElementById ? document.getElementById (id) :
594                                           ((document.all) ? document.all [id] : null);
595         return x;
596 }
597
598
599 }