//// FORM VALIDATE OBJECT //// // INCLUDES //// FORM CLASS //// // INCLUDES //// CORESCRIPT //// // CONSTRUCTOR function CoreScript () { } // PROPERTIES // METHODS CoreScript.prototype.stripCharacters = function (str, characters) { var i; var result = ''; var strLength = str.length; for (i = 0; i < strLength; i++) { var character = str.charAt(i); if (characters.indexOf(character) == -1) { result += character; } } return result; } CoreScript.prototype.isInteger = function (str) { var i; for (i = 0; i < str.length; i++) { var character = str.charAt(i); if (character < '0' || character > '9') { return false; } } return true; } CoreScript.prototype.popUpWindow = function (url, width, height) { day = new Date(); id = day.getTime(); eval("page" + id + " = window.open('" + url + "', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=" + width + ",height=" + height + ",left = 0,top = 0');"); } // CONSTRUCTOR function Form () { } // EXTEND Form.prototype = new CoreScript; // PROPERTIES // METHODS Form.prototype.setFormByFieldName = function (fieldName) { this.fieldName = fieldName; this.forms = document.getElementsByTagName('form'); if(this.forms.length > 0) { for(var i = 0; i < this.forms.length; i++) { inputs = this.forms[i].getElementsByTagName('input'); for(var j = 0; j < inputs.length; j++) { if (inputs[j].name == fieldName) { this.formName = this.forms[i].name; this.form = document[this.formName]; this.formLength = this.form.length; this.field = inputs[j]; } } } } else { alert('Couldn\'t find any forms on this page.'); return false; } } // CONSTRUCTOR function FormValidate () { } // EXTEND FormValidate.prototype = new Form; // PROPERTIES FormValidate.prototype.returnStatus = 'true'; FormValidate.prototype.alertMessage = ''; // METHODS FormValidate.prototype.attach = function (submitName) { this.setFormByFieldName(submitName); this.field.onclick = this.processFields; } FormValidate.prototype.processFields = function () { formValidate.alertMessage = ''; for (var i = 0; i < formValidate.formLength; i++) { field = formValidate.form[i]; //alert(field.alt); if (field.alt) { splitAlt = field.alt.split('|'); fn = splitAlt[0]; if (splitAlt[1]) { if (splitAlt[1] == 'false') { formValidate.required = false; } else { formValidate.blankIgnoreString = splitAlt[1]; formValidate.required = true; } } else { formValidate.required = true; } formValidate[fn](field); } else if (field.type == 'textarea' && field.title) { formValidate.textarea(field); } else if (field.type == 'select-one' && field.title) { formValidate.select(field); } } formValidate.displayErrors(); return formValidate.returnStatus; } FormValidate.prototype.isRequired = function (field) { if (this.required == true) { if (this.blank(field) == false) { return false; } } } FormValidate.prototype.lightbox = function (field) { return true; } FormValidate.prototype.select = function (field) { selectGroup = this.form[field.name]; for (var i = 0; i < selectGroup.options.length; i++) { if (selectGroup.options[i].selected == true && selectGroup.options[i].value.length > 0) { if (field.name == 'CreditCardType') { this.creditCardType = selectGroup.options[i].value; } return true; } } this.alertMessage += 'You need to select ' + field.title + "\n"; return false; } FormValidate.prototype.textarea = function (field) { if (field['value'].length <= 0) { this.alertMessage += field.title + " is a required field\n"; return false; } } FormValidate.prototype.checkbox = function (field) { if (field.checked != true) { this.alertMessage += 'Please check ' + field.title + "\n"; return false; } } FormValidate.prototype.file = function () { } FormValidate.prototype.radio = function (field) { radioGroup = this.form[field.name]; for (var i = 0; i < radioGroup.length; i++) { if (radioGroup[i].checked == true) { return true; } } this.alertMessage += 'You need to select an option for ' + field.title + "\n"; return false; } FormValidate.prototype.blank = function (field) { if (field['value'].length <= 0) { this.alertMessage += field['title'] + " is a required field\n"; return false; } else if (this.blankIgnoreString) { if (this.blankIgnoreString == field['value']) { this.alertMessage += field['title'] + " is a required field\n"; return false; } } } FormValidate.prototype.email = function (field) { this.isRequired(field); if (field['value'].length > 0) { var emailRegExp = /^.+@.+\..{2,3}$/; var re = new RegExp(emailRegExp); if (field['value'].match(re)) { return true; } else { this.alertMessage += field['value'] + " is not a valid email\n"; return false; } } } FormValidate.prototype.url = function (field) { this.isRequired(field); if (field['value'].length > 0) { var urlRegExp = /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/; var re = new RegExp(urlRegExp); if (field['value'].match(re)) { return true; } else { this.alertMessage += field['value'] + " is not a valid url\n"; return false; } } } FormValidate.prototype.phone = function (field) { this.isRequired(field); if (field['value'].length > 0) { var digits = '0123456789'; var delimiters = '()- .+'; var minLength = 10; var phone = field['value']; phoneStripped = this.stripCharacters(phone, delimiters); phoneIsInteger = this.isInteger(phoneStripped); if (phoneIsInteger == false) { this.alertMessage += field['title'] + " contains invalid characters\n"; return false; } if (phoneStripped.length != minLength) { this.alertMessage += phone + " is not a valid phone number\n"; return false; } } } FormValidate.prototype.number = function (field) { this.isRequired(field); if (field['value'].length > 0) { var digits = '0123456789'; var number = field['value']; numberIsInteger = this.isInteger(number); if (numberIsInteger == false) { this.alertMessage += field['title'] + " contains invalid characters\n"; return false; } } } FormValidate.prototype.other = function (field) { if (field['value'].length > 0) { var digits = '0123456789'; var delimiters = '()- .+'; var minLength = 10; var other = field['value']; otherStripped = this.stripCharacters(other, delimiters); otherIsInteger = this.isInteger(otherStripped); if (otherIsInteger == false) { this.alertMessage += field['title'] + " contains invalid characters\n"; return false; } if (otherStripped.length != minLength) { this.alertMessage += other + " is not a valid other number\n"; return false; } } } FormValidate.prototype.fax = function (field) { if (field['value'].length > 0) { var digits = '0123456789'; var delimiters = '()- .+'; var minLength = 10; var fax = field['value']; faxStripped = this.stripCharacters(fax, delimiters); faxIsInteger = this.isInteger(faxStripped); if (faxIsInteger == false) { this.alertMessage += field['title'] + " contains invalid characters\n"; return false; } if (faxStripped.length != minLength) { this.alertMessage += fax + " is not a valid fax number\n"; return false; } } } FormValidate.prototype.zip = function (field) { this.isRequired(field); if (field['value'].length > 0) { // Canada var postalCodeRegExp = /^\s*[a-ceghj-npr-tvxy]\d[a-z](\s)?\d[a-z]\d\s*$/i; var postalCodeRE = new RegExp(postalCodeRegExp); // USA var zipRegExp = /(^\d{5}$)|(^\d{5}-\d{4}$)/; var zipRE = new RegExp(zipRegExp); if (field['value'].match(zipRE) || field['value'].match(postalCodeRE)) { return true; } else { this.alertMessage += field['value'] + " is not a valid postal code\n"; return false; } } } FormValidate.prototype.ccNumber = function (field) { this.creditCardNumber = field['value']; status = formCreditCard.checkCreditCard(field['value'], this.creditCardType); if (status != 'true') { this.alertMessage += status + "\n"; return false; } return true; } FormValidate.prototype.displayErrors = function () { if (this.alertMessage.length > 0) { alert(this.alertMessage); this.returnStatus = false; return false; } this.returnStatus = true; } FormValidate.prototype.enableDisable = function (formName) { disableId = document.getElementById('disable'); disableBox = document[formName].disable; inputFields = disableId.getElementsByTagName('input'); selectFields = disableId.getElementsByTagName('select'); textareaFields = disableId.getElementsByTagName('textarea'); for (var i = 0; i < inputFields.length; i++) { if (inputFields[i].name != disableBox.name) { inputFields[i].readonly = disableBox.checked; } } for (var i = 0; i < selectFields.length; i++) { selectFields[i].readonly = disableBox.checked; } for (var i = 0; i < textareaFields.length; i++) { textareaFields[i].readonly = disableBox.checked; } }