5 * Chris Toshok (toshok@ximian.com)
7 * (c) 2005-2009 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 function WebFormValidation_Initialize(webForm) {
31 webForm.have_validation_summaries = false;
33 webForm.HaveRegexp = function ()
40 webForm.ValidatorOnLoad = function ()
42 if (typeof (webForm.Page_ValidationSummaries) != 'undefined' && webForm.Page_ValidationSummaries != null) {
43 webForm.have_validation_summaries = true;
46 if (typeof (webForm.Page_Validators) != 'undefined' || webForm.Page_Validators != null) {
47 for (var v = 0; v < webForm.Page_Validators.length; v++) {
48 var vo = webForm.Page_Validators [v];
50 if (typeof(vo.isvalid) == "string" && vo.isvalid == "False")
55 if (typeof(vo.enabled) == "string" && vo.enabled == "False")
60 if (typeof(vo.evaluationfunction) == "string")
61 vo.evaluationfunction = webForm [vo.evaluationfunction];
65 webForm.Page_ValidationActive = true;
68 webForm.validation_result = true;
70 webForm.ValidationSummaryOnSubmit = function (group)
72 /* handle validation summaries here */
73 if (webForm.validation_result == false && webForm.have_validation_summaries) {
75 for (var vi = 0; vi < webForm.Page_ValidationSummaries.length; vi++) {
76 var vs = webForm.Page_ValidationSummaries[vi];
78 if(webForm.IsValidationGroupMatch(vs, group)) {
81 if(typeof(vs.headertext)=="string")
82 header = vs.headertext;
84 if (vs.showsummary != "False") {
85 if (typeof(vs.displaymode) != "string")
86 vs.displaymode = "BulletList";
90 if (vs.displaymode == "List") {
96 else if (vs.displaymode == "SingleParagraph") {
111 for (var v = 0; v < webForm.Page_Validators.length; v++) {
112 var vo = webForm.Page_Validators [v];
115 var text = vo.errormessage;
116 if (text != null && text != "") {
117 html += item_pre + text + item_post;
124 vs.style.display = "block";
127 if (vs.showmessagebox == "True") {
130 for (var v = 0; v < webForm.Page_Validators.length; v++) {
131 var vo = webForm.Page_Validators [v];
134 var text = vo.errormessage;
135 if (text != null && text != "") {
136 v_contents += "-" + text + "\n";
141 var alert_header = header;
142 if (alert_header != "")
143 alert_header += "\n";
144 summary_contents = alert_header + v_contents;
145 alert (summary_contents);
152 webForm.ValidatorCommonOnSubmit = function ()
154 var rv = webForm.validation_result;
155 webForm.validation_result = true;
159 webForm.ValidatorGetValue = function (controlname)
161 var el = webForm.GetElement (controlname);
165 /* if the element has a 'value' attribute, return it */
166 if (typeof (el.value) != 'undefined' && el.value != null) {
170 /* if it's a select, loop over the options looking for the
172 if (typeof (el.selectedIndex) != 'undefined') {
173 return el.options[el.selectedIndex].value;
176 return webForm.ValidatorGetValueRecursive(el);
179 webForm.ValidatorGetValueRecursive = function (el)
181 if (typeof(el.value) == "string") {
182 if (el.type != "radio" || el.checked == true) return el.value;
184 for (var i = 0; i<el.childNodes.length; i++) {
185 var val = webForm.ValidatorGetValueRecursive(el.childNodes[i]);
186 if (val != "") return val;
191 webForm.ValidatorTrim = function (s)
196 s = s.replace (/^\s+/g, "");
197 s = s.replace (/\s+$/g, "");
202 webForm.Page_ClientValidate = function (group)
204 webForm.validation_result = true;
206 /* clear out the existing text from all our summaries */
207 if (webForm.have_validation_summaries) {
208 for (var vi = 0; vi < webForm.Page_ValidationSummaries.length; vi++) {
209 var vs = webForm.Page_ValidationSummaries[vi];
210 vs.style.display = "none";
215 var invalidControlHasBeenFocused = false;
216 if (typeof (webForm.Page_Validators) != 'undefined' || webForm.Page_Validators != null) {
217 for (var v = 0; v < webForm.Page_Validators.length; v++) {
218 var vo = webForm.Page_Validators [v];
219 var evalfunc = vo.evaluationfunction;
221 var el = webForm.GetElement (vo.controltovalidate);
225 webForm.ValidatorSucceeded (vo);
226 } else if (!vo._enabled || !webForm.IsValidationGroupMatch(vo, group)) {
228 webForm.ValidatorSucceeded (vo);
230 result = evalfunc.call (this, vo);
234 webForm.validation_result = false;
235 if (!invalidControlHasBeenFocused && typeof(vo.focusOnError) == "string" && vo.focusOnError == "t") {
236 invalidControlHasBeenFocused = webForm.ValidatorSetFocus(vo);
240 vo._isvalid = result;
242 webForm.ValidationSummaryOnSubmit(group);
245 return webForm.validation_result;
248 webForm.IsValidationGroupMatch = function (vo, group) {
250 if (typeof(vo.validationGroup) == "string")
251 valGroup = vo.validationGroup;
252 if ((typeof(group) == "undefined") || (group == null)) {
253 return (valGroup == "");
255 return (valGroup == group);
258 webForm.ValidatorSetFocus = function (val) {
259 var ctrl = webForm.GetElement(val.controltovalidate);
260 if ((typeof(ctrl) != "undefined") && (ctrl != null) &&
261 ((ctrl.tagName.toLowerCase() != "input") || (ctrl.type.toLowerCase() != "hidden")) &&
262 (typeof(ctrl.disabled) == "undefined" || ctrl.disabled == null || ctrl.disabled == false) &&
263 (typeof(ctrl.visible) == "undefined" || ctrl.visible == null || ctrl.visible != false) &&
264 (webForm.IsInVisibleContainer(ctrl))) {
265 if (ctrl.tagName.toLowerCase() == "table") {
266 var inputElements = ctrl.getElementsByTagName("input");
267 var lastInputElement = inputElements[inputElements.length -1];
268 if (lastInputElement != null) {
269 ctrl = lastInputElement;
272 if (typeof(ctrl.focus) != "undefined" && ctrl.focus != null) {
280 webForm.IsInVisibleContainer = function (ctrl) {
281 if (typeof(ctrl.style) != "undefined" &&
282 ((typeof(ctrl.style.display) != "undefined" && ctrl.style.display == "none") ||
283 (typeof(ctrl.style.visibility) != "undefined" && ctrl.style.visibility == "hidden"))) {
286 else if (typeof(ctrl.parentNode) != "undefined" && ctrl.parentNode != null && ctrl.parentNode != ctrl) {
287 return webForm.IsInVisibleContainer(ctrl.parentNode);
292 /*******************/
293 /* type converters */
295 webForm.ToInteger = function (s, validator)
297 if (s.match(/^\s*[-\+]?\d+\s*$/) == null)
299 var v = parseInt(s, 10);
300 return (isNaN(v) ? null : v);
303 webForm.ToString = function (s, validator)
308 webForm.ToDouble = function (s, validator)
310 if ((v = parseFloat(s)) != s - 0)
316 webForm.ToDate = function (s, validator)
318 if (!webForm.HaveRegexp ())
320 var m, day, month, year;
321 var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\s*$");
322 m = s.match(yearFirstExp);
323 if (m != null && (m[2].length == 4 || validator.dateorder == "ymd")) {
326 year = (m[2].length == 4) ? m[2] : webForm.GetFullYear(parseInt(m[3], 10), validator.cutoffyear)
329 if (validator.dateorder == "ymd") return null;
330 var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
331 m = s.match(yearLastExp);
332 if (m == null) return null;
333 if (validator.dateorder == "mdy") {
341 year = (m[5].length == 4) ? m[5] : webForm.GetFullYear(parseInt(m[6], 10), validator.cutoffyear)
344 var date = new Date(year, month, day);
345 return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
348 webForm.ToCurrency = function (s, validator)
350 if (!webForm.HaveRegexp ())
353 var hasDigits = (validator.digits > 0);
354 var beginGroupSize, subsequentGroupSize;
355 var groupSizeNum = parseInt(validator.groupsize, 10);
356 if (!isNaN(groupSizeNum) && groupSizeNum > 0) {
357 beginGroupSize = "{1," + groupSizeNum + "}";
358 subsequentGroupSize = "{" + groupSizeNum + "}";
361 beginGroupSize = subsequentGroupSize = "+";
363 var exp = new RegExp("^\\s*([-\\+])?((\\d" + beginGroupSize + "(\\" + validator.groupchar + "\\d" + subsequentGroupSize + ")+)|\\d*)"
364 + (hasDigits ? "\\" + validator.decimalchar + "?(\\d{0," + validator.digits + "})" : "")
366 var m = s.match(exp);
369 if (m[2].length == 0 && hasDigits && m[5].length == 0)
371 var cleanInput = (m[1] != null ? m[1] : "") + m[2].replace(new RegExp("(\\" + validator.groupchar + ")", "g"), "") + ((hasDigits && m[5].length > 0) ? "." + m[5] : "");
372 var num = parseFloat(cleanInput);
373 return (isNaN(num) ? null : num);
376 webForm.GetFullYear = function (year, maxYear)
378 var twoDigitMaxYear = maxYear % 100;
379 var centure = maxYear - twoDigitMaxYear;
380 return ((year > twoDigitMaxYear) ? (centure - 100 + year) : (centure + year));
383 /*******************/
386 webForm.CompareValidatorEvaluateIsValid = function (validator)
388 var Operator = validator.operator.toLowerCase();
389 var ControlToValidate = validator.controltovalidate;
390 var DataType = validator.type;
392 var ctrl_value = webForm.ValidatorGetValue (ControlToValidate);
393 if (webForm.ValidatorTrim (ctrl_value) == "") {
394 webForm.ValidatorSucceeded (validator);
398 var left = webForm.Convert (ctrl_value, DataType, validator);
400 webForm.ValidatorFailed (validator);
404 if (Operator == "datatypecheck") {
405 webForm.ValidatorSucceeded (validator);
410 if (typeof(validator.controltocompare) == "string" && document.getElementById(validator.controltocompare))
411 compare = webForm.ValidatorGetValue(validator.controltocompare);
412 else if (typeof(validator.valuetocompare) == "string")
413 compare = validator.valuetocompare;
415 var right = compare != null ? webForm.Convert (compare, DataType, validator) : null;
417 webForm.ValidatorSucceeded (validator);
423 if (Operator == "equal") {
424 result = (left == right);
426 else if (Operator == "notequal") {
427 result = (left != right);
429 else if (Operator == "lessthan") {
430 result = (left < right);
432 else if (Operator == "lessthanequal") {
433 result = (left <= right);
435 else if (Operator == "greaterthan") {
436 result = (left > right);
438 else if (Operator == "greaterthanequal") {
439 result = (left >= right);
442 if (result == false) {
443 webForm.ValidatorFailed (validator);
447 webForm.ValidatorSucceeded (validator);
452 webForm.RangeValidatorEvaluateIsValid = function (validator)
454 var ControlToValidate = validator.controltovalidate;
455 var DataType = validator.type;
457 var ctrl_value = webForm.ValidatorTrim (webForm.ValidatorGetValue (ControlToValidate));
459 if (ctrl_value == "") {
460 webForm.ValidatorSucceeded (validator);
464 var MinimumValue = webForm.Convert (validator.minimumvalue, DataType, validator);
465 var MaximumValue = webForm.Convert (validator.maximumvalue, DataType, validator);
466 var val = webForm.Convert (ctrl_value, DataType, validator);
467 if (val == null || val < MinimumValue || val > MaximumValue) {
468 webForm.ValidatorFailed (validator);
472 webForm.ValidatorSucceeded (validator);
477 webForm.RegularExpressionValidatorEvaluateIsValid = function (validator)
479 var ValidationExpression = validator.validationexpression;
480 var ControlToValidate = validator.controltovalidate;
482 var ctrl_value = webForm.ValidatorTrim (webForm.ValidatorGetValue (ControlToValidate));
484 if (ctrl_value == "") {
485 webForm.ValidatorSucceeded (validator);
489 if (!webForm.HaveRegexp ())
492 var r = new RegExp (ValidationExpression);
493 match = r.exec (ctrl_value);
494 if (match == null || match[0] != ctrl_value) {
495 webForm.ValidatorFailed (validator);
499 webForm.ValidatorSucceeded (validator);
504 webForm.RequiredFieldValidatorEvaluateIsValid = function (validator)
506 var InitialValue = validator.initialvalue;
507 var ControlToValidate = validator.controltovalidate;
509 var ctrl_value = webForm.ValidatorTrim (webForm.ValidatorGetValue (ControlToValidate));
511 if (ctrl_value == webForm.ValidatorTrim (InitialValue)) {
512 webForm.ValidatorFailed (validator);
516 webForm.ValidatorSucceeded (validator);
521 webForm.CustomValidatorEvaluateIsValid = function (validator)
523 var ControlToValidate = validator.controltovalidate;
524 var evaluationfunc = validator.clientvalidationfunction;
527 if (ControlToValidate) {
528 ctrl_value = webForm.ValidatorTrim (webForm.ValidatorGetValue (ControlToValidate));
530 if ((ctrl_value.length == 0) && ((typeof(validator.validateemptytext) != "string") || (validator.validateemptytext != "true"))) {
531 webForm.ValidatorSucceeded (validator);
538 if (evaluationfunc && evaluationfunc != "") {
539 args = {Value:ctrl_value, IsValid:true};
540 eval (evaluationfunc + "(validator, args)");
541 result = args.IsValid;
545 webForm.ValidatorSucceeded (validator);
549 webForm.ValidatorFailed (validator);
554 /*********************/
555 /* utility functions */
557 webForm.Convert = function (s, ty, validator)
559 var cvt = this ["To" + ty];
560 if (typeof (cvt) == 'function')
561 return cvt.call (this, s, validator);
566 webForm.ValidatorUpdateDisplay = function (v, valid)
568 var display = v.display;
570 /* for validators that aren't displayed, do nothing */
571 if (display == "None") {
575 v.style.visibility = (valid ? "hidden" : "visible");
576 if (display == "Dynamic") {
577 v.style.display = (valid ? "none" : "inline");
581 webForm.ValidatorFailed = function (v)
583 webForm.ValidatorUpdateDisplay (v, false);
586 webForm.ValidatorSucceeded = function (v)
588 webForm.ValidatorUpdateDisplay (v, true);
591 webForm.GetElement = function (id)
593 var x = document.getElementById ? document.getElementById (id) :
594 ((document.all) ? document.all [id] : null);