// Semkhor Javascript Library // // Some of the the functions take in locale configuration for currencies and dates, which is why this is an ASP file which generates javascript. // So the variables which must be sent are: // // config_currency ("pound" is the only recognized value, otherwise "dollar" is assumed) // [aug 2013]: added "euro" here although it is not officially supported system-wide // config_dateFormat -- mm/dd/yyyy default, can specify dd/mm/yyyy function formFieldIsBlank(fieldID){ if (document.getElementById(fieldID)){ t_val = document.getElementById(fieldID).value; t_val = t_val.replace(/^\s+|\s+$/g, ""); // trim whitespace if (t_val == ""){ return true; // blank } } return false; // OK } function trim(inVal){ inVal = inVal.replace(/^\s+|\s+$/g, ""); return inVal; } // this facilitates setting style.display but is also necessary because // of the legacy concept of using classes for this purpose. function toggleDisplay(elID, state){ var obj = document.getElementById(elID); if (obj){ if (obj.className == "divHide" || obj.className == "divShow") obj.className = ""; if (state) obj.style.display = ""; else obj.style.display = "none"; } } function isDisplayed(thisObj){ var isShown = true; while (thisObj){ if (thisObj.style.display == "none"){ return false; } thisObj = thisObj.parentElement; } return true; } // when outputting strings to be displayed, this makes sure that html tags are not output. function toText(inVal){ return inVal.replace(/]+)>)/ig,""); return inVal; } // outputs a text summary based on the first X # chars, then goes to the end of that word and outputs ... // if there is more. function textSummary(inStr, numChars){ var retVal = "" if (inStr.length <= numChars){ retVal = inStr; } else { // advance numChars until next space or end of string tchr = inStr.substr(numChars, 1); while (" \t\n\r\v".indexOf(tchr) == -1){ numChars++; tchr = inStr.substr(numChars, 1); } retVal = inStr.substr(0, numChars); if (numChars < inStr.length) retVal += "..."; } return retVal; } // returns the # of checkboxes selected with the id "fieldName" or starting // with fieldName: because sometimes the different checkboxes have unique // IDs such as fieldName:1 or fieldName:2 function validateCheckbox(formObj, fieldName){ retVal = 0; for (i=0;i 0){ var i; for (i=0;i -1){ theType = false; // percentage theValue = theValue.replace(/%/g,""); } // remove dollar sign if present //theValue = theValue.replace(/\$/g,""); theValue = stripNonNumeric(theValue); // Now we should be left with a # if (theValue == "" || isNaN(theValue)){ theValue = ""; } else { if (parseInt(theValue) > 100000){ alert("Quantities larger than $100,000 are not supported. Please enter a lower number."); theValue = ""; } else if (parseInt(theValue) < 0){ theValue = ""; // can't enter negative #'s. } else { // looks like we have a valid price, make sure that it's formatted correctly if (theType) theValue = formatCurrency(theValue,flagSymbol); else theValue = formatPercent(theValue); } } thisObj.value = theValue; } // minVal & maxVal are optional. flagSymbol is optional and defaults to true. function validateCost(thisObj, minVal, maxVal,flagSymbol){ theValue = new String(thisObj.value); if (flagSymbol == undefined) flagSymbol = true; if (isNaN(minVal)) minVal = 0; if (isNaN(maxVal)) maxVal = 0; // 0 = infinity for this one // Trim whitespace theValue = theValue.replace(/^\s+|\s+$/g, ""); // remove dollar sign if present //theValue = theValue.replace(/\$/g,""); theValue = stripNonNumeric(theValue); // Now we should be left with a # if (theValue == "" || isNaN(theValue)){ theValue = ""; } else { if (maxVal > 0 && parseFloat(theValue) > maxVal) theValue = maxVal; if (parseFloat(theValue) < minVal) theValue = minVal; // looks like we have a valid price, make sure that it's formatted correctly theValue = formatCurrency(theValue,flagSymbol); } thisObj.value = theValue; } function validatePercent(thisObj){ theValue = new String(thisObj.value); theValue = theValue.replace(/%/g,""); if (theValue == "" || isNaN(theValue)){ theValue = ""; } else { // make sure that it's formatted correctly theValue = formatPercent(theValue); } thisObj.value = theValue; } // note that this function is outdated as the "must be positive" restriction can be // specified by the range parameters of validateInt() function validatePositiveInt(thisObj, useDefault){ if (thisObj.value == null) retVal = useDefault; else if (thisObj.value == "") retVal = useDefault; else if (isNaN(thisObj.value)) retVal = useDefault; else retVal = parseInt(thisObj.value); if (retVal < 0) retVal = useDefault; thisObj.value = retVal; } // minVal & maxVal are optional. function validateInt(thisObj, useDefault, minVal, maxVal){ if (isNaN(minVal)) minVal = null; if (isNaN(maxVal)) maxVal = null; theValue = thisObj.value; theValue = theValue.replace(/^\s+|\s+$/g, ""); if (theValue == null) retVal = useDefault; else if (theValue == "") retVal = useDefault; else if (isNaN(theValue)) retVal = useDefault; else { retVal = parseInt(theValue); if (minVal != null && retVal < minVal) retVal = minVal; if (maxVal != null && retVal > maxVal) retVal = maxVal; } thisObj.value = retVal; return retVal; } // Should accept either mm/dd/yyyy or dd/mm/yyyy depending on format configuration. function validateDate(thisObj){ tStr = thisObj.value; if (tStr != ""){// blank values are acceptable userFormat = "mm/dd/yyyy"; // first test the general format ##/##/#### var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/ if (re.test(tStr)) { // Now test the date is actually valid. var dArr = tStr.split("/"); var d = new Date(); if (userFormat == "mm/dd/yyyy"){ d.setFullYear(dArr[2], dArr[0]-1, dArr[1]); if (d.getMonth() + 1 == parseInt(dArr[0],10) && d.getDate() == parseInt(dArr[1],10) && d.getFullYear() == parseInt(dArr[2],10)){} // good else { alert("The date you have entered is not valid. Expected format is " + userFormat + "."); thisObj.value = ""; } } else { // dd/mm/yyyy format d.setFullYear(dArr[2], parseInt(dArr[1],10)-1, dArr[0]); if (d.getDate() == parseInt(dArr[0],10) && d.getMonth()+1 == parseInt(dArr[1],10) && d.getFullYear() == parseInt(dArr[2],10)){} // good else { alert("The date you have entered is not valid. Expected format is " + userFormat + "."); thisObj.value = ""; } } } else { alert("Dates must be entered in " + userFormat + " format."); thisObj.value = ""; } } } function parseSlashedDate(userInput){ var userFormat = "mm/dd/yyyy"; var dArr = userInput.split("/"); var d = new Date(); if (userFormat == "mm/dd/yyyy"){ d.setFullYear(dArr[2], dArr[0]-1, dArr[1]); } else { // dd/mm/yyyy format d.setFullYear(dArr[2], parseInt(dArr[1],10)-1, dArr[0]); } return d; } function formatSlashedDate(inDate){ var tOut; var userFormat = "mm/dd/yyyy"; if (userFormat == "mm/dd/yyyy"){ tOut = (inDate.getMonth()+1) + "/" + inDate.getDate() + "/" + inDate.getFullYear(); } else { // dd/mm/yyyy tOut = inDate.getDate() + "/" + (inDate.getMonth()+1) + "/" + inDate.getFullYear(); } return tOut; } function validatePhoneNumber(thisObj, locale){ tVal = thisObj.value; outVal = tVal; if (tVal != ""){ // First strip out all non-numeric characters: tVal = tVal.replace(/[^0-9]/g, ''); // "locale" determines formatting and accepted values. If not provided, then we default // to USA: 10 digits ###-###-#### (strip leading 0 or 1) // other locales currently supported: // AUS: 9 digits but preceeded by a 0: 0#-####-#### // NZ: 8 digits but preceeded by a 0: 0#-###-#### // AUS/NZ: will accept either of the above and format depending how many digits are received. if (!locale || locale=="USA"){ // should have either 10 or 11 characters, if 11 then check for and strip a leading 1 or 0: if (tVal.length == 11) if (tVal.substr(0,1)=="0" || tVal.substr(0,1)=="1") tVal = tVal.substr(1); if (tVal.length == 10){ // Good value, just format: outVal = tVal.substr(0,3) + "-" + tVal.substr(3,3) + "-" + tVal.substr(6,4); } else { // bad input, alert user and clear it out: alert("The value you have entered is not a valid phone number. The expected format is ###-###-####."); outVal = ""; } } else if (locale=="AUS"){ // Should have 9 chars. If 10 then look for leading 0. if (tVal.length==10) if (tVal.substr(0,1)=="0") tVal = tVal.substr(1); if (tVal.length == 9){ // Good value, just format: outVal = "0" + tVal.substr(0,1) + "-" + tVal.substr(1,4) + "-" + tVal.substr(5,4); } else { // bad input, alert user and clear it out: alert("The value you have entered is not a valid phone number. The expected format is 0#-####-####."); outVal = ""; } } else if (locale=="NZ"){ // Should have 8 chars. If 9 then look for leading 0. if (tVal.length==9) if (tVal.substr(0,1)=="0") tVal = tVal.substr(1); if (tVal.length == 8){ // Good value, just format: outVal = "0" + tVal.substr(0,1) + "-" + tVal.substr(1,3) + "-" + tVal.substr(4,4); } else { // bad input, alert user and clear it out: alert("The value you have entered is not a valid phone number. The expected format is 0#-###-####."); outVal = ""; } } else if (locale=="AUS/NZ"){ if (tVal.substr(0,1)=="0") tVal = tVal.substr(1); if (tVal.length==9){ outVal = "0" + tVal.substr(0,1) + "-" + tVal.substr(1,4) + "-" + tVal.substr(5,4); } else if (tVal.length==8){ outVal = "0" + tVal.substr(0,1) + "-" + tVal.substr(1,3) + "-" + tVal.substr(4,4); } else { alert("The value you have entered is not a valid phone number. The expected format is 0# + 7 or 8 digits."); outVal = ""; } } } thisObj.value = outVal; } // returns true or false function validateEmailAddress(inValue){ var i; // validation works like this: // email address must contain @ followed by . // email address is divided up into 3 sections: // account = section up to @ // domain = section up to first . // tld = the rest right of the first . // each section must be present and not blank. // account must be alphanumeric or -_. // domain names are alphanumeric with - but cannot begin or end with - // TLD is alphanumeric plus . (because they can be chained together) inValue = inValue.replace(/^\s+|\s+$/g, ""); // trim whitespace if (inValue.indexOf("@") == -1 || inValue.indexOf(".") == -1) return false; // no @ or no . atLoc = inValue.indexOf("@"); // Sep2013: now using last period (consider blah@gw2.u-netsurf.ne.jp) //firstdotLoc = inValue.indexOf(".",atLoc); firstdotLoc = inValue.lastIndexOf("."); if (firstdotLoc == -1) return false; // no . after the @ account = inValue.substr(0, atLoc); domain = inValue.substr(atLoc+1, firstdotLoc-atLoc-1); tld = inValue.substr(firstdotLoc+1); if (account=="" || domain=="" || tld=="") return false; // a required component is blank for (i=0;i= 65 && tCode <= 90) || (tCode >= 97 && tCode <= 122) || (tCode >= 48 && tCode <= 57) || tChar=="_" || tChar=="-" || tChar==".")) return false; } if (domain.substr(0,1)=="-" || domain.substr(domain.length-1,1)=="-") return false; // begins or ends with dash for (i=0;i= 65 && tCode <= 90) || (tCode >= 97 && tCode <= 122) || (tCode >= 48 && tCode <= 57) || tChar=="-" || tChar==".")) return false; } for (i=0;i= 65 && tCode <= 90) || (tCode >= 97 && tCode <= 122) || (tCode >= 48 && tCode <= 57) || tChar==".")) return false; } return true; } // Imposes a length limit on textareas. To use add this parameter to your