// SCRIPT FOR SUPPORT FORM VALIDATION var defaultEmptyOK = false; var longComment = false; var longCommentWord = false; // CONSTANT STRING DECLARATIONS // (grouped for ease of translation and localization) // m is an abbreviation for "missing" var mPrefix = "Please fill in the field \""; var mSuffix = "\" . This will help us better serve you. Thank you!"; var iPrefix = "Please fill in a valid \"" var iEmailSuffix = "\" (such as info@jpmorgan.com). Thank you!"; var iAlphanumericSuffix = "\" must contain a valid alphanumeric string. Please reenter it."; var iAlphabeticSuffix = "\" must contain a valid alphabetic string. Please reenter it."; var iNumberSuffix = "\" must contain a valid number. Please reenter it."; var iSelectPrefix = "You have not filled one of the required fields. \nPlease select one of the options for the item named \""; var iSelectSuffix = "\""; var iRadioPrefix = "You have not filled one of the required fields. \nPlease select one of the buttons for the item named \""; var iRadioSuffix = "\""; var iCommentDefaultSuffix = "\" cannot have words that exceed 50 characters. Please reenter it"; var iCommentWordSuffix = "\" cannot have words that exceed 50 characters. Please reenter it"; var iCommentSuffix = "\" cannot exceed 2500 characters. Please reenter it"; // regular expressions used for checking fields // Email Address // BOI, followed by one or more characters, followed by @, // followed by one or more characters, followed by ., // followed by one or more characters, followed by EOI. var reEmail = /^.+\@.+\..+$/; // BOI, followed by one or more whitespace characters, followed by EOI. var reWhitespace = /^\s+$/; // Alphanumeric fields // BOI, followed by one or more lower or uppercase English letters // or digits, followed by EOI. var reAlphanumeric = /^[a-zA-Z0-9 ]+$/; // BOI, followed by one or more digits, followed by EOI. var reInteger = /^[\d ]+$/; // BOI, followed by one or more lower or uppercase English letters, // followed by EOI. var reAlphabetic = /^[a-zA-Z ]+$/; // Check whether string s is empty. function isEmpty(s) { return ((s == null) || (s.length == 0)) } // Returns true if string s is empty or // whitespace characters only. function isWhitespace (s) { // Is s empty? return (isEmpty(s) || reWhitespace.test(s)); } // Notify user that required field theField is empty. // String s describes expected contents of theField.value. // Put focus in theField and return false. function warnEmpty (theField, s) { theField.focus(); alert(mPrefix + s + mSuffix); return false; } // Notify user that contents of field theField are invalid. // String s describes expected contents of theField.value. // Put select theField, pu focus in it, and return false. function warnInvalid (theField, s) { theField.focus(); theField.select(); alert(s); return false; } function warnMustFill (theField, s) { theField.focus(); alert(s); return false; } /* FUNCTIONS TO INTERACTIVELY CHECK VARIOUS FIELDS. */ // checkString (TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false]) // // Check that string theField.value is not all whitespace. // // For explanation of optional argument emptyOK, // see comments of function isInteger. function checkString (theField, s, emptyOK) { // Next line is needed on NN3 to avoid "undefined is not a number" error // in equality comparison below. if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK; if ((emptyOK == true) && (isEmpty(theField.value))) return true; if (isWhitespace(theField.value)) return warnEmpty (theField, s); else return true; } // ********************** Email field testing *************** // isEmail (STRING s [, BOOLEAN emptyOK]) // // Email address must be of form a@b.c -- in other words: // * there must be at least one character before the @ // * there must be at least one character before and after the . // * the characters @ and . are both required function isEmail (s) { if (isEmpty(s)) if (isEmail.arguments.length == 1) return defaultEmptyOK; else return (isEmail.arguments[1] == true); else { return reEmail.test(s) } } // isAlphanumeric (STRING s [, BOOLEAN emptyOK]) // // Returns true if string s is English letters // (A .. Z, a..z) and numbers only. // // For explanation of optional argument emptyOK, // see comments of function isInteger. // // NOTE: Need i18n version to support European characters. // This could be tricky due to different character // sets and orderings for various languages and platforms. function isAlphanumeric (s) { var i; if (isEmpty(s)) if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK; else return (isAlphanumeric.arguments[1] == true); else { return reAlphanumeric.test(s) } } // isInteger (STRING s [, BOOLEAN emptyOK]) // // Returns true if all characters in string s are numbers. // // Accepts non-signed integers only. Does not accept floating // point, exponential notation, etc. // // We don't use parseInt because that would accept a string // with trailing non-numeric characters. // // By default, returns defaultEmptyOK if s is empty. // There is an optional second argument called emptyOK. // emptyOK is used to override for a single function call // the default behavior which is specified globally by // defaultEmptyOK. // If emptyOK is false (or any value other than true), // the function will return false if s is empty. // If emptyOK is true, the function will return true if s is empty. // // EXAMPLE FUNCTION CALL: RESULT: // isInteger ("5") true // isInteger ("") defaultEmptyOK // isInteger ("-5") false // isInteger ("", true) true // isInteger ("", false) false // isInteger ("5", false) true function isInteger (s) { var i; if (isEmpty(s)) if (isInteger.arguments.length == 1) return defaultEmptyOK; else return (isInteger.arguments[1] == true); return reInteger.test(s) } // isAlphabetic (STRING s [, BOOLEAN emptyOK]) // // Returns true if string s is English letters // (A .. Z, a..z) only. // function isAlphabetic (s) { var i; if (isEmpty(s)) if (isAlphabetic.arguments.length == 1) return defaultEmptyOK; else return (isAlphabetic.arguments[1] == true); else { return reAlphabetic.test(s) } } //isComment(STRING s[, BOOLEAN emptyOK]) //Returns true if field contains less than 2500 characters, and each word is less than 50 characters function isComment(s) { var i; if (isEmpty(s)) if (isComments.arguments.length == 1) return defaultEmptyOK; else return (isComments.arguments[1] == true); else { //Check the description length before submiting the form if (s./*value.*/length > 2500) { window.alert("Your comments are " + s.length + " characters long, please shortened these to less than 2500 characters."); iCommentDefaultSuffix = iCommentSuffix; return false; } //Check the word length before submitting words = s./*value.*/split(' '); for (var loop = 0; loop <= words.length - 1; ++loop) { if (words[loop].length >= 50) { window.alert("A word in your comments contains " + words[loop].length + " characters, this needs to be shortened to less than 50 characters."); iCommentDefaultSuffix = iCommentWordSuffix; return false; } } return true; } } // checkEmail (TEXTFIELD theField [, BOOLEAN emptyOK==false]) // // Check that string theField.value is a valid Email. // function checkEmail (theField, message, emptyOK) { if (checkEmail.arguments.length == 2) emptyOK = defaultEmptyOK; if ((emptyOK == true) && (isEmpty(theField.value))) return true; else if (!isEmail(theField.value, false)) return warnInvalid (theField, message); else return true; } // check alphanumeric (TEXTFIELD theField [, BOOLEAN emptyOK==false]) // // Check that string theField.value is a valid alphanumeric string. // function checkAlphanumeric (theField, message, emptyOK) { if (checkAlphanumeric.arguments.length == 2) emptyOK = defaultEmptyOK; if ((emptyOK == true) && (isEmpty(theField.value))) return true; else if (!isAlphanumeric(theField.value)) return warnInvalid (theField, message); else return true; } // checkAlphabetic (TEXTFIELD theField [, BOOLEAN emptyOK==false]) // // Check that string theField.value is a valid alphabetic string. // function checkAlphabetic (theField, message, emptyOK) { if (checkAlphabetic.arguments.length == 2) emptyOK = defaultEmptyOK; if ((emptyOK == true) && (isEmpty(theField.value))) return true; else if (!isAlphabetic(theField.value)) return warnInvalid (theField, message); else return true; } // checkNumber (TEXTFIELD theField [, BOOLEAN emptyOK==false]) // // Check that string theField.value is a valid number string. // function checkNumber (theField, message, emptyOK) { if (checkNumber.arguments.length == 2) emptyOK = defaultEmptyOK; if ((emptyOK == true) && (isEmpty(theField.value))) return true; else if (!isInteger(theField.value)) return warnInvalid (theField, message); else return true; } // checkRequiredSelect (SelectField theField) // // Check that option theField.value is filled by user // function checkRequiredSelect(theField, message) { if (theField.options[theField.options.selectedIndex].value == "$$$$$") return warnMustFill(theField, message); else return true; } function checkRequiredRadio(theField, message) { if (!theField.checked) return warnMustFill(theField, message); else return true; } // checkComment (TEXTFIELD theField [, BOOLEAN emptyOK==false]) // // Check that string theField.value is a valid comment. // function checkComment (theField, message, emptyOK) { if (checkComment.arguments.length == 2) emptyOK = defaultEmptyOK; if ((emptyOK == true) && (isEmpty(theField.value))) return true; else if (!isComment(theField.value)) { //return warnInvalid (theField, message); } else return true; } // check form element values before submitting // if all fields look good return true so that the form can be submitted function CheckForm(theForm, fes) { //for (i = 0; i < theForm.elements.length; i++) //alert(isComment(theForm.elements[4].value)); //alert(fes[theForm.elements[i].name].ctype); //alert(fes[theForm.elements[i].name].type); // first check that all "required" fields are filled for (i = 0; i < theForm.elements.length; i++) { if (typeof(fes[theForm.elements[i].name]) == "undefined") { continue; } if (fes[theForm.elements[i].name].required == 1) { if (fes[theForm.elements[i].name].type == "text" || fes[theForm.elements[i].name].type == "textarea") { // if (!checkString(theForm.elements[i], // theForm.elements[i].name, false)) if (!checkString(theForm.elements[i], fes[theForm.elements[i].name].desc, false)) return false; } else if (fes[theForm.elements[i].name].type == "select") { if (!checkRequiredSelect(theForm.elements[i], iSelectPrefix + fes[theForm.elements[i].name].desc+iSelectSuffix)) return false; } else if (fes[theForm.elements[i].name].type == "radio") { var radiochecked = theForm.elements[i].checked; var radioname = theForm.elements[i].name; var j = i+1; while (j < theForm.elements.length) { if (theForm.elements[j].type == "radio" && theForm.elements[j].name == radioname) { if (!radiochecked) radiochecked = theForm.elements[j].checked; j++; } else break; // reached end of radio button sequence } if (!radiochecked) return warnMustFill(theForm.elements[i], iRadioPrefix + fes[theForm.elements[i].name].desc+iRadioSuffix); i = j - 1; } } } // now check the contents of all fields that require tests for (i = 0; i < theForm.elements.length; i++) { if (typeof(fes[theForm.elements[i].name]) == "undefined") { continue; } if (fes[theForm.elements[i].name].ctype == "Alphabetic") { if (!checkAlphabetic(theForm.elements[i], iPrefix + fes[theForm.elements[i].name].desc + iAlphabeticSuffix, (fes[theForm.elements[i].name].required == 1) ? false : true)) return false; } else if (fes[theForm.elements[i].name].ctype == "Alphanumeric") { if (!checkAlphanumeric(theForm.elements[i], iPrefix + fes[theForm.elements[i].name].desc + iAlphanumericSuffix, (fes[theForm.elements[i].name].required == 1) ? false : true)) return false; } else if (fes[theForm.elements[i].name].ctype == "Number") { if (!checkNumber(theForm.elements[i], iPrefix + fes[theForm.elements[i].name].desc + iNumberSuffix, (fes[theForm.elements[i].name].required == 1) ? false : true)) return false; } else if (fes[theForm.elements[i].name].ctype == "Email") { if (!checkEmail(theForm.elements[i], iPrefix + fes[theForm.elements[i].name].desc + iEmailSuffix, (fes[theForm.elements[i].name].required == 1) ? false : true)) return false; } else if(fes[theForm.elements[i].name].ctype == "length") { if (!checkComment(theForm.elements[i], iPrefix + fes[theForm.elements[i].name].desc + iCommentSuffix , (fes[theForm.elements[i].name].required == 1) ? false : true)) {return false;} } else if (fes[theForm.elements[i].name].ctype == "None") { } } //theForm.submitTime.value=new Date(); return true; } function formelement(req, typ, contentt, flddesc) { this.required = req; // is this a required item? this.ctype = contentt; // is content numeric, alphabetic etc. // applies only to text item fields // possible values are "Alphabetic", "Number", // "Alphanumeric", "Email", "None", length this.type = typ; // what is the type of this item // "text", "select", "radio", "textarea" this.desc = flddesc; // descriptive string for item return this; } /* BrowserDetector() Parses User-Agent string into useful info. Source: Webmonkey Code Library (http://www.hotwired.com/webmonkey/javascript/code_library/) Author: Richard Blaylock Author Email: blaylock@wired.com Usage: var bd = new BrowserDetector(navigator.userAgent); */ // Utility function to trim spaces from both ends of a string function Trim(inString) { var retVal = ""; var start = 0; while ((start < inString.length) && (inString.charAt(start) == ' ')) { ++start; } var end = inString.length; while ((end > 0) && (inString.charAt(end - 1) == ' ')) { --end; } retVal = inString.substring(start, end); return retVal; } function BrowserDetector(ua) { // Defaults this.browser = "Unknown"; this.platform = "Unknown"; this.version = ""; this.majorver = ""; this.minorver = ""; uaLen = ua.length; // ##### Split into stuff before parens and stuff in parens var preparens = ""; var parenthesized = ""; i = ua.indexOf("("); if (i >= 0) { preparens = Trim(ua.substring(0,i)); parenthesized = ua.substring(i+1, uaLen); j = parenthesized.indexOf(")"); if (j >= 0) { parenthesized = parenthesized.substring(0, j); } } else { preparens = ua; } // ##### First assume browser and version are in preparens // ##### override later if we find them in the parenthesized stuff var browVer = preparens; var tokens = parenthesized.split(";"); var token = ""; // # Now go through parenthesized tokens for (var i=0; i < tokens.length; i++) { token = Trim(tokens[i]); //## compatible - might want to reset from Netscape if (token == "compatible") { //## One might want to reset browVer to a null string //## here, but instead, we'll assume that if we don't //## find out otherwise, then it really is Mozilla //## (or whatever showed up before the parens). //## browser - try for Opera or IE } else if (token.indexOf("MSIE") >= 0) { browVer = token; } else if (token.indexOf("Opera") >= 0) { browVer = token; } //'## platform - try for X11, SunOS, Win, Mac, PPC else if ((token.indexOf("X11") >= 0) || (token.indexOf("SunOS") >= 0) || (token.indexOf("Linux") >= 0)) { this.platform = "Unix"; } else if (token.indexOf("Win") >= 0) { this.platform = token; } else if ((token.indexOf("Mac") >= 0) || (token.indexOf("PPC") >= 0)) { this.platform = token; } } var msieIndex = browVer.indexOf("MSIE"); if (msieIndex >= 0) { browVer = browVer.substring(msieIndex, browVer.length); } var leftover = ""; if (browVer.substring(0, "Mozilla".length) == "Mozilla") { this.browser = "Netscape"; leftover = browVer.substring("Mozilla".length+1, browVer.length); } else if (browVer.substring(0, "Lynx".length) == "Lynx") { this.browser = "Lynx"; leftover = browVer.substring("Lynx".length+1, browVer.length); } else if (browVer.substring(0, "MSIE".length) == "MSIE") { this.browser = "IE"; leftover = browVer.substring("MSIE".length+1, browVer.length); } else if (browVer.substring(0, "Microsoft Internet Explorer".length) == "Microsoft Internet Explorer") { this.browser = "IE" leftover = browVer.substring("Microsoft Internet Explorer".length+1, browVer.length); } else if (browVer.substring(0, "Opera".length) == "Opera") { this.browser = "Opera" leftover = browVer.substring("Opera".length+1, browVer.length); } leftover = Trim(leftover); // # Try to get version info out of leftover stuff i = leftover.indexOf(" "); if (i >= 0) { this.version = leftover.substring(0, i); } else { this.version = leftover; } j = this.version.indexOf("."); if (j >= 0) { this.majorver = this.version.substring(0,j); this.minorver = this.version.substring(j+1, this.version.length); } else { this.majorver = this.version; } } // function getRandom() { var randomNum = Math.random() * 37423535353578 + 1; randomNum = parseInt(randomNum); if(isNaN(randomNum)) randomNum = 0; // for Netscape return randomNum; }