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 = 0; v < Page_ValidationSummaries.length; v++) {
36 var vs = Page_ValidationSummaries [v];
37 if (vs.getAttribute ("validationgroup") == null)
38 vs.setAttribute ("validationgroup", "");
42 for (var v = 0; v < Page_Validators.length; v++) {
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 = 0; vi < Page_ValidationSummaries.length; vi++) {
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 = 0; v < Page_Validators.length; v++) {
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 = 0; v < Page_Validators.length; v++) {
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 = 0; vi < Page_ValidationSummaries.length; vi++) {
189 var vs = Page_ValidationSummaries[vi];
190 vs.style.display = "none";
195 for (var v = 0; v < Page_Validators.length; v++) {
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));
429 if ((ctrl_value.length == 0) && ((typeof(validator.validateemptytext) != "string") || (validator.validateemptytext != "true"))) {
430 ValidatorSucceeded (validator);
436 if (evaluationfunc && evaluationfunc != "") {
437 args = {Value:ctrl_value, IsValid:true};
438 eval (evaluationfunc + "(validator, args)");
439 result = args.IsValid;
443 ValidatorSucceeded (validator);
447 ValidatorFailed (validator);
452 /*********************/
453 /* utility functions */
455 function Convert (s, ty, validator)
457 var cvt = this ["To" + ty];
458 if (typeof (cvt) == 'function')
459 return cvt (s, validator);
464 function ValidatorUpdateDisplay (v, valid)
466 var display = v.getAttribute ("display");
468 /* for validators that aren't displayed, do nothing */
469 if (display == "None") {
473 v.style.visibility = (valid ? "hidden" : "visible");
474 if (display == "Dynamic") {
475 v.style.display = (valid ? "none" : "inline");
479 function ValidatorGetErrorMessage (v)
481 var text = v.getAttribute ("errormessage");
482 if (text == null || text == "")
483 text = v.getAttribute ("text");
489 function ValidatorGetText (v)
491 var text = v.getAttribute ("text");
492 if (text == null || text == "")
493 text = v.getAttribute ("errormessage");
499 function ValidatorFailed (v)
501 var text = ValidatorGetText (v);
504 ValidatorUpdateDisplay (v, false);
507 function ValidatorSucceeded (v)
511 ValidatorUpdateDisplay (v, true);
514 function GetElement(id)
516 var x = document.getElementById ? document.getElementById (id) :
517 ((document.all) ? document.all [id] : null);