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 ValidatorOnLoad ()
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", "");
42 for (var v in Page_Validators) {
43 var vo = Page_Validators [v];
44 var funcname = vo.getAttribute ("evaluationfunction");
46 func = this[funcname];
48 vo["evaluationfunction"] = func;
50 if (vo.getAttribute ("isvalid") == null)
51 vo.setAttribute ("isvalid", "true");
53 if (vo.getAttribute ("enabled") == null)
54 vo.setAttribute ("enabled", "true");
56 if (vo.getAttribute ("validationgroup") == null)
57 vo.setAttribute ("validationgroup", "");
59 func = vo ["evaluationfunction"];
62 Page_ValidationActive = true;
65 var validation_result = true;
67 function ValidationSummaryOnSubmit (group)
69 /* handle validation summaries here */
70 if (validation_result == false && have_validation_summaries) {
72 for (var vi in Page_ValidationSummaries) {
73 var vs = Page_ValidationSummaries[vi];
75 if(IsValidationGroupMatch(vs, group)) {
77 var header = vs.getAttribute ("headertext");
81 attr = vs.getAttribute ("showsummary");
82 if (attr == null || attr.toLowerCase() == "true") {
83 var displaymode = vs.getAttribute ("displaymode");
84 if (displaymode == null) displaymode = "Bulleted";
88 if (displaymode == "List") {
94 else if (displaymode == "SingleParagraph") {
109 for (var v in Page_Validators) {
110 var vo = Page_Validators [v];
112 if (vo.getAttribute ("isvalid").toLowerCase() == "false") {
113 var text = ValidatorGetErrorMessage (vo);
114 if (text != null && text != "") {
115 html += item_pre + text + item_post;
122 vs.style.display = "block";
125 attr = vs.getAttribute ("showmessagebox");
126 if (attr != null && attr.toLowerCase() == "true") {
129 for (var v in Page_Validators) {
130 var vo = Page_Validators [v];
132 if (vo.getAttribute ("isvalid").toLowerCase() == "false") {
133 var text = ValidatorGetErrorMessage (vo);
134 if (text != null && text != "") {
135 v_contents += "-" + text + "\n";
140 var alert_header = header;
141 if (alert_header != "")
142 alert_header += "\n";
143 summary_contents = alert_header + v_contents;
144 alert (summary_contents);
151 function ValidatorCommonOnSubmit ()
153 rv = validation_result;
154 validation_result = true;
158 function ValidatorGetValue (controlname)
160 var el = GetElement (controlname);
162 /* if the element has a 'value' attribute, return it */
163 if (typeof (el.value) != 'undefined' && el.value != null) {
167 /* if it's a select, loop over the options looking for the
169 if (typeof (el.selectedIndex) != 'undefined') {
170 return el.options[el.selectedIndex].value;
174 function ValidatorTrim (s)
176 s = s.replace (/^\s+/g, "");
177 s = s.replace (/\s+$/g, "");
182 function Page_ClientValidate(group)
184 validation_result = true;
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";
195 for (var v in Page_Validators) {
196 var vo = Page_Validators [v];
197 var evalfunc = vo["evaluationfunction"];
200 if (vo.getAttribute ("enabled").toLowerCase() == "false" || !IsValidationGroupMatch(vo, group)) {
202 ValidatorSucceeded (vo);
205 result = evalfunc (vo);
209 validation_result = false;
211 vo.setAttribute("isvalid", result ? "true" : "false");
213 ValidationSummaryOnSubmit(group);
214 return validation_result;
217 function IsValidationGroupMatch(vo, group) {
218 var valGroup = vo.getAttribute ("validationgroup");
219 if ((typeof(group) == "undefined") || (group == null)) {
220 return (valGroup == "");
222 return (valGroup == group);
225 /*******************/
226 /* type converters */
228 function ToInteger (s, validator)
230 if ((v = parseInt(s, 10)) != s - 0)
236 function ToString (s, validator)
241 function ToDouble (s, validator)
243 if ((v = parseFloat(s)) != s - 0)
249 function ToDate (s, validator)
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")) {
257 year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10), validator.cutoffyear)
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") {
272 year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10), validator.cutoffyear)
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;
279 function ToCurrency (s, validator)
285 function GetFullYear(year, maxYear)
287 var twoDigitMaxYear = maxYear % 100;
288 var centure = maxYear - twoDigitMaxYear;
289 return ((year > twoDigitMaxYear) ? (centure - 100 + year) : (centure + year));
292 /*******************/
295 function CompareValidatorEvaluateIsValid (validator)
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");
303 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
304 var compare = (ControlToCompare != null && ControlToCompare != "") ? ValidatorTrim (ValidatorGetValue (ControlToCompare)) : ValueToCompare;
306 var left = Convert (ctrl_value, DataType, validator);
308 ValidatorFailed (validator);
312 var right = compare != null ? Convert (compare, DataType, validator) : null;
314 ValidatorSucceeded (validator);
320 if (Operator == "equal") {
321 result = (left == right);
323 else if (Operator == "notequal") {
324 result = (left != right);
326 else if (Operator == "lessthan") {
327 result = (left < right);
329 else if (Operator == "lessthanequal") {
330 result = (left <= right);
332 else if (Operator == "greaterthan") {
333 result = (left > right);
335 else if (Operator == "greaterthanequal") {
336 result = (left >= right);
339 if (result == false) {
340 ValidatorFailed (validator);
344 ValidatorSucceeded (validator);
349 function RangeValidatorEvaluateIsValid (validator)
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");
356 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
358 if (ctrl_value == "") {
359 ValidatorSucceeded (validator);
363 var val = Convert (ctrl_value, DataType, validator);
364 if (val == null || val < MinimumValue || val > MaximumValue) {
365 ValidatorFailed (validator);
369 ValidatorSucceeded (validator);
374 function RegularExpressionValidatorEvaluateIsValid (validator)
376 var ValidationExpression = validator.getAttribute ("validationexpression");
377 var ControlToValidate = validator.getAttribute ("controltovalidate");
379 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
381 if (ctrl_value == "") {
382 ValidatorSucceeded (validator);
386 var r = new RegExp (ValidationExpression);
387 match = r.exec (ctrl_value);
388 if (match == null || match[0] == "") {
389 ValidatorFailed (validator);
393 ValidatorSucceeded (validator);
398 function RequiredFieldValidatorEvaluateIsValid (validator)
400 var InitialValue = validator.getAttribute ("initialvalue");
401 var ControlToValidate = validator.getAttribute ("controltovalidate");
403 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
405 if (ctrl_value == ValidatorTrim (InitialValue)) {
406 ValidatorFailed (validator);
410 ValidatorSucceeded (validator);
415 function CustomValidatorEvaluateIsValid (validator)
417 var InitialValue = validator.getAttribute ("initialvalue");
418 var ControlToValidate = validator.getAttribute ("controltovalidate");
420 if (!ControlToValidate) {
421 ValidatorSucceeded (validator);
425 var evaluationfunc = validator.getAttribute ("clientvalidationfunction");
427 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
431 if (evaluationfunc && evaluationfunc != "") {
432 args = {Value:ctrl_value, IsValid:false};
433 eval (evaluationfunc + "(validator, args)");
434 result = args.IsValid;
438 ValidatorSucceeded (validator);
442 ValidatorFailed (validator);
447 /*********************/
448 /* utility functions */
450 function Convert (s, ty, validator)
452 var cvt = this ["To" + ty];
453 if (typeof (cvt) == 'function')
454 return cvt (s, validator);
459 function ValidatorUpdateDisplay (v, valid)
461 var display = v.getAttribute ("display");
463 /* for validators that aren't displayed, do nothing */
464 if (display == "None") {
468 v.style.visibility = (valid ? "hidden" : "visible");
469 if (display == "Dynamic") {
470 v.style.display = (valid ? "none" : "inline");
474 function ValidatorGetErrorMessage (v)
476 var text = v.getAttribute ("errormessage");
477 if (text == null || text == "")
478 text = v.getAttribute ("text");
484 function ValidatorGetText (v)
486 var text = v.getAttribute ("text");
487 if (text == null || text == "")
488 text = v.getAttribute ("errormessage");
494 function ValidatorFailed (v)
496 var text = ValidatorGetText (v);
499 ValidatorUpdateDisplay (v, false);
502 function ValidatorSucceeded (v)
506 ValidatorUpdateDisplay (v, true);
509 function GetElement(id)
511 var x = document.getElementById ? document.getElementById (id) :
512 ((document.all) ? document.all [id] : null);