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