0

I am making an input 'type text' that accepts decimal numbers. The input currently validates a max integer value and a maximum of decimal digits.

For example the data type Decimal(4, 2) in the method onKeyPress. But I am havig problems when the maximum number of decimal digits is reached and the user is editing the integer part.

Is there a way to know the index that is being edited so I can tell if the user is editing the integer part or the decimal part?

Here is what I have for now:

function validar_decimales(event, oTxt, enteros, decimales){
    var key = event.keyCode;
    console.log(JSON.stringify(event));
    var puntos = 0;
    var ents = 0;
    var decs = 0;
    var pasoPunto = false;
    if(oTxt.value.length == 0 && key == 46)
        return false;
    var str = "";
    
    for(var i = 0; i < oTxt.value.length; i++){
        if(oTxt.value[i] == "."){
            pasoPunto = true;
            puntos++;
            continue;
        }
        if(!pasoPunto){
            ents++;
        }else{
            decs++;
        }
    }
    if(event.keyCode == 46){
        if(puntos > 0)
            return false;
    }else
        if (key <= 13 || (key >= 48 && key <= 57)){
            if(pasoPunto){
                return decs < decimales;
            }else
                return ents < enteros;
        }else
            return false;
}
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    Use `event.target.selectionStart` to get the cursor position. –  Jun 07 '17 at 23:30
  • a bit difficult to understand. maybe make a fiddle/snippet so we can see your problem – A. L Jun 07 '17 at 23:33
  • You're welcome, but this question is a duplicate: https://stackoverflow.com/questions/2897155/get-cursor-position-in-characters-within-a-text-input-field so it'll probably get flagged and deleted soon –  Jun 08 '17 at 00:44

0 Answers0