// invoke from onSubmit() event handler function verify(form) { var msg = "Your form could not be submitted due to the following:\n\n"; var errors = ""; for(var i = 0; i < form.length; i++) { var e = form.elements[i]; if((e.type == "text") || (e.type == "textarea")) { if(!e.optional) { // e is a non-optional text field // check if field is empty if(isEmpty(e.value)) { errors += "Required field \"" + e.description + "\" is empty\n"; continue; } } // check for fields that are supposed to be numeric if(((e.numeric) || (e.min != null) || (e.max != null)) && (!isBlank(e.value))) { var v = parseFloat(e.value); if(isNaN(v) || ((e.min != null) && (v < e.min)) || ((e.max != null) && (v > e.max))) { errors += "The field \"" + e.description + "\" must be a number"; if(e.min != null) errors += " greater than " + e.min; if(e.max != null && e.min != null) errors += " and less than " + e.max; else if(e.max != null) errors += " less than " + e.max; errors += "\n"; } } // check for fields that are supposed to be email addresses if((e.email) && (!isBlank(e.value))) { if(!isEmail(e.value)) { errors += "The field \"" + e.description + "\" must be an email address\n"; } } } // end if text field } // end for(form.length) if(!errors) { return true; } else { alert(msg + errors); return false; } } // utility function; returns true if string contains only whitespace characters function isBlank(s) { for(var i = 0; i < s.length; i++) { var c = s.charAt(i); if((c != ' ') && (c != '\n') && (c != '\t')) return false; } return true; } // utility function; returns true if input value is empty function isEmpty(v) { if((v == null) || (v == "") || isBlank(v)) return true; else return false; } // utility function; returns true is string has format x@* function isEmail(s) { for(var i = 1; i < s.length; i++) { var c = s.charAt(i); if(c == '@') return true; } return false; }