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;
}