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 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 for (var v = 0; v < webForm.Page_Validators.length; v++) {
47 var vo = webForm.Page_Validators [v];
49 if (typeof(vo.isvalid) == "string" && vo.isvalid == "False")
54 if (typeof(vo.enabled) == "string" && vo.enabled == "False")
59 if (typeof(vo.evaluationfunction) == "string")
60 vo.evaluationfunction = webForm [vo.evaluationfunction];
63 webForm.Page_ValidationActive = true;
66 webForm.validation_result = true;
68 webForm.ValidationSummaryOnSubmit = function (group)
70 /* handle validation summaries here */
71 if (webForm.validation_result == false && webForm.have_validation_summaries) {
73 for (var vi = 0; vi < webForm.Page_ValidationSummaries.length; vi++) {
74 var vs = webForm.Page_ValidationSummaries[vi];
76 if(webForm.IsValidationGroupMatch(vs, group)) {
79 if(typeof(vs.headertext)=="string")
80 header = vs.headertext;
82 if (vs.showsummary != "False") {
83 if (typeof(vs.displaymode) != "string")
84 vs.displaymode = "BulletList";
88 if (vs.displaymode == "List") {
94 else if (vs.displaymode == "SingleParagraph") {
109 for (var v = 0; v < webForm.Page_Validators.length; v++) {
110 var vo = webForm.Page_Validators [v];
113 var text = vo.errormessage;
114 if (text != null && text != "") {
115 html += item_pre + text + item_post;
122 vs.style.display = "block";
125 if (vs.showmessagebox == "True") {
128 for (var v = 0; v < webForm.Page_Validators.length; v++) {
129 var vo = webForm.Page_Validators [v];
132 var text = vo.errormessage;
133 if (text != null && text != "") {
134 v_contents += "-" + text + "\n";
139 var alert_header = header;
140 if (alert_header != "")
141 alert_header += "\n";
142 summary_contents = alert_header + v_contents;
143 alert (summary_contents);
150 webForm.ValidatorCommonOnSubmit = function ()
152 var rv = webForm.validation_result;
153 webForm.validation_result = true;
157 webForm.ValidatorGetValue = function (controlname)
159 var el = webForm.GetElement (controlname);
161 /* if the element has a 'value' attribute, return it */
162 if (typeof (el.value) != 'undefined' && el.value != null) {
166 /* if it's a select, loop over the options looking for the
168 if (typeof (el.selectedIndex) != 'undefined') {
169 return el.options[el.selectedIndex].value;
172 return webForm.ValidatorGetValueRecursive(el);
175 webForm.ValidatorGetValueRecursive = function (el)
177 if (typeof(el.value) == "string") {
178 if (el.type != "radio" || el.checked == true) return el.value;
180 for (var i = 0; i<el.childNodes.length; i++) {
181 var val = webForm.ValidatorGetValueRecursive(el.childNodes[i]);
182 if (val != "") return val;
187 webForm.ValidatorTrim = function (s)
189 s = s.replace (/^\s+/g, "");
190 s = s.replace (/\s+$/g, "");
195 webForm.Page_ClientValidate = function (group)
197 webForm.validation_result = true;
199 /* clear out the existing text from all our summaries */
200 if (webForm.have_validation_summaries) {
201 for (var vi = 0; vi < webForm.Page_ValidationSummaries.length; vi++) {
202 var vs = webForm.Page_ValidationSummaries[vi];
203 vs.style.display = "none";
208 var invalidControlHasBeenFocused = false;
209 for (var v = 0; v < webForm.Page_Validators.length; v++) {
210 var vo = webForm.Page_Validators [v];
211 var evalfunc = vo.evaluationfunction;
214 if (!vo._enabled || !webForm.IsValidationGroupMatch(vo, group)) {
216 webForm.ValidatorSucceeded (vo);
219 result = evalfunc.call (this, vo);
223 webForm.validation_result = false;
224 if (!invalidControlHasBeenFocused && typeof(vo.focusOnError) == "string" && vo.focusOnError == "t") {
225 invalidControlHasBeenFocused = webForm.ValidatorSetFocus(vo);
229 vo._isvalid = result;
231 webForm.ValidationSummaryOnSubmit(group);
232 return webForm.validation_result;
235 webForm.IsValidationGroupMatch = function (vo, group) {
237 if (typeof(vo.validationGroup) == "string")
238 valGroup = vo.validationGroup;
239 if ((typeof(group) == "undefined") || (group == null)) {
240 return (valGroup == "");
242 return (valGroup == group);
245 webForm.ValidatorSetFocus = function (val) {
246 var ctrl = webForm.GetElement(val.controltovalidate);
247 if ((typeof(ctrl) != "undefined") && (ctrl != null) &&
248 ((ctrl.tagName.toLowerCase() != "input") || (ctrl.type.toLowerCase() != "hidden")) &&
249 (typeof(ctrl.disabled) == "undefined" || ctrl.disabled == null || ctrl.disabled == false) &&
250 (typeof(ctrl.visible) == "undefined" || ctrl.visible == null || ctrl.visible != false) &&
251 (webForm.IsInVisibleContainer(ctrl))) {
252 if (ctrl.tagName.toLowerCase() == "table") {
253 var inputElements = ctrl.getElementsByTagName("input");
254 var lastInputElement = inputElements[inputElements.length -1];
255 if (lastInputElement != null) {
256 ctrl = lastInputElement;
259 if (typeof(ctrl.focus) != "undefined" && ctrl.focus != null) {
267 webForm.IsInVisibleContainer = function (ctrl) {
268 if (typeof(ctrl.style) != "undefined" &&
269 ((typeof(ctrl.style.display) != "undefined" && ctrl.style.display == "none") ||
270 (typeof(ctrl.style.visibility) != "undefined" && ctrl.style.visibility == "hidden"))) {
273 else if (typeof(ctrl.parentNode) != "undefined" && ctrl.parentNode != null && ctrl.parentNode != ctrl) {
274 return webForm.IsInVisibleContainer(ctrl.parentNode);
279 /*******************/
280 /* type converters */
282 webForm.ToInteger = function (s, validator)
284 if (s.match(/^\s*[-\+]?\d+\s*$/) == null)
286 var v = parseInt(s, 10);
287 return (isNaN(v) ? null : v);
290 webForm.ToString = function (s, validator)
295 webForm.ToDouble = function (s, validator)
297 if ((v = parseFloat(s)) != s - 0)
303 webForm.ToDate = function (s, validator)
305 if (!webForm.HaveRegexp ())
307 var m, day, month, year;
308 var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\s*$");
309 m = s.match(yearFirstExp);
310 if (m != null && (m[2].length == 4 || validator.dateorder == "ymd")) {
313 year = (m[2].length == 4) ? m[2] : webForm.GetFullYear(parseInt(m[3], 10), validator.cutoffyear)
316 if (validator.dateorder == "ymd") return null;
317 var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
318 m = s.match(yearLastExp);
319 if (m == null) return null;
320 if (validator.dateorder == "mdy") {
328 year = (m[5].length == 4) ? m[5] : webForm.GetFullYear(parseInt(m[6], 10), validator.cutoffyear)
331 var date = new Date(year, month, day);
332 return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
335 webForm.ToCurrency = function (s, validator)
337 if (!webForm.HaveRegexp ())
340 var hasDigits = (validator.digits > 0);
341 var beginGroupSize, subsequentGroupSize;
342 var groupSizeNum = parseInt(validator.groupsize, 10);
343 if (!isNaN(groupSizeNum) && groupSizeNum > 0) {
344 beginGroupSize = "{1," + groupSizeNum + "}";
345 subsequentGroupSize = "{" + groupSizeNum + "}";
348 beginGroupSize = subsequentGroupSize = "+";
350 var exp = new RegExp("^\\s*([-\\+])?((\\d" + beginGroupSize + "(\\" + validator.groupchar + "\\d" + subsequentGroupSize + ")+)|\\d*)"
351 + (hasDigits ? "\\" + validator.decimalchar + "?(\\d{0," + validator.digits + "})" : "")
353 var m = s.match(exp);
356 if (m[2].length == 0 && hasDigits && m[5].length == 0)
358 var cleanInput = (m[1] != null ? m[1] : "") + m[2].replace(new RegExp("(\\" + validator.groupchar + ")", "g"), "") + ((hasDigits && m[5].length > 0) ? "." + m[5] : "");
359 var num = parseFloat(cleanInput);
360 return (isNaN(num) ? null : num);
363 webForm.GetFullYear = function (year, maxYear)
365 var twoDigitMaxYear = maxYear % 100;
366 var centure = maxYear - twoDigitMaxYear;
367 return ((year > twoDigitMaxYear) ? (centure - 100 + year) : (centure + year));
370 /*******************/
373 webForm.CompareValidatorEvaluateIsValid = function (validator)
375 var Operator = validator.operator.toLowerCase();
376 var ControlToValidate = validator.controltovalidate;
377 var DataType = validator.type;
379 var ctrl_value = webForm.ValidatorGetValue (ControlToValidate);
380 if (webForm.ValidatorTrim (ctrl_value) == "") {
381 webForm.ValidatorSucceeded (validator);
385 var left = webForm.Convert (ctrl_value, DataType, validator);
387 webForm.ValidatorFailed (validator);
391 if (Operator == "datatypecheck") {
392 webForm.ValidatorSucceeded (validator);
397 if (typeof(validator.controltocompare) == "string" && document.getElementById(validator.controltocompare))
398 compare = webForm.ValidatorGetValue(validator.controltocompare);
399 else if (typeof(validator.valuetocompare) == "string")
400 compare = validator.valuetocompare;
402 var right = compare != null ? webForm.Convert (compare, DataType, validator) : null;
404 webForm.ValidatorSucceeded (validator);
410 if (Operator == "equal") {
411 result = (left == right);
413 else if (Operator == "notequal") {
414 result = (left != right);
416 else if (Operator == "lessthan") {
417 result = (left < right);
419 else if (Operator == "lessthanequal") {
420 result = (left <= right);
422 else if (Operator == "greaterthan") {
423 result = (left > right);
425 else if (Operator == "greaterthanequal") {
426 result = (left >= right);
429 if (result == false) {
430 webForm.ValidatorFailed (validator);
434 webForm.ValidatorSucceeded (validator);
439 webForm.RangeValidatorEvaluateIsValid = function (validator)
441 var ControlToValidate = validator.controltovalidate;
442 var DataType = validator.type;
444 var ctrl_value = webForm.ValidatorTrim (webForm.ValidatorGetValue (ControlToValidate));
446 if (ctrl_value == "") {
447 webForm.ValidatorSucceeded (validator);
451 var MinimumValue = webForm.Convert (validator.minimumvalue, DataType, validator);
452 var MaximumValue = webForm.Convert (validator.maximumvalue, DataType, validator);
453 var val = webForm.Convert (ctrl_value, DataType, validator);
454 if (val == null || val < MinimumValue || val > MaximumValue) {
455 webForm.ValidatorFailed (validator);
459 webForm.ValidatorSucceeded (validator);
464 webForm.RegularExpressionValidatorEvaluateIsValid = function (validator)
466 var ValidationExpression = validator.validationexpression;
467 var ControlToValidate = validator.controltovalidate;
469 var ctrl_value = webForm.ValidatorTrim (webForm.ValidatorGetValue (ControlToValidate));
471 if (ctrl_value == "") {
472 webForm.ValidatorSucceeded (validator);
476 if (!webForm.HaveRegexp ())
479 var r = new RegExp (ValidationExpression);
480 match = r.exec (ctrl_value);
481 if (match == null || match[0] != ctrl_value) {
482 webForm.ValidatorFailed (validator);
486 webForm.ValidatorSucceeded (validator);
491 webForm.RequiredFieldValidatorEvaluateIsValid = function (validator)
493 var InitialValue = validator.initialvalue;
494 var ControlToValidate = validator.controltovalidate;
496 var ctrl_value = webForm.ValidatorTrim (webForm.ValidatorGetValue (ControlToValidate));
498 if (ctrl_value == webForm.ValidatorTrim (InitialValue)) {
499 webForm.ValidatorFailed (validator);
503 webForm.ValidatorSucceeded (validator);
508 webForm.CustomValidatorEvaluateIsValid = function (validator)
510 var ControlToValidate = validator.controltovalidate;
512 if (!ControlToValidate) {
513 webForm.ValidatorSucceeded (validator);
517 var evaluationfunc = validator.clientvalidationfunction;
519 var ctrl_value = webForm.ValidatorTrim (webForm.ValidatorGetValue (ControlToValidate));
521 if ((ctrl_value.length == 0) && ((typeof(validator.validateemptytext) != "string") || (validator.validateemptytext != "true"))) {
522 webForm.ValidatorSucceeded (validator);
528 if (evaluationfunc && evaluationfunc != "") {
529 args = {Value:ctrl_value, IsValid:true};
530 eval (evaluationfunc + "(validator, args)");
531 result = args.IsValid;
535 webForm.ValidatorSucceeded (validator);
539 webForm.ValidatorFailed (validator);
544 /*********************/
545 /* utility functions */
547 webForm.Convert = function (s, ty, validator)
549 var cvt = this ["To" + ty];
550 if (typeof (cvt) == 'function')
551 return cvt.call (this, s, validator);
556 webForm.ValidatorUpdateDisplay = function (v, valid)
558 var display = v.display;
560 /* for validators that aren't displayed, do nothing */
561 if (display == "None") {
565 v.style.visibility = (valid ? "hidden" : "visible");
566 if (display == "Dynamic") {
567 v.style.display = (valid ? "none" : "inline");
571 webForm.ValidatorFailed = function (v)
573 webForm.ValidatorUpdateDisplay (v, false);
576 webForm.ValidatorSucceeded = function (v)
578 webForm.ValidatorUpdateDisplay (v, true);
581 webForm.GetElement = function (id)
583 var x = document.getElementById ? document.getElementById (id) :
584 ((document.all) ? document.all [id] : null);