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;
36 for (var v in Page_Validators) {
37 var vo = Page_Validators [v];
38 var funcname = vo.getAttribute ("evaluationfunction");
40 func = this[funcname];
42 vo["evaluationfunction"] = func;
44 if (vo.getAttribute ("isvalid") == null)
45 vo.setAttribute ("isvalid", "true");
47 if (vo.getAttribute ("enabled") == null)
48 vo.setAttribute ("enabled", "true");
50 func = vo ["evaluationfunction"];
53 Page_ValidationActive = true;
56 var validation_result = true;
58 function ValidatorCommonOnSubmit ()
60 /* handle validation summaries here */
61 if (validation_result == false && have_validation_summaries) {
63 for (var vi in Page_ValidationSummaries) {
64 var vs = Page_ValidationSummaries[vi];
66 var header = vs.getAttribute ("headertext");
70 attr = vs.getAttribute ("showsummary");
71 if (attr == null || attr.toLowerCase() == "true") {
72 var displaymode = vs.getAttribute ("displaymode");
73 if (displaymode == null) displaymode = "Bulleted";
77 if (displaymode == "List") {
83 else if (displaymode == "SingleParagraph") {
98 for (var v in Page_Validators) {
99 var vo = Page_Validators [v];
101 if (vo.getAttribute ("isvalid").toLowerCase() == "false") {
102 var text = ValidatorGetErrorMessage (vo);
103 if (text != null && text != "") {
104 html += item_pre + text + item_post;
111 vs.style.display = "block";
114 attr = vs.getAttribute ("showmessagebox");
115 if (attr != null && attr.toLowerCase() == "true") {
118 for (var v in Page_Validators) {
119 var vo = Page_Validators [v];
121 if (vo.getAttribute ("isvalid").toLowerCase() == "false") {
122 var text = ValidatorGetErrorMessage (vo);
123 if (text != null && text != "") {
124 v_contents += "-" + text + "\n";
129 var alert_header = header;
130 if (alert_header != "")
131 alert_header += "\n";
132 summary_contents = alert_header + v_contents;
133 alert (summary_contents);
138 rv = validation_result;
139 validation_result = true;
143 function ValidatorGetValue (controlname)
145 var el = GetElement (controlname);
147 /* if the element has a 'value' attribute, return it */
148 if (typeof (el.value) != 'undefined' && el.value != null) {
152 /* if it's a select, loop over the options looking for the
154 if (typeof (el.selectedIndex) != 'undefined') {
155 return el.options[el.selectedIndex].value;
159 function ValidatorTrim (s)
161 s = s.replace (/^\s+/g, "");
162 s = s.replace (/\s+$/g, "");
167 function Page_ClientValidate()
169 validation_result = true;
171 /* clear out the existing text from all our summaries */
172 if (have_validation_summaries) {
173 for (var vi in Page_ValidationSummaries) {
174 var vs = Page_ValidationSummaries[vi];
175 vs.style.display = "none";
180 for (var v in Page_Validators) {
181 var vo = Page_Validators [v];
182 var evalfunc = vo["evaluationfunction"];
185 if (vo.getAttribute ("enabled").toLowerCase() == "false") {
187 ValidatorSucceeded (vo);
190 result = evalfunc (vo);
194 validation_result = false;
196 vo.setAttribute("isvalid", result ? "true" : "false");
199 return validation_result;
202 /*******************/
203 /* type converters */
205 function ToInteger (s)
207 if ((v = parseInt(s, 10)) != s - 0)
213 function ToString (s)
218 function ToDouble (s)
220 if ((v = parseFloat(s)) != s - 0)
232 function ToCurrency (s)
238 /*******************/
241 function CompareValidatorEvaluateIsValid (validator)
243 var ControlToCompare = validator.getAttribute ("controltocompare");
244 var ValueToCompare = validator.getAttribute ("valuetocompare");
245 var Operator = validator.getAttribute ("operator").toLowerCase();
246 var ControlToValidate = validator.getAttribute ("controltovalidate");
247 var DataType = validator.getAttribute ("datatype");
249 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
250 var compare = (ControlToCompare != null && ControlToCompare != "") ? ValidatorTrim (ValidatorGetValue (ControlToCompare)) : ValueToCompare;
252 var left = Convert (ctrl_value, DataType);
254 ValidatorFailed (validator);
258 var right = Convert (compare, DataType);
260 ValidatorSucceeded (validator);
266 if (Operator == "equal") {
267 result = (left == right);
269 else if (Operator == "notequal") {
270 result = (left != right);
272 else if (Operator == "lessthan") {
273 result = (left < right);
275 else if (Operator == "lessthanequal") {
276 result = (left <= right);
278 else if (Operator == "greaterthan") {
279 result = (left > right);
281 else if (Operator == "greaterthanequal") {
282 result = (left >= right);
285 if (result == false) {
286 ValidatorFailed (validator);
290 ValidatorSucceeded (validator);
295 function RangeValidatorEvaluateIsValid (validator)
297 var MinimumValue = parseInt (validator.getAttribute ("minimumvalue"));
298 var MaximumValue = parseInt (validator.getAttribute ("maximumvalue"));
299 var ControlToValidate = validator.getAttribute ("controltovalidate");
300 var DataType = validator.getAttribute ("datatype");
302 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
304 if (ctrl_value == "") {
305 ValidatorSucceeded (validator);
309 var val = Convert (ctrl_value, DataType);
310 if (val == null || val < MinimumValue || val > MaximumValue) {
311 ValidatorFailed (validator);
315 ValidatorSucceeded (validator);
320 function RegularExpressionValidatorEvaluateIsValid (validator)
322 var ValidationExpression = validator.getAttribute ("validationexpression");
323 var ControlToValidate = validator.getAttribute ("controltovalidate");
325 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
327 if (ctrl_value == "") {
328 ValidatorSucceeded (validator);
332 var r = new RegExp (ValidationExpression);
333 match = r.exec (ctrl_value);
334 if (match == null || match[0] == "") {
335 ValidatorFailed (validator);
339 ValidatorSucceeded (validator);
344 function RequiredFieldValidatorEvaluateIsValid (validator)
346 var InitialValue = validator.getAttribute ("initialvalue");
347 var ControlToValidate = validator.getAttribute ("controltovalidate");
349 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
351 if (ctrl_value == ValidatorTrim (InitialValue)) {
352 ValidatorFailed (validator);
356 ValidatorSucceeded (validator);
361 function CustomValidatorEvaluateIsValid (validator)
363 var InitialValue = validator.getAttribute ("initialvalue");
364 var ControlToValidate = validator.getAttribute ("controltovalidate");
366 if (!ControlToValidate) {
367 ValidatorSucceeded (validator);
371 var evaluationfunc = validator.getAttribute ("clientvalidationfunction");
373 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
377 if (evaluationfunc && evaluationfunc != "") {
378 args = {Value:ctrl_value, IsValid:false};
379 eval (evaluationfunc + "(validator, args)");
380 result = args.IsValid;
384 ValidatorSucceeded (validator);
388 ValidatorFailed (validator);
393 /*********************/
394 /* utility functions */
396 function Convert (s, ty)
398 var cvt = this ["To" + ty];
399 if (typeof (cvt) == 'function')
405 function ValidatorUpdateDisplay (v, valid)
407 var display = v.getAttribute ("display");
409 /* for validators that aren't displayed, do nothing */
410 if (display == "None") {
414 v.style.visibility = (valid ? "hidden" : "visible");
415 if (display == "Dynamic") {
416 v.style.display = (valid ? "none" : "inline");
420 function ValidatorGetErrorMessage (v)
422 var text = v.getAttribute ("errormessage");
423 if (text == null || text == "")
424 text = v.getAttribute ("text");
430 function ValidatorGetText (v)
432 var text = v.getAttribute ("text");
433 if (text == null || text == "")
434 text = v.getAttribute ("errormessage");
440 function ValidatorFailed (v)
442 var text = ValidatorGetText (v);
445 ValidatorUpdateDisplay (v, false);
448 function ValidatorSucceeded (v)
452 ValidatorUpdateDisplay (v, true);
455 function GetElement(id)
457 var x = document.getElementById ? document.getElementById (id) :
458 ((document.all) ? document.all [id] : null);