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 SetValidatorContext (currForm)
33 if (currForm.Page_Validators && (Page_Validators != currForm.Page_Validators)) {
34 Page_Validators = currForm.Page_Validators;
35 Page_ValidationSummaries = currForm.Page_ValidationSummaries;
40 function ValidatorOnLoad ()
42 if (typeof (Page_ValidationSummaries) != 'undefined' && Page_ValidationSummaries != null) {
43 have_validation_summaries = true;
44 for (var v = 0; v < Page_ValidationSummaries.length; v++) {
45 var vs = Page_ValidationSummaries [v];
46 if (vs.getAttribute ("validationgroup") == null)
47 vs.setAttribute ("validationgroup", "");
51 for (var v = 0; v < Page_Validators.length; v++) {
52 var vo = Page_Validators [v];
54 if (vo.getAttribute ("isvalid") == null)
55 vo.setAttribute ("isvalid", "true");
57 if (vo.getAttribute ("enabled") == null)
58 vo.setAttribute ("enabled", "true");
60 if (vo.getAttribute ("validationgroup") == null)
61 vo.setAttribute ("validationgroup", "");
64 Page_ValidationActive = true;
67 var validation_result = true;
69 function ValidationSummaryOnSubmit (group)
71 /* handle validation summaries here */
72 if (validation_result == false && have_validation_summaries) {
74 for (var vi = 0; vi < Page_ValidationSummaries.length; vi++) {
75 var vs = Page_ValidationSummaries[vi];
77 if(IsValidationGroupMatch(vs, group)) {
79 var header = vs.getAttribute ("headertext");
83 attr = vs.getAttribute ("showsummary");
84 if (attr == null || attr.toLowerCase() == "true") {
85 var displaymode = vs.getAttribute ("displaymode");
86 if (displaymode == null) displaymode = "Bulleted";
90 if (displaymode == "List") {
96 else if (displaymode == "SingleParagraph") {
111 for (var v = 0; v < Page_Validators.length; v++) {
112 var vo = Page_Validators [v];
114 if (vo.getAttribute ("isvalid").toLowerCase() == "false") {
115 var text = ValidatorGetErrorMessage (vo);
116 if (text != null && text != "") {
117 html += item_pre + text + item_post;
124 vs.style.display = "block";
127 attr = vs.getAttribute ("showmessagebox");
128 if (attr != null && attr.toLowerCase() == "true") {
131 for (var v = 0; v < Page_Validators.length; v++) {
132 var vo = Page_Validators [v];
134 if (vo.getAttribute ("isvalid").toLowerCase() == "false") {
135 var text = ValidatorGetErrorMessage (vo);
136 if (text != null && text != "") {
137 v_contents += "-" + text + "\n";
142 var alert_header = header;
143 if (alert_header != "")
144 alert_header += "\n";
145 summary_contents = alert_header + v_contents;
146 alert (summary_contents);
153 function ValidatorCommonOnSubmit ()
155 rv = validation_result;
156 validation_result = true;
160 function ValidatorGetValue (controlname)
162 var el = GetElement (controlname);
164 /* if the element has a 'value' attribute, return it */
165 if (typeof (el.value) != 'undefined' && el.value != null) {
169 /* if it's a select, loop over the options looking for the
171 if (typeof (el.selectedIndex) != 'undefined') {
172 return el.options[el.selectedIndex].value;
176 function ValidatorTrim (s)
178 s = s.replace (/^\s+/g, "");
179 s = s.replace (/\s+$/g, "");
184 function Page_ClientValidate(group)
186 validation_result = true;
188 /* clear out the existing text from all our summaries */
189 if (have_validation_summaries) {
190 for (var vi = 0; vi < Page_ValidationSummaries.length; vi++) {
191 var vs = Page_ValidationSummaries[vi];
192 vs.style.display = "none";
197 var invalidControlHasBeenFocused = false;
198 for (var v = 0; v < Page_Validators.length; v++) {
199 var vo = Page_Validators [v];
200 var evalfunc = this[vo.getAttribute ("evaluationfunction")];
203 if (vo.getAttribute ("enabled").toLowerCase() == "false" || !IsValidationGroupMatch(vo, group)) {
205 ValidatorSucceeded (vo);
208 result = evalfunc (vo);
212 validation_result = false;
213 if (!invalidControlHasBeenFocused && typeof(vo.focusOnError) == "string" && vo.focusOnError == "t") {
214 invalidControlHasBeenFocused = ValidatorSetFocus(vo);
218 vo.setAttribute("isvalid", result ? "true" : "false");
220 ValidationSummaryOnSubmit(group);
221 return validation_result;
224 function IsValidationGroupMatch(vo, group) {
225 var valGroup = vo.getAttribute ("validationgroup");
226 if ((typeof(group) == "undefined") || (group == null)) {
227 return (valGroup == "");
229 return (valGroup == group);
232 function ValidatorSetFocus(val) {
233 var ctrl = document.getElementById(val.getAttribute ("controltovalidate"));
234 if ((typeof(ctrl) != "undefined") && (ctrl != null) &&
235 ((ctrl.tagName.toLowerCase() != "input") || (ctrl.type.toLowerCase() != "hidden")) &&
236 (typeof(ctrl.disabled) == "undefined" || ctrl.disabled == null || ctrl.disabled == false) &&
237 (typeof(ctrl.visible) == "undefined" || ctrl.visible == null || ctrl.visible != false) &&
238 (IsInVisibleContainer(ctrl))) {
239 if (ctrl.tagName.toLowerCase() == "table") {
240 var inputElements = ctrl.getElementsByTagName("input");
241 var lastInputElement = inputElements[inputElements.length -1];
242 if (lastInputElement != null) {
243 ctrl = lastInputElement;
246 if (typeof(ctrl.focus) != "undefined" && ctrl.focus != null) {
254 function IsInVisibleContainer(ctrl) {
255 if (typeof(ctrl.style) != "undefined" &&
256 ((typeof(ctrl.style.display) != "undefined" && ctrl.style.display == "none") ||
257 (typeof(ctrl.style.visibility) != "undefined" && ctrl.style.visibility == "hidden"))) {
260 else if (typeof(ctrl.parentNode) != "undefined" && ctrl.parentNode != null && ctrl.parentNode != ctrl) {
261 return IsInVisibleContainer(ctrl.parentNode);
266 /*******************/
267 /* type converters */
269 function ToInteger (s, validator)
271 if ((v = parseInt(s, 10)) != s - 0)
277 function ToString (s, validator)
282 function ToDouble (s, validator)
284 if ((v = parseFloat(s)) != s - 0)
290 function ToDate (s, validator)
292 var m, day, month, year;
293 var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\s*$");
294 m = s.match(yearFirstExp);
295 if (m != null && (m[2].length == 4 || validator.dateorder == "ymd")) {
298 year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10), validator.cutoffyear)
301 if (validator.dateorder == "ymd") return null;
302 var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
303 m = s.match(yearLastExp);
304 if (m == null) return null;
305 if (validator.dateorder == "mdy") {
313 year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10), validator.cutoffyear)
316 var date = new Date(year, month, day);
317 return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
320 function ToCurrency (s, validator)
322 var hasDigits = (validator.digits > 0);
323 var beginGroupSize, subsequentGroupSize;
324 var groupSizeNum = parseInt(validator.groupsize, 10);
325 if (!isNaN(groupSizeNum) && groupSizeNum > 0) {
326 beginGroupSize = "{1," + groupSizeNum + "}";
327 subsequentGroupSize = "{" + groupSizeNum + "}";
330 beginGroupSize = subsequentGroupSize = "+";
332 var exp = new RegExp("^\\s*([-\\+])?((\\d" + beginGroupSize + "(\\" + validator.groupchar + "\\d" + subsequentGroupSize + ")+)|\\d*)"
333 + (hasDigits ? "\\" + validator.decimalchar + "?(\\d{0," + validator.digits + "})" : "")
335 var m = s.match(exp);
338 if (m[2].length == 0 && hasDigits && m[5].length == 0)
340 var cleanInput = (m[1] != null ? m[1] : "") + m[2].replace(new RegExp("(\\" + validator.groupchar + ")", "g"), "") + ((hasDigits && m[5].length > 0) ? "." + m[5] : "");
341 var num = parseFloat(cleanInput);
342 return (isNaN(num) ? null : num);
345 function GetFullYear(year, maxYear)
347 var twoDigitMaxYear = maxYear % 100;
348 var centure = maxYear - twoDigitMaxYear;
349 return ((year > twoDigitMaxYear) ? (centure - 100 + year) : (centure + year));
352 /*******************/
355 function CompareValidatorEvaluateIsValid (validator)
357 var ControlToCompare = validator.getAttribute ("controltocompare");
358 var ValueToCompare = validator.getAttribute ("valuetocompare");
359 var Operator = validator.getAttribute ("operator").toLowerCase();
360 var ControlToValidate = validator.getAttribute ("controltovalidate");
361 var DataType = validator.getAttribute ("datatype");
363 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
364 if (ctrl_value == "") {
365 ValidatorSucceeded (validator);
368 var compare = (ControlToCompare != null && ControlToCompare != "") ? ValidatorTrim (ValidatorGetValue (ControlToCompare)) : ValueToCompare;
370 var left = Convert (ctrl_value, DataType, validator);
372 ValidatorFailed (validator);
376 var right = compare != null ? Convert (compare, DataType, validator) : null;
378 ValidatorSucceeded (validator);
384 if (Operator == "equal") {
385 result = (left == right);
387 else if (Operator == "notequal") {
388 result = (left != right);
390 else if (Operator == "lessthan") {
391 result = (left < right);
393 else if (Operator == "lessthanequal") {
394 result = (left <= right);
396 else if (Operator == "greaterthan") {
397 result = (left > right);
399 else if (Operator == "greaterthanequal") {
400 result = (left >= right);
403 if (result == false) {
404 ValidatorFailed (validator);
408 ValidatorSucceeded (validator);
413 function RangeValidatorEvaluateIsValid (validator)
415 var ControlToValidate = validator.getAttribute ("controltovalidate");
416 var DataType = validator.getAttribute ("datatype");
418 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
420 if (ctrl_value == "") {
421 ValidatorSucceeded (validator);
425 var MinimumValue = Convert (validator.getAttribute ("minimumvalue"), DataType, validator);
426 var MaximumValue = Convert (validator.getAttribute ("maximumvalue"), DataType, validator);
427 var val = Convert (ctrl_value, DataType, validator);
428 if (val == null || val <= MinimumValue || val >= MaximumValue) {
429 ValidatorFailed (validator);
433 ValidatorSucceeded (validator);
438 function RegularExpressionValidatorEvaluateIsValid (validator)
440 var ValidationExpression = validator.getAttribute ("validationexpression");
441 var ControlToValidate = validator.getAttribute ("controltovalidate");
443 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
445 if (ctrl_value == "") {
446 ValidatorSucceeded (validator);
450 var r = new RegExp (ValidationExpression);
451 match = r.exec (ctrl_value);
452 if (match == null || match[0] == "") {
453 ValidatorFailed (validator);
457 ValidatorSucceeded (validator);
462 function RequiredFieldValidatorEvaluateIsValid (validator)
464 var InitialValue = validator.getAttribute ("initialvalue");
465 var ControlToValidate = validator.getAttribute ("controltovalidate");
467 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
469 if (ctrl_value == ValidatorTrim (InitialValue)) {
470 ValidatorFailed (validator);
474 ValidatorSucceeded (validator);
479 function CustomValidatorEvaluateIsValid (validator)
481 var InitialValue = validator.getAttribute ("initialvalue");
482 var ControlToValidate = validator.getAttribute ("controltovalidate");
484 if (!ControlToValidate) {
485 ValidatorSucceeded (validator);
489 var evaluationfunc = validator.getAttribute ("clientvalidationfunction");
491 var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
493 if ((ctrl_value.length == 0) && ((typeof(validator.validateemptytext) != "string") || (validator.validateemptytext != "true"))) {
494 ValidatorSucceeded (validator);
500 if (evaluationfunc && evaluationfunc != "") {
501 args = {Value:ctrl_value, IsValid:true};
502 eval (evaluationfunc + "(validator, args)");
503 result = args.IsValid;
507 ValidatorSucceeded (validator);
511 ValidatorFailed (validator);
516 /*********************/
517 /* utility functions */
519 function Convert (s, ty, validator)
521 var cvt = this ["To" + ty];
522 if (typeof (cvt) == 'function')
523 return cvt (s, validator);
528 function ValidatorUpdateDisplay (v, valid)
530 var display = v.getAttribute ("display");
532 /* for validators that aren't displayed, do nothing */
533 if (display == "None") {
537 v.style.visibility = (valid ? "hidden" : "visible");
538 if (display == "Dynamic") {
539 v.style.display = (valid ? "none" : "inline");
543 function ValidatorGetErrorMessage (v)
545 var text = v.getAttribute ("errormessage");
546 if (text == null || text == "")
547 text = v.getAttribute ("text");
553 function ValidatorGetText (v)
555 var text = v.getAttribute ("text");
556 if (text == null || text == "")
557 text = v.getAttribute ("errormessage");
563 function ValidatorFailed (v)
565 ValidatorUpdateDisplay (v, false);
568 function ValidatorSucceeded (v)
570 ValidatorUpdateDisplay (v, true);
573 function GetElement(id)
575 var x = document.getElementById ? document.getElementById (id) :
576 ((document.all) ? document.all [id] : null);