var blankText = "."; function supportsJava() { // If getElementById isn't supported, there's no java // and then validation is server-side only if (!document.getElementById) return false; return true; } function valBasic(field, errorTextId, required) { // Don't try to do any more on non-java enabled systems if (!supportsJava()) return true; /* Get text, if it's blank and not required, stop. */ var fieldText = field.value; if (fieldText == "" && !required) { var textElement = document.getElementById(errorTextId); textElement.firstChild.nodeValue = blankText; return true; } if (fieldText == "") { var textElement = document.getElementById(errorTextId); textElement.firstChild.nodeValue = "Field cannot be blank."; return false; } // Look for invalid characters var i = 0; for (i = 0; i < fieldText.length; i++) { if (fieldText.charAt(i) == '\\' || fieldText.charAt(i) == '/' || fieldText.charAt(i) == '\"' || fieldText.charAt(i) == '\'' || fieldText.charAt(i) == ':' || fieldText.charAt(i) == '<' || fieldText.charAt(i) == '>') { var textElement = document.getElementById(errorTextId); textElement.firstChild.nodeValue = "Invalid characters used."; return false; } } var textElement = document.getElementById(errorTextId); textElement.firstChild.nodeValue = blankText; return true; } function valAlpha(field, errorTextId, required) { // Don't try to do any more on non-java enabled systems if (!supportsJava()) return true; /* Get text, if it's blank and not required, stop. */ var fieldText = field.value; if (fieldText == "" && !required) { var textElement = document.getElementById(errorTextId); textElement.firstChild.nodeValue = blankText; return true; } // Run basic checks if (!valBasic(field, errorTextId, required)) return false; // Run alpha checks var i = 0, lowerText = fieldText.toLowerCase(); for (i = 0; i < lowerText.length; i++) { if (lowerText.charAt(i) < 'a' || lowerText.charAt(i) > 'z') { var textElement = document.getElementById(errorTextId); textElement.firstChild.nodeValue = "Field must only be 'a' to 'z'."; return false; } } var textElement = document.getElementById(errorTextId); textElement.firstChild.nodeValue = blankText; return true; } function valPassword(field, compare, errorTextId, required) { // Don't try to do any more on non-java enabled systems if (!supportsJava()) return true; /* Get text, if it's blank and not required, stop. */ var fieldText = field.value; if (fieldText == "" && !required) return true; var compareText = compare.value; // Run basic checks if (!valBasic(field, errorTextId)) return false; if (fieldText != compareText) { var textElement = document.getElementById(errorTextId); textElement.firstChild.nodeValue = "Password fields do not match."; return false; } var textElement = document.getElementById(errorTextId); textElement.firstChild.nodeValue = blankText; return true; } function valEmail(field, errorTextId, required) { // Don't try to do any more on non-java enabled systems if (!supportsJava()) return true; /* Get text, if it's blank and not required, stop. */ var fieldText = field.value; if (fieldText == "" && !required) { var textElement = document.getElementById(errorTextId); textElement.firstChild.nodeValue = blankText; return true; } // Run basic checks if (!valBasic(field, errorTextId)) return false; var foundAt = false, foundDot = false, valid = true, i = 0; for (i = 0; i < fieldText.length; i++) { if (fieldText.charAt(i) == '@') { if (foundAt) { valid = false; break; } foundAt = true; } if (fieldText.charAt(i) == '.') foundDot = true; } if (!valid || !foundAt || !foundDot) { var textElement = document.getElementById(errorTextId); textElement.firstChild.nodeValue = "Must be of the form '[user]@[domain].[type]'"; return false; } var textElement = document.getElementById(errorTextId); textElement.firstChild.nodeValue = blankText; return true; }