function Validator(frm) { this.form = frm; this.fields = {}; this.beforeSubmitHandler = null; Validator.registerValidator(this); } Validator.validators = []; OOUtils.extend(Validator, { doValidation: function() { for (var valInd in Validator.validators) { if (Validator.validators[valInd].form == this) { return Validator.validators[valInd].validate(); } } }, registerValidator: function(val) { val.form.onsubmit = Validator.doValidation; Validator.validators.push(val); } }); Validator.prototype = { validate: function() { for (var fieldName in this.fields) { var field = this.fields[fieldName].field; for (var valInd in this.fields[fieldName].validators) { if (!this.fields[fieldName].validators[valInd].validate(field)) { alert(FieldValidator.getFieldName(field) + " requires " + this.fields[fieldName].validators[valInd].requirement); field.select(); return false; } } } if (this.beforeSubmitHandler) { return this.beforeSubmitHandler(this); } }, addFieldValidator: function(field, val) { if (!this.fields[field.id]) { this.fields[field.id] = {field: field, validators: []}; } this.fields[field.id].validators.push(val); }, setBeforeSubmitHandler: function(handler) { this.beforeSubmitHandler = handler; }, getField: function(fieldObj) { if (typeof(fieldObj) != "string") { fieldObj = fieldObj.id; } return this.fields[fieldObj]; }, getValidator: function(fieldObj, validatorType) { for (var validator in fieldObj.validators) { var currentValidator = fieldObj.validators[validator]; if (currentValidator instanceof validatorType) { return currentValidator; } } return null; } }; function FieldValidator() { } OOUtils.extend(FieldValidator, { getText: function(node) { if (node.title) { return node.title; } if (node.data) { return node.data; } var text = ""; for (var child in node.childNodes) { text += FieldValidator.getText(node.childNodes[child]); } return text; }, getFieldName: function(field) { var fieldName = "A field"; var labels = document.getElementsByTagName("label"); for (var labelInd in labels) { if (labels[labelInd].htmlFor == field.id) { fieldName = labels[labelInd].title || FieldValidator.getText(labels[labelInd]); break; } } return fieldName; } }); function REValidator() { } REValidator.prototype = { requirement: "any value", re: /.*/, validate: function(field) { var value = field.value; if (!this.re.test(value.trim())) { return false; } return true; } } function MandatoryValidator(childValidator) { if (childValidator) { this.requirement = childValidator.requirement; this.childValidator = childValidator; } } MandatoryValidator.prototype = new REValidator(); OOUtils.extend(MandatoryValidator.prototype, { requirement: "a value", re: /^.+$/, validate: function(field) { if (!REValidator.prototype.validate.apply(this, arguments)) { return false; } if (this.childValidator) { return this.childValidator.validate(field); } return true; } }); function IntegerValidator() { } IntegerValidator.prototype = new REValidator(); OOUtils.extend(IntegerValidator.prototype, { requirement: "an integer value", re: /^\-?\d*$/ }); function RealValidator() { } RealValidator.prototype = new REValidator(); OOUtils.extend(RealValidator.prototype, { requirement: "a real value", re: /^\d*(\.\d+)?$/ }); function DateValidator() { this.validatedDate = null; } DateValidator.prototype = new FieldValidator(); OOUtils.extend(DateValidator.prototype, { validate: function(field) { var dateObj; this.validatedDate = null; if (field.value.trim() == "") { return true; } try { dateObj = DateValidator.parse(field.value); } catch (e) { return false; } this.validatedDate = dateObj; return true; }, requirement: "a valid date" }); DateValidator.parse = function(dateStr) { var fullDateRE = /(\d+|\D{3,9})[\-\\\/\.](\d+|\D{3,9})[\-\\\/\.](\d+|\D{3,9})/; dateStr = dateStr.replace(/\s/g, ""); if (!re.test(dateStr)) throw "' " + dateStr + " ' is not a valid date string"; var foundMonth = 0; var foundYear = 0; var foundDay = 0; var matches = re.exec(dateStr); var monthTextRe = /\D{3,9}/; for (var x = 1; x <= 3; x++) { if (monthTextRe.test(matches[x])) { if (foundMonth != 0) throw "' " + dateStr + " ' is not a valid date string"; matches[x] = this.getMonthFromStr(matches[x]); if (!matches[x]) throw "' " + dateStr + " ' is not a valid date string"; foundMonth = x; } else { var num = new Number(matches[x]); if (num > 31 || num < 1) { if (foundYear != 0) throw "' " + dateStr + " ' is not a valid date string"; foundYear = x; } else { if (num > 12) { if (foundDay != 0) throw "' " + dateStr + " ' is not a valid date string"; foundDay = x; } } } } if (foundYear != 0 && foundMonth != 0) { foundDay = (6 - (foundYear + foundMonth)); } else { if (foundYear != 0 && foundDay != 0) { foundMonth = (6 - (foundYear + foundDay)); } else { if (foundMonth != 0 && foundDay != 0) { foundYear = (6 - (foundMonth + foundDay)); } } } if (foundMonth == 0 && foundYear == 0 && foundDay == 0) { foundDay = 1; foundMonth = 2; foundYear = 3; } if (foundMonth == 0 || foundYear == 0 || foundDay == 0) { if (foundMonth != 0) { switch (foundMonth) { case 3 : throw "' " + dateStr + " ' is not a valid date string"; case 2 : foundDay = 1; foundYear = 3; break; case 1 : foundDay = 2; foundYear = 3; break; } } if (foundYear != 0) { switch (foundYear) { case 3 : foundDay = 1; foundMonth = 2; break; case 2 : throw "' " + dateStr + " ' is not a valid date string"; case 1 : foundMonth = 2; foundDay = 3; break; } } if (foundDay != 0) { switch (foundDay) { case 3 : foundYear = 1; foundMonth = 2; break; case 2 : throw "' " + dateStr + " ' is not a valid date string"; case 1 : foundMonth = 2; foundYear = 3; break; } } } var year, month, day; year = Number(matches[foundYear]); month = Number(matches[foundMonth]); day = Number(matches[foundDay]); var date = new Date(year, month - 1, day); return date; } DateValidator.getMonthFromStr = function(str) { var names = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]; for (var month in names) { if (str.toLowerCase() == names[month] || names[month].indexOf(str.toLowerCase()) == 0) { return new Number(month) + 1; } } return false; } function TimeValidator() { this.validatedDate = null; } TimeValidator.prototype = new FieldValidator(); TimeValidator.prototype.requirement = "a valid time"; TimeValidator.prototype.validate = function(field) { var dateObj; this.validatedDate = null; if (field.value.trim() == "") { return true; } try { dateObj = TimeValidator.parse(field.value); } catch (e) { return false; } this.validatedDate = dateObj; return true; } TimeValidator.parse = function(timeStr) { var re = /^(\d{1,2})(:(\d{2}))?(:(\d{2}))?(AM|PM)?$/i; timeStr = timeStr.replace(/\s/g, ""); if (!re.test(timeStr)) { throw "'" + timeStr + "' is not a valid time string"; } var matches = re.exec(timeStr); var hours = Number(matches[1]); var minutes = 0; var seconds = 0; var noonRelation = null; if (matches[2] != null) { minutes = Number(matches[3]); } if (matches[4] != null) { seconds = Number(matches[5]); } if (matches[6] != null) { noonRelation = matches[6]; } if (noonRelation != null) { if (hours > 12 || hours < 1) { throw "'" + timeStr + "' is not a valid time string"; } hours %= 12; if (noonRelation.toLowerCase() == "pm") { hours += 12; } } if (minutes > 59 || seconds > 59 || hours > 23) { throw "'" + timeStr + "' is not a valid time string"; } var date = new Date(0); date.setSeconds(seconds); date.setMinutes(minutes); date.setHours(hours); return date; } function DateTimeValidator() { this.validateDate = null; }