Buenas
Amigos, he desarrollado una función basada en el script escrito por tunait (mascara de entrada)
He agregado la funcionalidad de autocompletar, por ejemplo, si un usuario ingresa .5 automaticamente se convierta en 0.5 y asi en adelante, el valor que se rellena es configurable y se envia como parámetro de la función.
Los parámetros estan definidos asi:
Código:
mascara(d,sep,rel,pat,n)
d = objeto de form (this)
sep = string separador
rel = string a utilizar como relleno
pat = array patron
n = si sólo acepta numeros (bool)
Podrian ayudarme a depurarlo? probándolo e indicándome los errores que encontréis (si lo hubieren).
Código HTML:
<html>
<head>
<title> Mascara </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript">
patron = new Array('2','2','4');
function mascara(d,sep,rel,pat,n)
{
val = d.value;
lar = val.length;
f = val.charAt(lar-1);
if (n)
{
if ( isNaN(f) && f != sep )
val = val.substr(0,lar-1);
}
if (f == sep)
{
x = val.split(sep)
xi = x.length -1;
val = '';
for (i=0; i < xi; i++)
{
for (j=0; j < pat[i] - x[i].length; j++)
val += rel;
val += x[i] + sep;
}
}
x= val.split(sep)
xi = x.length -1;
if (xi != 0)
{
aux = x[xi];
}
else
aux = val;
if ( aux.length > pat[xi])
{
x[xi] = aux.substr(0,pat[xi]) + sep + aux.substr(pat[xi]);
}
aux = '';
for (i=0; i<= xi; i++)
{
aux += x[i];
if ( xi > 0 && i < xi ) { aux += sep}
}
d.value = aux;
}
</script>
</head>
<body>
<form name="form2" method="post" action="">
<input name="masca" type="text" id="masca" onKeyUp="mascara(this,'.','0',patron,true)" maxlength="10">
</form>
</body>
</html>
Desde ya muchas gracias.
Saludos.