5 * Chris Toshok (toshok@ximian.com)
7 * (c) 2005 Novell, Inc. (http://www.novell.com)
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:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
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.
29 var have_validation_summaries = false;
31 function HaveRegexp ()
38 function ValidatorOnLoad ()
40 if (typeof (Page_ValidationSummaries) != 'undefined' && Page_ValidationSummaries != null)
41 have_validation_summaries = true;
43 for (var v = 0; v < Page_Validators.length; v++) {
44 var vo = Page_Validators [v];
45 var funcname = vo.getAttribute ("evaluationfunction");
47 func = this[funcname];
49 vo["evaluationfunction"] = func;
51 if (vo.getAttribute ("isvalid") == null)
52 vo.setAttribute ("isvalid", "true");
54 if (vo.getAttribute ("enabled") == null)
55 vo.setAttribute ("enabled", "true");
57 func = vo ["evaluationfunction"];
60 Page_ValidationActive = true;
63 var validation_result = true;
65 function ValidatorCommonOnSubmit ()
67 /* handle validation summaries here */
68 if (validation_result == false && have_validation_summaries) {
70 for (var vi in Page_ValidationSummaries) {
71 var vs = Page_ValidationSummaries[vi];
73 var header = vs.getAttribute ("headertext");
77 attr = vs.getAttribute ("showsummary");
78 if (attr == null || attr.toLowerCase() == "true") {
79 var displaymode = vs.getAttribute ("displaymode");
80 if (displaymode == null) displaymode = "Bulleted";
84 if (displaymode == "List") {
90 else if (displaymode == "SingleParagraph") {
105 for (var v = 0; v < Page_Validators.length; v++) {
106 var vo = Page_Validators [v];
108 if (vo.getAttribute ("isvalid").toLowerCase() == "false") {
109 var text = ValidatorGetErrorMessage (vo);
110 if (text != null && text != "") {
111 html += item_pre + text + item_post;
118 vs.style.display = "block";
121 attr = vs.getAttribute ("showmessagebox");
122 if (attr != null && attr.toLowerCase() == "true") {
125 for (var v = 0; v < Page_Validators.length; v++) {
126 var vo = Page_Validators [v];
128 if (vo.getAttribute ("isvalid").toLowerCase() == "false") {
129 var text = ValidatorGetErrorMessage (vo);
130 if (text != null && text != "") {
131 v_contents += "-" + text + "\n";
136 var alert_header = header;
137 if (alert_header != "")
138 alert_header += "\n";
139 summary_contents = alert_header + v_contents;
140 alert (summary_contents);
145 rv = validation_result;
146 validation_result = true;
150 function ValidatorGetValue (controlname)
152 var el = GetElement (controlname);
154 /* if the element has a 'value' attribute, return it */
155 if (typeof (el.value) != 'undefined' && el.value != null) {
159 /* if it's a select, loop over the options looking for the
161 if (typeof (el.selectedIndex) != 'undefined') {
162 return el.options[el.selectedIndex].value;
166 function ValidatorTrim (s)
168 s = s.replace (/^\s+/g, "");
169 s = s.replace (/\s+$/g, "");
174 function Page_ClientValidate()
176 validation_result = true;
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";
187 for (var v = 0; v < Page_Validators.length; v++) {
188 var vo = Page_Validators [v];
189 var evalfunc = vo["evaluationfunction"];
192 if (vo.getAttribute ("enabled").toLowerCase() == "false") {
194 ValidatorSucceeded (vo);
197 result = evalfunc (vo);
201 validation_result = false;
203 vo.setAttribute("isvalid", result ? "true" : "false");
206 return validation_result;
209 /*******************/
210 /* type converters */
212 function ToInteger (s, validator)
214 if ((v = parseInt(s, 10)) != s - 0)
220 function ToString (s, validator)
225 function ToDouble (s, validator)
227 if ((v = parseFloat(s)) != s - 0)
233 function ToDate (s, validator)
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")) {
243 year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10), validator.cutoffyear)
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") {
258 year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10), validator.cutoffyear)
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;
265 function ToCurrency (s)
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 + "}";
277 beginGroupSize = subsequentGroupSize = "+";
279 var exp = new RegExp("^\\s*([-\\+])?((\\d" + beginGroupSize + "(\\" + validator.groupchar + "\\d" + subsequentGroupSize + ")+)|\\d*)"
280 + (hasDigits ? "\\" + validator.decimalchar + "?(\\d{0," + validator.digits + "})" : "")
282 var m = s.match(exp);
285 if (m[2].length == 0 && hasDigits && m[5].length == 0)
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);
292 function GetFullYear(year, maxYear)
294 var twoDigitMaxYear = maxYear % 100;
295 var centure = maxYear - twoDigitMaxYear;
296 return ((year > twoDigitMaxYear) ? (centure - 100 + year) : (centure + year));
299 /*******************/
302 function CompareValidatorEvaluateIsValid (validator)
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");
310 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
311 var compare = (ControlToCompare != null && ControlToCompare != "") ? ValidatorTrim (ValidatorGetValue (ControlToCompare)) : ValueToCompare;
313 var left = Convert (ctrl_value, DataType, validator);
315 ValidatorFailed (validator);
319 var right = Convert (compare, DataType, validator);
321 ValidatorSucceeded (validator);
327 if (Operator == "equal") {
328 result = (left == right);
330 else if (Operator == "notequal") {
331 result = (left != right);
333 else if (Operator == "lessthan") {
334 result = (left < right);
336 else if (Operator == "lessthanequal") {
337 result = (left <= right);
339 else if (Operator == "greaterthan") {
340 result = (left > right);
342 else if (Operator == "greaterthanequal") {
343 result = (left >= right);
346 if (result == false) {
347 ValidatorFailed (validator);
351 ValidatorSucceeded (validator);
356 function RangeValidatorEvaluateIsValid (validator)
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");
363 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
365 if (ctrl_value == "") {
366 ValidatorSucceeded (validator);
370 var val = Convert (ctrl_value, DataType, validator);
371 if (val == null || val < MinimumValue || val > MaximumValue) {
372 ValidatorFailed (validator);
376 ValidatorSucceeded (validator);
381 function RegularExpressionValidatorEvaluateIsValid (validator)
383 var ValidationExpression = validator.getAttribute ("validationexpression");
384 var ControlToValidate = validator.getAttribute ("controltovalidate");
386 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
388 if (ctrl_value == "") {
389 ValidatorSucceeded (validator);
394 return false; // better fail than pass invalidated input to the
397 var r = new RegExp (ValidationExpression);
398 match = r.exec (ctrl_value);
399 if (match == null || match[0] != ctrl_value) {
400 ValidatorFailed (validator);
404 ValidatorSucceeded (validator);
409 function RequiredFieldValidatorEvaluateIsValid (validator)
411 var InitialValue = validator.getAttribute ("initialvalue");
412 var ControlToValidate = validator.getAttribute ("controltovalidate");
414 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
416 if (ctrl_value == ValidatorTrim (InitialValue)) {
417 ValidatorFailed (validator);
421 ValidatorSucceeded (validator);
426 function CustomValidatorEvaluateIsValid (validator)
428 var InitialValue = validator.getAttribute ("initialvalue");
429 var ControlToValidate = validator.getAttribute ("controltovalidate");
431 if (!ControlToValidate) {
432 ValidatorSucceeded (validator);
436 var evaluationfunc = validator.getAttribute ("clientvalidationfunction");
438 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
442 if (evaluationfunc && evaluationfunc != "") {
443 args = {Value:ctrl_value, IsValid:false};
444 eval (evaluationfunc + "(validator, args)");
445 result = args.IsValid;
449 ValidatorSucceeded (validator);
453 ValidatorFailed (validator);
458 /*********************/
459 /* utility functions */
461 function Convert (s, ty, validator)
463 var cvt = this ["To" + ty];
464 if (typeof (cvt) == 'function')
465 return cvt (s, validator);
470 function ValidatorUpdateDisplay (v, valid)
472 var display = v.getAttribute ("display");
474 /* for validators that aren't displayed, do nothing */
475 if (display == "None") {
479 v.style.visibility = (valid ? "hidden" : "visible");
480 if (display == "Dynamic") {
481 v.style.display = (valid ? "none" : "inline");
485 function ValidatorGetErrorMessage (v)
487 var text = v.getAttribute ("errormessage");
488 if (text == null || text == "")
489 text = v.getAttribute ("text");
495 function ValidatorGetText (v)
497 var text = v.getAttribute ("text");
498 if (text == null || text == "")
499 text = v.getAttribute ("errormessage");
505 function ValidatorFailed (v)
507 var text = ValidatorGetText (v);
510 ValidatorUpdateDisplay (v, false);
513 function ValidatorSucceeded (v)
517 ValidatorUpdateDisplay (v, true);
520 function GetElement(id)
522 var x = document.getElementById ? document.getElementById (id) :
523 ((document.all) ? document.all [id] : null);