I'm developing a javascript mask for an input and am having a problem with the selection.removeAllRanges();
function every time I use it, it removes the cursor from the input and I can not use the backspace key to delete the values contained in input.
function fdecimal(vobj,vint,vdec)
{ var selection = window.getSelection (); selection.removeAllRanges ();
document.getElementById(vobj.id).select();
vlista = "-0123456789";
vchar = String.fromCharCode(event.which);
/*=== Caracter digitado deve estar parametrizado na lista (vlista) ===*/
if (vlista.indexOf(vchar,0) == -1)
return event.keyCode = 0;
vaux = vobj.value + vchar;
vaux = vaux.replace(",","");
/*=== tratamento sinal negativo ===*/
vneg = vaux.indexOf("-",0);
while (vaux.indexOf("-") != -1)
{
vaux = vaux.replace("-","");
}
/*=== Tamanho máximo numero ===*/
if(vaux.length > (vint + vdec)) {
return event.keyCode = 0;
}
/*=== Completa zeros a frente ===*/
vaux = "" + Number(vaux);
for (i=vaux.length;i<=vdec;i++)
{
vaux = '0' + vaux;
}
/*=== Coloca virgula como separador de decimal ===*/
vret = vaux.substr(0,vaux.length-vdec) + ',' + vaux.substr(vaux.length-vdec,vdec);
vobj.value = (vneg!=-1?'-':'') + vret;
selection.removeAllRanges();
}