//function form_onsubmit(f) { //----------------------------------------------------------------------------------------- // set the properties used by the verify function // include on form tag: onSubmit="return form_onsubmit(this);" //----------------------------------------------------------------------------------------- // if (f.method.value == "Cancel") return true; // f.agencyaddr2.optional = true; // f.workfax.optional = true; // f.workemail.optional = true; // f.agencypostalcode.min = 1; // f.agencypostalcode.max = 99999; // return verify(f); //} function isBlank(s) { //----------------------------------------------------------------------------------------- // a utility function that returns true if a string contains only whitespace characters. //----------------------------------------------------------------------------------------- for (var i = 0; i < s.length; i++) { var c = s.charAt(i); if ((c != ' ') && (c != '\n') && (c != '\t')) return false; } return true; } function verify(f) { //----------------------------------------------------------------------------------------- // This function performs form verification. Invoke it from the onSubmit() event handler. // The handler should return whatever value this function returns. // Taken from David Flanagan's JavaScript: the Definitive Guide, pp 311-314. //----------------------------------------------------------------------------------------- // who when what //----------------------------------------------------------------------------------------- // djohnson 2000-02-09 add password to the types checked. //----------------------------------------------------------------------------------------- var msg; var empty_fields = ""; var errors = ""; // loop thru all the elements in the form for (var i = 0; i < f.length; i++) { var e = f.elements[i]; // is it text or textarea or password and not optional? if (((e.type == "text") || (e.type == "textarea") || (e.type == "password")) && !e.optional) { // is the field empty? if ((e.value == null) || (e.value == "") || isBlank(e.value)) { empty_fields += "\n " + e.name; continue; } // is numeric field really numeric, and in the desired range? if (e.numeric || (e.min != null) || (e.max != null)) { var v = parseFloat(e.value); if (isNaN(v) || ((e.min != null) && (v < e.min)) || ((e.max != null) && (v > e.max))) { errors += "- The field " + e.name + " must be a number"; if (e.min != null) errors += " that is greater than " + e.min; if (e.max != null && e.min != null) errors += " and less than " + e.max; else if (e.max != null) errors += " that is less than " + e.max; errors += ".\n"; } } } } // display any error messages if (!empty_fields && !errors) return true; msg = "______________________________________________\n\n"; msg += "The form was not submitted because some fields are required. \n"; // msg += "Please add missing data and re-submit. \n"; msg += "______________________________________________\n\n"; if (empty_fields) { msg += " - The following required fields are empty: " + empty_fields + "\n"; if (errors) msg += "\n"; } msg += errors; alert(msg); return false; }