Site Navigation

Useful JScript Functions

[2001-03-22] < Back

I know this is probably long overdue, but here is a list of some of the more common JScript functions that I use, and hopefully you will find useful.

trim:
Equivalent to VB & VBScript's trim functions.
String.prototype.trim = function () {
    return this.replace(/(^\s+)|(\s+$)/g, "");
}
formatNumber:
This function takes a number and turns it into a formatted string, with commas and the specified number of decimal places. If you do not specify the number of decimal places it will default to 0.
Number.prototype.formatNumber = function (placesToTheRight) {
    if (placesToTheRight == null) placesToTheRight = 0;
    if (typeof placesToTheRight != "number") throw "Invalid Parameter: " + placesToTheRight;
    if (placesToTheRight < 0) throw "Argument out of range: " + placesToTheRight;
    /(-*\d+)(\.(\d*))*/.exec(String(this));
    var wholeNumber = RegExp.$1;
    var remainder = RegExp.$3;
    switch (placesToTheRight) {
        case 0:
            return wholeNumber;
        default:
            var arr = new Array(placesToTheRight);
            arr.initialize(0);
            for (var i = 0; i < remainder.length && i < arr.length; i++)
                arr[i] = remainder.charAt(i);
            return wholeNumber + "." + arr.join("");
    }
}
This is required for the formatNumber function.
Array.prototype.initialize = function (value) {
    for (var i = 0; i < this.length; i++) {
        this[i] = value;
    }
}
formatCurrency:
This is equivalent to VB & VBScripts FormatCurrency functions. Requires the formatNumber function to be present.
Number.prototype.formatCurrency = function () {
    return "$" + this.formatNumber(2);
}
getMonthName:
This function will take a date object and return a string of the month name.
Date.prototype.getMonthName = function() {
    switch(this.getMonth()) {
        case 1: return "January"; break;
        case 2: return "February"; break;
        case 3: return "March"; break;
        case 4: return "April"; break;
        case 5: return "May"; break;
        case 6: return "June"; break;
        case 7: return "July"; break;
        case 8: return "August"; break;
        case 9: return "September"; break;
        case 10: return "October"; break;
        case 11: return "November"; break;
        case 12: return "December"; break;
        default: return "";
    }
}
formatDate:
This function will format a date into the specified format. You will need to reference the function definition to determine how the returned date will be formatted. The default is mm/dd/yyyy.
Date.prototype.formatDate = function (type) {
    switch (type) {
        case 2: // dd mname, yyyy
            return (this.getDate() + " " + this.getMonthName() + ", " + this.getFullYear());
        case 3: //mname dd, yyyy
            return (this.getMonthName() + " " + this.getDate() + ", " + this.getFullYear());
        case 4: // mm/dd/yyyy hh:mm:ss AM|PM
            return this.formatDate() + " " + this.formatDate (7);
        case 5: // mm/dd/yyyy hh:mm AM|PM
            return this.formatDate() + " " + this.formatDate (7);
        case 6: // hh:mm:ss AM|PM
            return this.formatDate(8) + ":" + this.getSeconds().zeroPad() + " " +
							(this.getHours() > 12 ? "PM" : "AM");
        case 7: // hh:mm AM|PM
            return this.formatDate(8) + " " + (this.getHours() > 12 ? "PM" : "AM");
        case 8: // hh:mm
            return (this.getHours() % 12 == 0 ? 12 : this.getHours() % 12) + ":" +
							this.getMinutes().zeroPad();
        default : // mm/dd/yyyy
            return (this.getMonth() + 1).zeroPad() + "/" + this.getDate().zeroPad() + "/" +
							this.getFullYear();
    }
}
This is required for the formatDate function.
Number.prototype.zeroPad = function() {
    if (this < 10)
        return "0" + this.toString();
    return this.toString();
}
toUsaString:
This function is required for the formatDate function.
Date.prototype.toUsaString = function() {
    return (ZeroPad(this.getMonth() + 1)) + "/" + 
        ZeroPad(this.getDate()) + "/" + 
        ZeroPad(this.getFullYear()) + " " + 
        ZeroPad(this.getHours()) + ":" + 
        ZeroPad(this.getMinutes()) + ":" + 
        ZeroPad(this.getSeconds());
}
formatPhone:
This function takes in a string and formats it as a phone number, adding parends and dashes in appropriate places.
String.prototype.formatPhone = function () {
    var returnString = new String();
    if (String(this).length == 7) {
        returnString = String(this).substring(0,3) + "-" + String(this).substring(3,7);
    } else if (String(this).length == 10) {
        returnString = "(" + String(this).substring(0,3) + ")" + String.fromCharCode(160) +
				String(this).substring(3,6) + "-" + String(this).substring(6,10);
    } else {
        returnString = this;
    }
    return returnString;
}
formatZip:
This function takes in a string and formats it as a zipcode.
String.prototype.formatZip = function() {
    strTmpZip = new String(this);
    if (this.indexOf("-") == -1) {
        if (this.length > 5) strTmpZip = this.substr(0,5) + "-" + this.substr(5);
    }
    return strTmpZip;
}
SQLin:
This function takes almost any type of JScript object type and formats it properly for input into SQL Server. When you use this function do not include single quotes around strings and dates as the function puts them in on it's own.
function SQLin (val) {
    switch (typeof val) {
        case "string":
            if (val.toUpperCase() == "NULL") return "NULL";
            if (val.toUpperCase() == "UNDEFINED") return "NULL";
            val = val.replace(/\'/g, "''").replace(/(&)/g, "&"); //'
            return "'" + val + "'";
        case "boolean":
            return val ? 1 : 0;
        case "number":
            return val;
        case "date":
            return "'" + val.toUsaString() + "'";
        default:
            return val == null ? "NULL" : SQLin(String(val));
    }
}

< Back