// FormChek.js // See FormChe2.js for original file // VARIABLE DECLARATIONS var digits = "0123456789"; var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz" var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" // whitespace characters var whitespace = " \t\n\r"; // non-digit characters which are allowed in phone numbers var phoneNumberDelimiters = "()- "; // characters which are allowed in US phone numbers var validUSPhoneChars = digits + phoneNumberDelimiters; // U.S. phone numbers have 10 digits. // They are formatted as 123 456 7890 or (123) 456-7890. var digitsInUSPhoneNumber = 10; // non-digit characters which are allowed in ZIP Codes var ZIPCodeDelimiters = "-"; // our preferred delimiter for reformatting ZIP Codes var ZIPCodeDelimeter = "-" // U.S. ZIP codes have 5 or 9 digits. // They are formatted as 12345 or 12345-6789. var digitsInZIPCode1 = 5 var digitsInZIPCode2 = 9 // CONSTANT STRING DECLARATIONS // m is an abbreviation for "missing" var mPrefix = "You did not enter a value into the " var mSuffix = " field. This is a required field. Please enter it now." // s is an abbreviation for "string" var sUSLastName = "Last Name" var sUSFirstName = "First Name" var sCompanyName = "Company Name" var sUSAddress = "Street Address" var sCity = "City" var sStateCode = "State Code" var sZIPCode = "ZIP Code" var sPhone = "Phone Number" var sEmail = "Email" // dl-add var sComments = "Comments" var sUSName = "Name" var sTitle ="Title (if applicable)" var sNeeds = "all that applies" var sRadio = "applicable choice" var sSubmit = "to eMail completed form." var sExit = "to return to previous page." // i is an abbreviation for "invalid" var iStateCode = "This field must be a valid two character U.S. state abbreviation (like CA for California). Please reenter it now." var iZIPCode = "This field must be a 5 or 9 digit U.S. ZIP Code (like 94043). Please reenter it now." var iUSPhone = "This field must be a 10 digit U.S. phone number (like 415 555 1212). Please reenter it now." var iEmail = "This field must be a valid email address (like foo@bar.com). Please reenter it now." // p is an abbreviation for "prompt" var pEntryPrompt = "Please enter a " var pStateCode = "2 character code (like CA)." var pZIPCode = "5 or 9 digit U.S. ZIP Code (like 94043)." var pUSPhone = "10 digit U.S. phone number (like 415 555 1212)." var pEmail = "valid email address (like foo@bar.com)." // dl-add var pEntryOther = "Please enter " var pEntryChoice = "Please check " var pTitle = "Title (if applicable)" var pSubmit = "Click 'SEND IT' button " var pExit = "Click 'EXIT' button " var defaultEmptyOK = false // Valid U.S. Postal Codes for states, territories, armed forces, etc. // See http://www.usps.gov/ncsc/lookups/abbr_state.txt. var USStateCodeDelimiter = "|"; var USStateCodes = "AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AE|AA|AE|AE|AP" // 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) { var i; // Is s empty? if (isEmpty(s)) return true; // Search through string's characters one by one // until we find a non-whitespace character. // When we do, return false; if we don't, return true. for (i = 0; i < s.length; i++) { // Check that current character isn't whitespace. var c = s.charAt(i); if (whitespace.indexOf(c) == -1) return false; } // All characters are whitespace. return true; } // Removes all characters which appear in string bag from string s. function stripCharsInBag (s, bag) { var i; var returnString = ""; // Search through string's characters one by one. // If character is not in bag, append to returnString. for (i = 0; i < s.length; i++) { // Check that current character isn't whitespace. var c = s.charAt(i); if (bag.indexOf(c) == -1) returnString += c; } return returnString; } // Removes all characters which do NOT appear in string bag // from string s. function stripCharsNotInBag (s, bag) { var i; var returnString = ""; // Search through string's characters one by one. // If character is in bag, append to returnString. for (i = 0; i < s.length; i++) { // Check that current character isn't whitespace. var c = s.charAt(i); if (bag.indexOf(c) != -1) returnString += c; } return returnString; } // Removes all whitespace characters from s. // Global variable whitespace (see above) // defines which characters are considered whitespace. function stripWhitespace (s) { return stripCharsInBag (s, whitespace) } function charInString (c, s) { for (i = 0; i < s.length; i++) { if (s.charAt(i) == c) return true; } return false } // Removes initial (leading) whitespace characters from s. // Global variable whitespace (see above) // defines which characters are considered whitespace. function stripInitialWhitespace (s) { var i = 0; while ((i < s.length) && charInString (s.charAt(i), whitespace)) i++; return s.substring (i, s.length); } // Returns true if character c is an English letter // (A .. Z, a..z). // // 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 isLetter (c) { return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) ) } // Returns true if character c is a digit // (0 .. 9). function isDigit (c) { return ((c >= "0") && (c <= "9")) } // Returns true if character c is a letter or digit. function isLetterOrDigit (c) { return (isLetter(c) || isDigit(c)) } // isInteger (STRING s [, BOOLEAN emptyOK]) function isInteger (s) { var i; if (isEmpty(s)) if (isInteger.arguments.length == 1) return defaultEmptyOK; else return (isInteger.arguments[1] == true); // Search through string's characters one by one // until we find a non-numeric character. // When we do, return false; if we don't, return true. for (i = 0; i < s.length; i++) { // Check that current character is number. var c = s.charAt(i); if (!isDigit(c)) return false; } // All characters are numbers. return true; } // isAlphabetic (STRING s [, BOOLEAN emptyOK]) function isAlphabetic (s) { var i; if (isEmpty(s)) if (isAlphabetic.arguments.length == 1) return defaultEmptyOK; else return (isAlphabetic.arguments[1] == true); // Search through string's characters one by one // until we find a non-alphabetic character. // When we do, return false; if we don't, return true. for (i = 0; i < s.length; i++) { // Check that current character is letter. var c = s.charAt(i); if (!isLetter(c)) return false; } // All characters are letters. return true; } // isAlphanumeric (STRING s [, BOOLEAN emptyOK]) function isAlphanumeric (s) { var i; if (isEmpty(s)) if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK; else return (isAlphanumeric.arguments[1] == true); // Search through string's characters one by one // until we find a non-alphanumeric character. // When we do, return false; if we don't, return true. for (i = 0; i < s.length; i++) { // Check that current character is number or letter. var c = s.charAt(i); if (! (isLetter(c) || isDigit(c) ) ) return false; } // All characters are numbers or letters. return true; } // reformat (TARGETSTRING, STRING, INTEGER, STRING, INTEGER ... ) function reformat (s) { var arg; var sPos = 0; var resultString = ""; for (var i = 1; i < reformat.arguments.length; i++) { arg = reformat.arguments[i]; if (i % 2 == 1) resultString += arg; else { resultString += s.substring(sPos, sPos + arg); sPos += arg; } } return resultString; } // isUSPhoneNumber (STRING s [, BOOLEAN emptyOK]) function isUSPhoneNumber (s) { if (isEmpty(s)) if (isUSPhoneNumber.arguments.length == 1) return defaultEmptyOK; else return (isUSPhoneNumber.arguments[1] == true); return (isInteger(s) && s.length == digitsInUSPhoneNumber) } // isZIPCode (STRING s [, BOOLEAN emptyOK]) function isZIPCode (s) { if (isEmpty(s)) if (isZIPCode.arguments.length == 1) return defaultEmptyOK; else return (isZIPCode.arguments[1] == true); return (isInteger(s) && ((s.length == digitsInZIPCode1) || (s.length == digitsInZIPCode2))) } // isStateCode (STRING s [, BOOLEAN emptyOK]) function isStateCode(s) { if (isEmpty(s)) if (isStateCode.arguments.length == 1) return defaultEmptyOK; else return (isStateCode.arguments[1] == true); return ( (USStateCodes.indexOf(s) != -1) && (s.indexOf(USStateCodeDelimiter) == -1) ) } // isEmail (STRING s [, BOOLEAN emptyOK]) function isEmail (s) { if (isEmpty(s)) if (isEmail.arguments.length == 1) return defaultEmptyOK; else return (isEmail.arguments[1] == true); // is s whitespace? if (isWhitespace(s)) return false; // there must be >= 1 character before @, so we // start looking at character position 1 // (i.e. second character) var i = 1; var sLength = s.length; // look for @ while ((i < sLength) && (s.charAt(i) != "@")) { i++ } if ((i >= sLength) || (s.charAt(i) != "@")) return false; else i += 2; // look for . while ((i < sLength) && (s.charAt(i) != ".")) { i++ } // there must be at least one character after the . if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false; else return true; } /* FUNCTIONS TO NOTIFY USER OF INPUT REQUIREMENTS OR MISTAKES. */ // Display prompt string s in status bar. function prompt (s) { window.status = s } // Display data entry prompt string s in status bar. function promptEntry (s) { window.status = pEntryPrompt + s } // dl-add // Display alt data entry prompt string s in status bar. // -- for non-singular, ie: Enter comments function promptOther (s) { window.status = pEntryOther + s } // dl-add // Display Mult data entry prompt string s in status bar. function promptChoice (s) { window.status = pEntryChoice + s } function promptSend (s) { window.status = pSubmit + s } function promptExit (s) { window.status = pExit + 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 } /* FUNCTIONS TO INTERACTIVELY CHECK VARIOUS FIELDS. */ 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; } // checkStateCode (TEXTFIELD theField [, BOOLEAN emptyOK==false]) // // Check that string theField.value is a valid U.S. state code. function checkStateCode (theField, emptyOK) { if (checkStateCode.arguments.length == 1) emptyOK = defaultEmptyOK; if ((emptyOK == true) && (isEmpty(theField.value))) return true; else { theField.value = theField.value.toUpperCase(); if (!isStateCode(theField.value, false)) return warnInvalid (theField, iStateCode); else return true; } } // dl-add function getItUp(theField) { theField.value =theField.value.toUpperCase(); return true; } function getItDown(theField) { theField.value =theField.value.toLowerCase(); return true; } function getItProper(theField) { theField.value =theField.value.toUpperCase(); return true; } // From proper.htm ---------------------------------------------------- //The Proper Case function, pcase(), is defined below. function pCase(theField) { //returns str in proper-noun case (first letter uppercase) strlen = theField.length jj = theField.substring(0,1).toUpperCase() jj = jj + theField.substring(1,strlen).toLowerCase() for (i = 2; i <= strlen; i++) { if (jj.charAt(i)==" ") { lefthalf = jj.substring(0,i+1) righthalf = jj.substring(i+1,strlen) righthalf = righthalf.substring(0,1).toUpperCase()+righthalf.substring(1,strlen) jj=lefthalf+righthalf } } return jj } // ---------------------------------------------------------------------- function getItXProper(theField) { theField.value =theField.value.pCase(theField); return true; } // takes ZIPString, a string of 5 or 9 digits; // if 9 digits, inserts separator hyphen function reformatZIPCode (ZIPString) { if (ZIPString.length == 5) return ZIPString; else return (reformat (ZIPString, "", 5, "-", 4)); } // checkZIPCode (TEXTFIELD theField [, BOOLEAN emptyOK==false]) // function checkZIPCode (theField, emptyOK) { if (checkZIPCode.arguments.length == 1) emptyOK = defaultEmptyOK; if ((emptyOK == true) && (isEmpty(theField.value))) return true; else { var normalizedZIP = stripCharsInBag(theField.value, ZIPCodeDelimiters) if (!isZIPCode(normalizedZIP, false)) return warnInvalid (theField, iZIPCode); else { // if you don't want to insert a hyphen, comment next line out theField.value = reformatZIPCode(normalizedZIP) return true; } } } // takes USPhone, a string of 10 digits // and reformats as (123) 456-789 function reformatUSPhone (USPhone) { return (reformat (USPhone, "(", 3, ") ", 3, "-", 4)) } // checkUSPhone (TEXTFIELD theField [, BOOLEAN emptyOK==false]) function checkUSPhone (theField, emptyOK) { if (checkUSPhone.arguments.length == 1) emptyOK = defaultEmptyOK; if ((emptyOK == true) && (isEmpty(theField.value))) return true; else { var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters) if (!isUSPhoneNumber(normalizedPhone, false)) return warnInvalid (theField, iUSPhone); else { // if you don't want to reformat as (123) 456-789, comment next line out theField.value = reformatUSPhone(normalizedPhone) return true; } } } // checkEmail (TEXTFIELD theField [, BOOLEAN emptyOK==false]) // function checkEmail (theField, emptyOK) { if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK; if ((emptyOK == true) && (isEmpty(theField.value))) return true; else if (!isEmail(theField.value, false)) return warnInvalid (theField, iEmail); else return true; } // Get checked value from radio button. function getRadioButtonValue (radio) { for (var i = 0; i < radio.length; i++) { if (radio[i].checked) { break } } return radio[i].value }