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)
230 if ((v = parseInt(s, 10)) != s - 0)
236 function ToString (s)
241 function ToDouble (s)
243 if ((v = parseFloat(s)) != s - 0)
255 function ToCurrency (s)
261 /*******************/
264 function CompareValidatorEvaluateIsValid (validator)
266 var ControlToCompare = validator.getAttribute ("controltocompare");
267 var ValueToCompare = validator.getAttribute ("valuetocompare");
268 var Operator = validator.getAttribute ("operator").toLowerCase();
269 var ControlToValidate = validator.getAttribute ("controltovalidate");
270 var DataType = validator.getAttribute ("datatype");
272 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
273 var compare = (ControlToCompare != null && ControlToCompare != "") ? ValidatorTrim (ValidatorGetValue (ControlToCompare)) : ValueToCompare;
275 var left = Convert (ctrl_value, DataType);
277 ValidatorFailed (validator);
281 var right = Convert (compare, DataType);
283 ValidatorSucceeded (validator);
289 if (Operator == "equal") {
290 result = (left == right);
292 else if (Operator == "notequal") {
293 result = (left != right);
295 else if (Operator == "lessthan") {
296 result = (left < right);
298 else if (Operator == "lessthanequal") {
299 result = (left <= right);
301 else if (Operator == "greaterthan") {
302 result = (left > right);
304 else if (Operator == "greaterthanequal") {
305 result = (left >= right);
308 if (result == false) {
309 ValidatorFailed (validator);
313 ValidatorSucceeded (validator);
318 function RangeValidatorEvaluateIsValid (validator)
320 var MinimumValue = parseInt (validator.getAttribute ("minimumvalue"));
321 var MaximumValue = parseInt (validator.getAttribute ("maximumvalue"));
322 var ControlToValidate = validator.getAttribute ("controltovalidate");
323 var DataType = validator.getAttribute ("datatype");
325 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
327 if (ctrl_value == "") {
328 ValidatorSucceeded (validator);
332 var val = Convert (ctrl_value, DataType);
333 if (val == null || val < MinimumValue || val > MaximumValue) {
334 ValidatorFailed (validator);
338 ValidatorSucceeded (validator);
343 function RegularExpressionValidatorEvaluateIsValid (validator)
345 var ValidationExpression = validator.getAttribute ("validationexpression");
346 var ControlToValidate = validator.getAttribute ("controltovalidate");
348 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
350 if (ctrl_value == "") {
351 ValidatorSucceeded (validator);
355 var r = new RegExp (ValidationExpression);
356 match = r.exec (ctrl_value);
357 if (match == null || match[0] == "") {
358 ValidatorFailed (validator);
362 ValidatorSucceeded (validator);
367 function RequiredFieldValidatorEvaluateIsValid (validator)
369 var InitialValue = validator.getAttribute ("initialvalue");
370 var ControlToValidate = validator.getAttribute ("controltovalidate");
372 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
374 if (ctrl_value == ValidatorTrim (InitialValue)) {
375 ValidatorFailed (validator);
379 ValidatorSucceeded (validator);
384 function CustomValidatorEvaluateIsValid (validator)
386 var InitialValue = validator.getAttribute ("initialvalue");
387 var ControlToValidate = validator.getAttribute ("controltovalidate");
389 if (!ControlToValidate) {
390 ValidatorSucceeded (validator);
394 var evaluationfunc = validator.getAttribute ("clientvalidationfunction");
396 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
400 if (evaluationfunc && evaluationfunc != "") {
401 args = {Value:ctrl_value, IsValid:false};
402 eval (evaluationfunc + "(validator, args)");
403 result = args.IsValid;
407 ValidatorSucceeded (validator);
411 ValidatorFailed (validator);
416 /*********************/
417 /* utility functions */
419 function Convert (s, ty)
421 var cvt = this ["To" + ty];
422 if (typeof (cvt) == 'function')
428 function ValidatorUpdateDisplay (v, valid)
430 var display = v.getAttribute ("display");
432 /* for validators that aren't displayed, do nothing */
433 if (display == "None") {
437 v.style.visibility = (valid ? "hidden" : "visible");
438 if (display == "Dynamic") {
439 v.style.display = (valid ? "none" : "inline");
443 function ValidatorGetErrorMessage (v)
445 var text = v.getAttribute ("errormessage");
446 if (text == null || text == "")
447 text = v.getAttribute ("text");
453 function ValidatorGetText (v)
455 var text = v.getAttribute ("text");
456 if (text == null || text == "")
457 text = v.getAttribute ("errormessage");
463 function ValidatorFailed (v)
465 var text = ValidatorGetText (v);
468 ValidatorUpdateDisplay (v, false);
471 function ValidatorSucceeded (v)
475 ValidatorUpdateDisplay (v, true);
478 function GetElement(id)
480 var x = document.getElementById ? document.getElementById (id) :
481 ((document.all) ? document.all [id] : null);