// Fieldvalidator version 1.5 // Noonday function FieldValidator() { var foundError = false; var theForm; var errorMessage = ''; this.validateForm = validateForm; this.highlightField = highlightField; this.validateRadio = validateRadio; this.validateEmpty = validateEmpty; this.validatePassword = validatePassword; this.validateEmail = validateEmail; this.validateIsChecked = validateIsChecked; this.validateLength = validateLength; this.validateValue = validateValue; this.getErrorMessage = getErrorMessage; function validateForm(e){ element = getElementFromE(e); targetElement = getTargetElementFromE(e); theForm = targetElement; // Warning! foundError = false; errorMessage = 'The form is not filled out correct:'; //alert(fields[theForm.id].length); for (var i = 0; i < fields[theForm.id].length; i++) { //alert('detta händer! '+fields[theForm.id][i][1]); highlightField(fields[theForm.id][i][0], theForm[fields[theForm.id][i][1]], fields[theForm.id][i][2], fields[theForm.id][i][3]); } if(foundError){ if(e && e.target){ element.stopPropagation(); element.preventDefault(); } else if(window.event && window.event.srcElement){ element.cancelBubble = true; element.returnValue = false; } alert(errorMessage); return false; } else{ return true; } } function highlightField(fieldErrorMessage,field,validationFunction, property){ var valid = false; switch(validationFunction) { case "validateRadio": if (validateRadio(field)) { valid = true; } break; case "validatePassword": if (eval(validationFunction + '(field.value, theForm["' + property + '"].value);')) { valid = true; } break; default: if (eval(validationFunction + '(field.value, property);')) { valid = true; } break; } if (valid) { //field.className = ''; } else{ errorMessage += "\n\n- "+fieldErrorMessage; foundError = true; //field.className = "highlighted"; } } function validateRadio(field) { var radio_choice = false; for (counter = 0; counter < field.length; counter++) { if (field[counter].checked) { radio_choice = true; } } return radio_choice; } function validateNumeric(sText) { var ValidChars = "0123456789"; var IsNumber=true; var Char; for (i = 0; i < sText.length && IsNumber == true; i++) { Char = sText.charAt(i); if (ValidChars.indexOf(Char) == -1) { IsNumber = false; } } return IsNumber; } function validateEmpty(value) { return value && value.length > 0 } function validateLength(value, len) { return eval("value.length" + len); } function validatePassword(value, value2) { if(value && value.length >= 4 && value == value2) { return true; } else { return false; } } function validateEmail(value) { if(validateEmpty(value) && value.match(new RegExp('^[^@]+@[^@]+\.[a-zA-Z]{2,6}$'))) { return true; } else { return false; } } function validateIsChecked(checked) { if(checked == true) { return true; } else { return false; } } function validateValue(value, comparison) { return eval("'" + value + "'" + comparison); } function getErrorMessage() { return this.errorMessage; } } function isEmpty(aTextField) { if ((aTextField.value.length==0) || (aTextField.value==null)) { return true; } else { return false; } }