Tuesday, September 25, 2018

JavaScript Utilities validation

JavaScript utilities:
--------------------------

amatJSUtil = function () { }
//
amatJSUtil.roundTo2Decimals = function (numberToRound, isNecessaryCheck) {
    if (isNecessaryCheck && isNecessaryCheck == true) {
        if (numberToRound.toString().split('.')[1])
            return Number(numberToRound).toFixed(2);
        else
            return numberToRound;
    }
    else {
        return Number(numberToRound).toFixed(2);
    }
}
//
amatJSUtil.roundTo1Decimals = function (numberToRound, isNecessaryCheck) {
    if (isNecessaryCheck && isNecessaryCheck == true) {
        if (numberToRound.toString().split('.')[1])
            return Number(numberToRound).toFixed(1);
        else
            return numberToRound;
    }
    else {
        return Number(numberToRound).toFixed(1);
    }
}
//
amatJSUtil.replaceAll = function (str, replace, with_this) {
    var str_hasil = "";
    var temp;
    for (var i = 0; i < str.length; i++) // not need to be equal. it causes the last change: undefined..
    {
        if (str[i] == replace)
            temp = with_this;
        else
            temp = str[i];

        str_hasil += temp;
    }
    return str_hasil;
}
//
amatJSUtil.getFileName = function (path) {
    var ary = amatJSUtil.replaceAll(path, "/", "\\").split("\\");
    return ary[ary.length - 1];
}
//
amatJSUtil.zeroPad = function (number, width) {
    z = '0';
    number = number + '';
    return number.length >= width ? number : new Array(width - number.length + 1).join(z) + number;
}
//
amatJSUtil.isValidDate = function (date) {
    var valid = true;

    date = amatJSUtil.replaceAll(date, '/', '');

   
    var month = parseInt(date.substring(0, 2), 10);
    var day = parseInt(date.substring(2, 4), 10);
    var year = parseInt(date.substring(4, 8), 10);
   
    var extra = date.substring(8, date.length);

    if (isNaN(year) || isNaN(month) || isNaN(day) || extra.trim() != '') {
        valid = false;
    } else {
        if ((month < 1) || (month > 12))
            valid = false;
        else if ((day < 1) || (day > 31))
            valid = false;
        else if (((month == 4) || (month == 6) || (month == 9) || (month == 11)) && (day > 30))
            valid = false;
        else if ((month == 2) && (((year % 400) == 0) || ((year % 4) == 0)) && ((year % 100) != 0) && (day > 29))
            valid = false;
        else if ((month == 2) && ((year % 100) == 0) && (day > 29))
            valid = false;
    }
    return valid;
}
//
amatJSUtil.validateEmail = function (email) {
    var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
    if (reg.test(email)) {
        return true;
    }
    else {
        return false;
    }
}
//
amatJSUtil.isValidDateOnPaste = function (e, obj) {

    var valid = true;
    var date;
    if (e) {
        date = e.clipboardData.getData('Text');
    } else {
        date = window.clipboardData.getData('Text');
    }
    date = amatJSUtil.replaceAll(date, '/', '');

    var year = parseInt(date.substring(0, 4), 10);
    var month = parseInt(date.substring(4, 6), 10);
    var day = parseInt(date.substring(6, 8), 10);
    var extra = date.substring(8, date.length);

    if (isNaN(year) || isNaN(month) || isNaN(day) || extra.trim() != '') {
        valid = false;
    } else {
        if ((month < 1) || (month > 12))
            valid = false;
        else if ((day < 1) || (day > 31))
            valid = false;
        else if (((month == 4) || (month == 6) || (month == 9) || (month == 11)) && (day > 30))
            valid = false;
        else if ((month == 2) && (((year % 400) == 0) || ((year % 4) == 0)) && ((year % 100) != 0) && (day > 29))
            valid = false;
        else if ((month == 2) && ((year % 100) == 0) && (day > 29))
            valid = false;
    }
    if (!valid)
        alert("Enter Valid Date");

    return valid;
}
amatJSUtil._decimalNumberOnly = function (e, obj) {
    var dotlength = obj.value.split('.').length;
    var dotlArray = obj.value.split('.');
    var unicode;
    if (e) {
        unicode = e.charCode ? e.charCode : e.keyCode;
    } else {
        unicode = event.charCode ? event.charCode : event.keyCode;
    }
    //
    if (unicode != 8) { //if the key isn't the backspace key (which we should allow)
        if (((unicode < 48 || unicode > 57) && unicode != 46) || (unicode == 46 && dotlength >= 2)) //obj.value.split(".").length>=3) ) //if not a number
            return false //disable key press
    }
}
//
amatJSUtil._decimalNumberTwoPrecisionOnly = function (e, obj) {
    var dotlength = obj.value.split('.').length;
    var dotlArray = obj.value.split('.');
    var unicode;
    if (e) {
        unicode = e.charCode ? e.charCode : e.keyCode;
    } else {
        unicode = event.charCode ? event.charCode : event.keyCode;
    }
    //
    if (unicode != 8) { //if the key isn't the backspace key (which we should allow)
        if (((unicode < 48 || unicode > 57) && unicode != 46) || (unicode == 46 && dotlength >= 2)) //obj.value.split(".").length>=3) ) //if not a number
            return false //disable key press
        else if (dotlength == 2 && dotlArray[1].length > 2) {
            return false;
        }
    }
}
//
amatJSUtil._decimalNumTwoPrecisionPaste = function (e, obj) {
    var totalCharacterCount;
    if (e) {
        totalCharacterCount = e.clipboardData.getData('Text');
    } else {
        totalCharacterCount = window.clipboardData.getData('Text');
    }

    var strValidChars = "0123456789";
    var strDot = ".";
    var strChar;
    var FilteredChars = "";
    var isDotPresent = 0;
    for (i = 0; i < totalCharacterCount.length; i++) {
        strChar = totalCharacterCount.charAt(i);
        if (strValidChars.indexOf(strChar) != -1) {
            FilteredChars = FilteredChars + strChar;
        }
        if (strDot.indexOf(strChar) != -1 && isDotPresent == 0) {
            FilteredChars = FilteredChars + strChar;
            isDotPresent = 1;
        }
    }
    if (isDotPresent == 1) {
        obj.value = Number(FilteredChars).toFixed(2);
    }
    else {
        obj.value = Number(FilteredChars);
    }

    amatJSUtil.currencyMask(obj);

    return false;
}

amatJSUtil._decimalNumPaste = function (e, obj) {
    var totalCharacterCount;
    if (e) {
        totalCharacterCount = e.clipboardData.getData('Text');
    } else {
        totalCharacterCount = window.clipboardData.getData('Text');
    }

    var strValidChars = "0123456789";
    var strDot = ".";
    var strChar;
    var FilteredChars = "";
    var isDotPresent = 0;
    for (i = 0; i < totalCharacterCount.length; i++) {
        strChar = totalCharacterCount.charAt(i);
        if (strValidChars.indexOf(strChar) != -1) {
            FilteredChars = FilteredChars + strChar;
        }
        if (strDot.indexOf(strChar) != -1 && isDotPresent == 0) {
            FilteredChars = FilteredChars + strChar;
            isDotPresent = 1;
        }
    }
    obj.value = Number(FilteredChars);
    amatJSUtil.currencyMask(obj);
    return false;
}
//
amatJSUtil._integerNumbersOnly = function (e, obj) {
    var unicode;
    if (e) {
        unicode = e.charCode ? e.charCode : e.keyCode;
    } else {
        unicode = event.charCode ? event.charCode : event.keyCode;
    }
    //
    if (unicode != 8) { //if the key isn't the backspace key (which we should allow)
        if (unicode < 48 || unicode > 57) //obj.value.split(".").length>=3) ) //if not a number
            return false //disable key press
    }
}
amatJSUtil._integerCurrencyOnly = function (e, obj) {
    var unicode;
    if (e) {
        unicode = e.charCode ? e.charCode : e.keyCode;
    } else {
        unicode = event.charCode ? event.charCode : event.keyCode;
    }
    //
    if (unicode != 8) { //if the key isn't the backspace key (which we should allow)
        if (unicode < 48 || unicode > 57) //obj.value.split(".").length>=3) ) //if not a number
            return false //disable key press
    }
}

//
amatJSUtil._integerNumPaste = function (e, obj) {
    var totalCharacterCount;
    if (e) {
        totalCharacterCount = e.clipboardData.getData('Text');
    } else {
        totalCharacterCount = window.clipboardData.getData('Text');
    }

    var strValidChars = "0123456789";
    var strChar;
    var FilteredChars = "";
    for (i = 0; i < totalCharacterCount.length; i++) {
        strChar = totalCharacterCount.charAt(i);
        if (strValidChars.indexOf(strChar) != -1) {
            FilteredChars = FilteredChars + strChar;
        }
    }
    obj.value = FilteredChars;
    return false;
}
amatJSUtil._integerCurrencyPaste = function (e, obj) {
    var totalCharacterCount;
    if (e) {
        totalCharacterCount = e.clipboardData.getData('Text');
    } else {
        totalCharacterCount = window.clipboardData.getData('Text');
    }

    var strValidChars = "0123456789";
    var strChar;
    var FilteredChars = "";
    for (i = 0; i < totalCharacterCount.length; i++) {
        strChar = totalCharacterCount.charAt(i);
        if (strValidChars.indexOf(strChar) != -1) {
            FilteredChars = FilteredChars + strChar;
        }
    }
    obj.value = FilteredChars;
    amatJSUtil.currencyMask(obj);
    return false;
}

amatJSUtil.currencyMask = function (obj) {
    //if(event.keyCode != 37 && event.keyCode != 39)
    {
        var amtString = (obj.value).split(".")[0];
        var amtString = AddComma(amatJSUtil.replaceAll(amtString, ",", ""));
        if ((obj.value).split(".").length >= 2)
            amtString = amtString + "." + (obj.value).split(".")[1];
        obj.value = amtString;
    }
}
function AddComma(MyString) {
    var objRegex = new RegExp('(-?[0-9]+)([0-9]{3})');
    //Check For Criteria....
    while (objRegex.test(MyString)) {
        //Add Commas After Every Three Digits Of Number...
        MyString = MyString.replace(objRegex, '$1,$2');
    }
    return MyString;
}
//
//#Region " Object Identification Methods "
amatJSUtil.getInputTypeText = function (title, id) {
    return (($("input[title='" + title + "']")[0]) ? $("input[title='" + title + "']")[0] : $("input[id*='" + id + "']")[0])
}
//
amatJSUtil.getInputTypeTextAll = function (id) {
    return ($("input[id*='" + id + "']"));
}
amatJSUtil.getInputTypeButtonById = function (id) {
    return ($("input[id*='" + id + "']")[0]);
}

//
amatJSUtil.getInputTypeOpenDecimalNumberText = function (title, id) {
    var objTextBox = (($("input[title='" + title + "']")[0]) ? $("input[title='" + title + "']")[0] : $("input[id*='" + id + "']")[0])
    objTextBox.style.textAlign = "right";
    objTextBox.value = amatJSUtil.replaceAll(objTextBox.value, ",", "");
    objTextBox.maxLength = 21;
    //$("input[id*='" + id + "']").mask("999,999,999,999,999.00");
    objTextBox.onkeypress = function (e) {
        return amatJSUtil._decimalNumberOnly(e, this);
    }
    objTextBox.onpaste = function (e) {

        //return amatJSUtil._decimalNumPaste(e, this);
        return false;
    }
    objTextBox.ondrag = function (e) {
        return false;
    }
    objTextBox.ondrop = function (e) {
        return false;
    }
    return objTextBox;
}
//
amatJSUtil.getInputTypeDecimalNumberText = function (title, id) {
    var objTextBox = (($("input[title='" + title + "']")[0]) ? $("input[title='" + title + "']")[0] : $("input[id*='" + id + "']")[0])
    objTextBox.style.textAlign = "right";
    objTextBox.value = amatJSUtil.replaceAll(objTextBox.value, ",", "");
    objTextBox.maxLength = 21;
    //$("input[id*='" + id + "']").mask("999,999,999,999,999.00");
    objTextBox.onkeypress = function (e) {
        return amatJSUtil._decimalNumberTwoPrecisionOnly(e, this);
    }
    objTextBox.onpaste = function (e) {
        return false;
        //return amatJSUtil._decimalNumTwoPrecisionPaste(e, this);
    }
    objTextBox.ondrag = function (e) {
        return false;
    }
    objTextBox.ondrop = function (e) {
        return false;
    }
    return objTextBox;
}
//
amatJSUtil.getInputTypeIntegerCurrencyText = function (title, id) {
    var objTextBox = (($("input[title='" + title + "']")[0]) ? $("input[title='" + title + "']")[0] : $("input[id*='" + id + "']")[0])
    objTextBox.style.textAlign = "right";
    objTextBox.value = amatJSUtil.replaceAll(objTextBox.value, ",", "");
    objTextBox.maxLength = 21;

    objTextBox.onkeypress = function (e) {
        return amatJSUtil._integerCurrencyOnly(e, this);
    }
    objTextBox.onpaste = function (e) {
        return false;
        //return amatJSUtil._integerCurrencyPaste(e, this);
    }
    objTextBox.ondrag = function (e) {
        return false;
    }
    objTextBox.ondrop = function (e) {
        return false;
    }

    return objTextBox;
}

amatJSUtil.getInputTypeIntegerNumberText = function (title, id) {
    var objTextBox = (($("input[title='" + title + "']")[0]) ? $("input[title='" + title + "']")[0] : $("input[id*='" + id + "']")[0])
    objTextBox.style.textAlign = "right";
    objTextBox.value = amatJSUtil.replaceAll(objTextBox.value, ",", "");

    objTextBox.onkeypress = function (e) {
        return amatJSUtil._integerNumbersOnly(e, this);
    }
    objTextBox.onpaste = function (e) {
        return false;
        //return amatJSUtil._integerNumPaste(e, this);
    }
    objTextBox.ondrag = function (e) {
        return false;
    }
    objTextBox.ondrop = function (e) {
        return false;
    }

    return objTextBox;
}
//
amatJSUtil.getSelectType = function (title, id) {
    return (($("select[title='" + title + "']")[0]) ? $("select[title='" + title + "']")[0] : $("select[id*=" + id + "]")[0])
}
//
amatJSUtil.getSelectTypeAll = function (id) {
    return ($("select[id*=" + id + "]"));
}
//
amatJSUtil.getTextAreaType = function (title, id) {
    return (($("textarea[title='" + title + "']")[0]) ? $("textarea[title='" + title + "']")[0] : $("textarea[id*=" + id + "]")[0])
}
//
amatJSUtil.getTextAreaTypeAll = function (title, id) {
    return ($("textarea[id*=" + id + "]"));
}
//
amatJSUtil.getDivType = function (title, id) {
    return (($("div[title='" + title + "']")[0]) ? $("div[title='" + title + "']")[0] : $("div[id$='" + id + "']")[0]);
}
//
amatJSUtil.getDivTypeAll = function (title, id) {
    return (($("div[title='" + title + "']")) ? $("div[title='" + title + "']") : $("div[id$='" + id + "']"));
}
//#endregion

No comments:

Post a Comment