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 vo.evaluationfunction = this [vo.evaluationfunction];
62 webForm.Page_ValidationActive = true;
65 webForm.validation_result = true;
67 webForm.ValidationSummaryOnSubmit = function (group)
69 /* handle validation summaries here */
70 if (webForm.validation_result == false && webForm.have_validation_summaries) {
72 for (var vi = 0; vi < webForm.Page_ValidationSummaries.length; vi++) {
73 var vs = webForm.Page_ValidationSummaries[vi];
75 if(webForm.IsValidationGroupMatch(vs, group)) {
78 if(typeof(vs.headertext)=="string")
79 header = vs.headertext;
81 if (vs.showsummary != "False") {
82 if (typeof(vs.displaymode) != "string")
83 vs.displaymode = "BulletList";
87 if (vs.displaymode == "List") {
93 else if (vs.displaymode == "SingleParagraph") {
108 for (var v = 0; v < webForm.Page_Validators.length; v++) {
109 var vo = webForm.Page_Validators [v];
112 var text = vo.errormessage;
113 if (text != null && text != "") {
114 html += item_pre + text + item_post;
121 vs.style.display = "block";
124 if (vs.showmessagebox == "True") {
127 for (var v = 0; v < webForm.Page_Validators.length; v++) {
128 var vo = webForm.Page_Validators [v];
131 var text = vo.errormessage;
132 if (text != null && text != "") {
133 v_contents += "-" + text + "\n";
138 var alert_header = header;
139 if (alert_header != "")
140 alert_header += "\n";
141 summary_contents = alert_header + v_contents;
142 alert (summary_contents);
149 webForm.ValidatorCommonOnSubmit = function ()
151 var rv = webForm.validation_result;
152 webForm.validation_result = true;
156 webForm.ValidatorGetValue = function (controlname)
158 var el = webForm.GetElement (controlname);
160 /* if the element has a 'value' attribute, return it */
161 if (typeof (el.value) != 'undefined' && el.value != null) {
165 /* if it's a select, loop over the options looking for the
167 if (typeof (el.selectedIndex) != 'undefined') {
168 return el.options[el.selectedIndex].value;
171 return webForm.ValidatorGetValueRecursive(el);
174 webForm.ValidatorGetValueRecursive = function (el)
176 if (typeof(el.value) == "string") {
177 if (el.type != "radio" || el.checked == true) return el.value;
179 for (var i = 0; i<el.childNodes.length; i++) {
180 var val = webForm.ValidatorGetValueRecursive(el.childNodes[i]);
181 if (val != "") return val;
186 webForm.ValidatorTrim = function (s)
188 s = s.replace (/^\s+/g, "");
189 s = s.replace (/\s+$/g, "");
194 webForm.Page_ClientValidate = function (group)
196 webForm.validation_result = true;
198 /* clear out the existing text from all our summaries */
199 if (webForm.have_validation_summaries) {
200 for (var vi = 0; vi < webForm.Page_ValidationSummaries.length; vi++) {
201 var vs = webForm.Page_ValidationSummaries[vi];
202 vs.style.display = "none";
207 var invalidControlHasBeenFocused = false;
208 for (var v = 0; v < webForm.Page_Validators.length; v++) {
209 var vo = webForm.Page_Validators [v];
210 var evalfunc = vo.evaluationfunction;
213 if (!vo.enabled || !webForm.IsValidationGroupMatch(vo, group)) {
215 webForm.ValidatorSucceeded (vo);
218 result = evalfunc.call (this, vo);
222 webForm.validation_result = false;
223 if (!invalidControlHasBeenFocused && typeof(vo.focusOnError) == "string" && vo.focusOnError == "t") {
224 invalidControlHasBeenFocused = webForm.ValidatorSetFocus(vo);
230 webForm.ValidationSummaryOnSubmit(group);
231 return webForm.validation_result;
234 webForm.IsValidationGroupMatch = function (vo, group) {
236 if (typeof(vo.validationGroup) == "string")
237 valGroup = vo.validationGroup;
238 if ((typeof(group) == "undefined") || (group == null)) {
239 return (valGroup == "");
241 return (valGroup == group);
244 webForm.ValidatorSetFocus = function (val) {
245 var ctrl = webForm.GetElement(val.controltovalidate);
246 if ((typeof(ctrl) != "undefined") && (ctrl != null) &&
247 ((ctrl.tagName.toLowerCase() != "input") || (ctrl.type.toLowerCase() != "hidden")) &&
248 (typeof(ctrl.disabled) == "undefined" || ctrl.disabled == null || ctrl.disabled == false) &&
249 (typeof(ctrl.visible) == "undefined" || ctrl.visible == null || ctrl.visible != false) &&
250 (webForm.IsInVisibleContainer(ctrl))) {
251 if (ctrl.tagName.toLowerCase() == "table") {
252 var inputElements = ctrl.getElementsByTagName("input");
253 var lastInputElement = inputElements[inputElements.length -1];
254 if (lastInputElement != null) {
255 ctrl = lastInputElement;
258 if (typeof(ctrl.focus) != "undefined" && ctrl.focus != null) {
266 webForm.IsInVisibleContainer = function (ctrl) {
267 if (typeof(ctrl.style) != "undefined" &&
268 ((typeof(ctrl.style.display) != "undefined" && ctrl.style.display == "none") ||
269 (typeof(ctrl.style.visibility) != "undefined" && ctrl.style.visibility == "hidden"))) {
272 else if (typeof(ctrl.parentNode) != "undefined" && ctrl.parentNode != null && ctrl.parentNode != ctrl) {
273 return webForm.IsInVisibleContainer(ctrl.parentNode);
278 /*******************/
279 /* type converters */
281 webForm.ToInteger = function (s, validator)
283 if ((v = parseInt(s, 10)) != s - 0)
289 webForm.ToString = function (s, validator)
294 webForm.ToDouble = function (s, validator)
296 if ((v = parseFloat(s)) != s - 0)
302 webForm.ToDate = function (s, validator)
304 if (!webForm.HaveRegexp ())
306 var m, day, month, year;
307 var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\s*$");
308 m = s.match(yearFirstExp);
309 if (m != null && (m[2].length == 4 || validator.dateorder == "ymd")) {
312 year = (m[2].length == 4) ? m[2] : webForm.GetFullYear(parseInt(m[3], 10), validator.cutoffyear)
315 if (validator.dateorder == "ymd") return null;
316 var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
317 m = s.match(yearLastExp);
318 if (m == null) return null;
319 if (validator.dateorder == "mdy") {
327 year = (m[5].length == 4) ? m[5] : webForm.GetFullYear(parseInt(m[6], 10), validator.cutoffyear)
330 var date = new Date(year, month, day);
331 return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
334 webForm.ToCurrency = function (s, validator)
336 if (!webForm.HaveRegexp ())
339 var hasDigits = (validator.digits > 0);
340 var beginGroupSize, subsequentGroupSize;
341 var groupSizeNum = parseInt(validator.groupsize, 10);
342 if (!isNaN(groupSizeNum) && groupSizeNum > 0) {
343 beginGroupSize = "{1," + groupSizeNum + "}";
344 subsequentGroupSize = "{" + groupSizeNum + "}";
347 beginGroupSize = subsequentGroupSize = "+";
349 var exp = new RegExp("^\\s*([-\\+])?((\\d" + beginGroupSize + "(\\" + validator.groupchar + "\\d" + subsequentGroupSize + ")+)|\\d*)"
350 + (hasDigits ? "\\" + validator.decimalchar + "?(\\d{0," + validator.digits + "})" : "")
352 var m = s.match(exp);
355 if (m[2].length == 0 && hasDigits && m[5].length == 0)
357 var cleanInput = (m[1] != null ? m[1] : "") + m[2].replace(new RegExp("(\\" + validator.groupchar + ")", "g"), "") + ((hasDigits && m[5].length > 0) ? "." + m[5] : "");
358 var num = parseFloat(cleanInput);
359 return (isNaN(num) ? null : num);
362 webForm.GetFullYear = function (year, maxYear)
364 var twoDigitMaxYear = maxYear % 100;
365 var centure = maxYear - twoDigitMaxYear;
366 return ((year > twoDigitMaxYear) ? (centure - 100 + year) : (centure + year));
369 /*******************/
372 webForm.CompareValidatorEvaluateIsValid = function (validator)
374 var Operator = validator.operator.toLowerCase();
375 var ControlToValidate = validator.controltovalidate;
376 var DataType = validator.type;
378 var ctrl_value = webForm.ValidatorTrim (webForm.ValidatorGetValue (ControlToValidate));
379 if (ctrl_value == "") {
380 webForm.ValidatorSucceeded (validator);
384 if (typeof(validator.controltocompare) == "string" && document.getElementById(validator.controltocompare))
385 compare = webForm.ValidatorTrim(webForm.ValidatorGetValue(val.controltocompare));
386 else if (typeof(validator.valuetocompare) == "string")
387 compare = validator.valuetocompare;
389 var left = webForm.Convert (ctrl_value, DataType, validator);
391 webForm.ValidatorFailed (validator);
395 var right = compare != null ? webForm.Convert (compare, DataType, validator) : null;
397 webForm.ValidatorSucceeded (validator);
403 if (Operator == "equal") {
404 result = (left == right);
406 else if (Operator == "notequal") {
407 result = (left != right);
409 else if (Operator == "lessthan") {
410 result = (left < right);
412 else if (Operator == "lessthanequal") {
413 result = (left <= right);
415 else if (Operator == "greaterthan") {
416 result = (left > right);
418 else if (Operator == "greaterthanequal") {
419 result = (left >= right);
422 if (result == false) {
423 webForm.ValidatorFailed (validator);
427 webForm.ValidatorSucceeded (validator);
432 webForm.RangeValidatorEvaluateIsValid = function (validator)
434 var ControlToValidate = validator.controltovalidate;
435 var DataType = validator.type;
437 var ctrl_value = webForm.ValidatorTrim (webForm.ValidatorGetValue (ControlToValidate));
439 if (ctrl_value == "") {
440 webForm.ValidatorSucceeded (validator);
444 var MinimumValue = webForm.Convert (validator.minimumvalue, DataType, validator);
445 var MaximumValue = webForm.Convert (validator.maximumvalue, DataType, validator);
446 var val = webForm.Convert (ctrl_value, DataType, validator);
447 if (val == null || val < MinimumValue || val > MaximumValue) {
448 webForm.ValidatorFailed (validator);
452 webForm.ValidatorSucceeded (validator);
457 webForm.RegularExpressionValidatorEvaluateIsValid = function (validator)
459 var ValidationExpression = validator.validationexpression;
460 var ControlToValidate = validator.controltovalidate;
462 var ctrl_value = webForm.ValidatorTrim (webForm.ValidatorGetValue (ControlToValidate));
464 if (ctrl_value == "") {
465 webForm.ValidatorSucceeded (validator);
469 if (!webForm.HaveRegexp ())
472 var r = new RegExp (ValidationExpression);
473 match = r.exec (ctrl_value);
474 if (match == null || match[0] == "") {
475 webForm.ValidatorFailed (validator);
479 webForm.ValidatorSucceeded (validator);
484 webForm.RequiredFieldValidatorEvaluateIsValid = function (validator)
486 var InitialValue = validator.initialvalue;
487 var ControlToValidate = validator.controltovalidate;
489 var ctrl_value = webForm.ValidatorTrim (webForm.ValidatorGetValue (ControlToValidate));
491 if (ctrl_value == webForm.ValidatorTrim (InitialValue)) {
492 webForm.ValidatorFailed (validator);
496 webForm.ValidatorSucceeded (validator);
501 webForm.CustomValidatorEvaluateIsValid = function (validator)
503 var ControlToValidate = validator.controltovalidate;
505 if (!ControlToValidate) {
506 webForm.ValidatorSucceeded (validator);
510 var evaluationfunc = validator.clientvalidationfunction;
512 var ctrl_value = webForm.ValidatorTrim (webForm.ValidatorGetValue (ControlToValidate));
514 if ((ctrl_value.length == 0) && ((typeof(validator.validateemptytext) != "string") || (validator.validateemptytext != "true"))) {
515 webForm.ValidatorSucceeded (validator);
521 if (evaluationfunc && evaluationfunc != "") {
522 args = {Value:ctrl_value, IsValid:true};
523 eval (evaluationfunc + "(validator, args)");
524 result = args.IsValid;
528 webForm.ValidatorSucceeded (validator);
532 webForm.ValidatorFailed (validator);
537 /*********************/
538 /* utility functions */
540 webForm.Convert = function (s, ty, validator)
542 var cvt = this ["To" + ty];
543 if (typeof (cvt) == 'function')
544 return cvt.call (this, s, validator);
549 webForm.ValidatorUpdateDisplay = function (v, valid)
551 var display = v.display;
553 /* for validators that aren't displayed, do nothing */
554 if (display == "None") {
558 v.style.visibility = (valid ? "hidden" : "visible");
559 if (display == "Dynamic") {
560 v.style.display = (valid ? "none" : "inline");
564 webForm.ValidatorFailed = function (v)
566 webForm.ValidatorUpdateDisplay (v, false);
569 webForm.ValidatorSucceeded = function (v)
571 webForm.ValidatorUpdateDisplay (v, true);
574 webForm.GetElement = function (id)
576 var x = document.getElementById ? document.getElementById (id) :
577 ((document.all) ? document.all [id] : null);