Saturday, July 20, 2013

Numeric Field Accept Only Defined decimal and Numeric Part And Block Non Numbers

function extractNumber(obj, numericPlaces, decimalPlaces, allowNegative, e) {
    //debugger;
    var temp = obj.value;
    // avoid changing things if already formatted correctly
    var ctrlDown;
    var akey = 65, vKey = 86, cKey = 67;

    ctrlDown = e.ctrlKey;
    if (((ctrlDown) && (e.keyCode == vKey || e.keyCode == cKey || e.keyCode == akey)) || e.keyCode == 17 ||
    event.keyCode == 37 || event.keyCode == 39) return true;

    var reg0Str = '^[0-9]{0,' + numericPlaces + '}(\\.[0-9][0-9]?)?$'; //reg0Str='[0-9]{0,4}\\.?[0-9]{0,2}$'
    var reg0 = new RegExp(reg0Str);
    if (reg0.test(temp)) return true;


    // first replace all non numbers
    var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
    var reg1 = new RegExp(reg1Str, 'g');
    temp = temp.replace(reg1, '');


    if (allowNegative) {
        // replace extra negative
        var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
        var reg2 = /-/g;
        temp = temp.replace(reg2, '');
        if (hasNegative) temp = '-' + temp;
    }


    if (decimalPlaces != 0) {
        var reg3 = /\./g;
        var reg3Array = reg3.exec(temp);
        if (reg3Array != null) {
            // keep only first occurrence of .
            //  and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
            var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
            reg3Right = reg3Right.replace(reg3, '');
            reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
            temp = temp.substring(0, reg3Array.index) + '.' + reg3Right;
        }
    }
    if (numericPlaces != 0) {
        var val = temp;


        var points = 0;
        points = val.indexOf(".", points);
        if (points == -1) {
            temp = numericPlaces > 0 ? temp.substring(0, numericPlaces) : temp;
        }
        else {
            var parts = val.split('.');
            if (parts[0].length <= numericPlaces) {
                // return true;
            }
            else {
                var nPart = parts[0];
                nPart = numericPlaces > 0 ? nPart.substring(0, numericPlaces) : nPart;
                temp = nPart + '.' + parts[1];
            }
        }
    }


    obj.value = temp;
    return true;
}


Block Non Numbers
------------------------
function blockNonNumbers(obj, e, allowDecimal, allowNegative) {
    //debugger;
    var ctrlDown = false;
    var ctrlKey = 17, akey = 65, vKey = 86, cKey = 67;

    if (e.keyCode == ctrlKey) ctrlDown = true;
    if (e.keyCode == ctrlKey) ctrlDown = false;
    if (ctrlDown && (e.keyCode == vKey || e.keyCode == cKey || e.keyCode == akey)) return true;


    var key = "";
    var isCtrl = false;
    var keychar;
    var reg;


    if (window.event) {
        key = e.keyCode;
        isCtrl = window.event.ctrlKey;
    }
    else if (e.which) {
        key = e.which;
        isCtrl = e.ctrlKey;
    }


    if (isNaN(key)) return true;


    keychar = String.fromCharCode(key);


    // check for backspace or delete, or if Ctrl was pressed
    if (key == 8 || isCtrl) {
        return true;
    }


    reg = /\d/;
    var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
    var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;


    return isFirstN || isFirstD || reg.test(keychar);
    // end of  text-box allow numeric and allow 2 decimal points only
}

1 comment:

  1. @onkeyup = "extractNumber(this,4,3,false,event);",
    @onkeypress = "return blockNonNumbers(this, event, true, false);",

    ReplyDelete