function validate(varFormName, varDelimitedString) /* varFormName Name of form in which elements are to be validated varDelimetedString A comma-space seperated string of form elements in the form: formelementname_typeofvalidation.displayname +formelement name being the name of the element as given in the form +displayname being the name that is displayed to the user on invalid +typeofvalidation being the type of validation (types currently supported givenbelow). TYPES OF VALIDATION SUPPORTED isPresent - checks for a value being present isEmail - checks for @ symbol and last . , and ensures the last . is after the @. isNumber - checks all strings content (except spaces are numeric. isSelected - checks whether a has been selected/checked */ { //Start Function /* -------------------------------------------------------------------------------- CONSTANTS -------------------------------------------------------------------------------- WARNINGS warningHeader - top header startOfWarning - displayed at lhs of each individual error _ _ _ WarningStart - displayed at start of that individual warning type _ _ _ WarningEnd - " " end " " " " -------------------------------------------------------------------------------- */ var warningHeader = "The following errors occured:\n\n" var startOfWarning = " + " var incompleteWarningStart = "" var incompleteWarningEnd = " must be completed\n" var invalidEmailWarningStart = "" var invalidEmailWarningEnd = " is not a valid email address\n" var invalidNumberWarningStart = "" var invalidNumberWarningEnd = " is not a valid number \n" var invalidCheckWarningStart = " You must choose a value for " var invalidCheckWarningEnd = "\n" var displayIncomplete = "" var displayInvalid = "" /* -------------------------------------------------------------------------------- */ var form = document[varFormName] arrFormElements = varDelimitedString.split(', ') for(var i = 0; i < arrFormElements.length; i++) { var element = arrFormElements[i] var positionStop = element.indexOf(".") var positionUnderScore = element.indexOf("_") var elementName = element.substr(0, positionUnderScore) var validateCondition = element.substring(element.indexOf("_") + 1, positionStop) var displayName = element.substring(positionStop + 1) var formItem = form.elements[elementName] var elementValue = formItem.value switch(validateCondition) { case "isPresent" : { if (elementValue=="") { displayIncomplete += startOfWarning + incompleteWarningStart + displayName +incompleteWarningEnd ; break ; } else { break ; } } case "isSelected" : { var notSelected = true for (var j=0; j < formItem.length; j++) { if (formItem[j].checked) { notSelected = false ; } } if (notSelected) { displayInvalid += startOfWarning + invalidCheckWarningStart + displayName + invalidCheckWarningEnd ; break ; } else { break ; } } case "isEmail" : { if (elementValue=="") { displayIncomplete += startOfWarning + incompleteWarningStart + displayName + incompleteWarningEnd ; break ; } else { var locationAtSymbol = elementValue.indexOf("@") var locationLastFullStop = elementValue.lastIndexOf(".") if (locationLastFullStop <= locationAtSymbol) { displayInvalid += startOfWarning + invalidEmailWarningStart + "'" + elementValue + "'" + invalidEmailWarningEnd ; break ; } else { break ; } } } case "isNumber" : { var noSpaces=elementValue.replace(" ", "") ; if (elementValue=="") { displayIncomplete += startOfWarning + incompleteWarningStart + displayName + incompleteWarningEnd ; break ; } else { while (noSpaces.indexOf(" ") > -1) { noSpaces=noSpaces.replace(" ", "") ; } if (isNaN(noSpaces)) { displayInvalid += startOfWarning + invalidNumberWarningStart + "'" + noSpaces + "'" + invalidNumberWarningEnd ; break ; } } } //End case } //End switch } //End for if (displayIncomplete.length + displayInvalid.length != 0) { alert(warningHeader + displayIncomplete + "\n" + displayInvalid) ; return false ; } else { return true ; } } //End Function