Foros del Web » Programando para Internet » Javascript »

Modificar este script

Estas en el tema de Modificar este script en el foro de Javascript en Foros del Web. Tengo el siguiente script que consegui en http://javascript.internet.com/ , el script lo qeu hace es que a medida que voy insertando numeros en un campo ...
  #1 (permalink)  
Antiguo 20/11/2002, 13:15
 
Fecha de Ingreso: abril-2001
Ubicación: Puerto Montt
Mensajes: 89
Antigüedad: 23 años, 1 mes
Puntos: 1
Modificar este script

Tengo el siguiente script que consegui en http://javascript.internet.com/ , el script lo qeu hace es que a medida que voy insertando numeros en un campo de formulario, me da formato al numero con separador de miles y con dos espacios de decimales, lo que quiero es que solo me ponga las comas de separador de miles, pero que no me ponga el punto de los decimales, es decir, asumimos que no hay decimales, es un entero, que como normalmente es un numero muy largo necesito que tenga el separador de miles para que sea mas facil,
aqui pongo el script a ver si alguien me ayuda a modificarlo, please:



<HEAD>

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
function currencyFormat(fld, milSep, decSep, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13) return true; // Enter
key = String.fromCharCode(whichCode); // Get key value from key code
if (strCheck.indexOf(key) == -1) return false; // Not a valid key
len = fld.value.length;
for(i = 0; i < len; i++)
if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
aux = '';
for(; i < len; i++)
if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
aux += key;
len = aux.length;
if (len == 0) fld.value = '';
if (len == 1) fld.value = '0'+ decSep + '0' + aux;
if (len == 2) fld.value = '0'+ decSep + aux;
if (len > 2) {
aux2 = '';
for (j = 0, i = len - 3; i >= 0; i--) {
if (j == 3) {
aux2 += milSep;
j = 0;
}
aux2 += aux.charAt(i);
j++;
}
fld.value = '';
len2 = aux2.length;
for (i = len2 - 1; i >= 0; i--)
fld.value += aux2.charAt(i);
fld.value += decSep + aux.substr(len - 2, len);
}
return false;
}
// End -->
</script>

</HEAD>


<BODY>

<form>
Enter Value:
<input type=text name=test length=15 onKeyPress="return(currencyFormat(this,',','.',eve nt))">
</form>

<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>
  #2 (permalink)  
Antiguo 20/11/2002, 14:18
Avatar de Kaopectate
Colaborador
 
Fecha de Ingreso: diciembre-2001
Ubicación: Curaçao (Antillas Holandesas)
Mensajes: 3.179
Antigüedad: 22 años, 4 meses
Puntos: 38
Muy bueno el script, me dió dolor trasquilarlo. En algún momento lo generalizo para pasarle como parámetro el número de decimales.

Bueno, de cualquier modo es asi:

Código PHP:
<html>
 <
head>
  <
script language="JavaScript">
   function 
currencyFormat(fldmilSepdecSepe) {
    var 
sep 0;
    var 
key '';
    var 
0;
    var 
len len2 0;
    var 
strCheck '0123456789';
    var 
aux aux2 '';
    var 
whichCode = (window.Event) ? e.which e.keyCode;
    if (
whichCode == 13) return true// Enter
    
key String.fromCharCode(whichCode); // Get key value from key code
    
if (strCheck.indexOf(key) == -1) return false// Not a valid key
    
len fld.value.length;
    for(
0leni++)
     if ((
fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
    
aux '';
    for(; 
leni++)
     if (
strCheck.indexOf(fld.value.charAt(i))!=-1aux += fld.value.charAt(i);
    
aux += key;
    
len aux.length;
    
aux2 '';
    for (
0len 1>= 0i--) {
     if (
== 3) {
      
aux2 += milSep;
      
0;
     }
     
aux2 += aux.charAt(i);
     
j++;
    }
    
fld.value '';
    
len2 aux2.length;
    for (
len2 1>= 0i--)
     
fld.value += aux2.charAt(i);
    return 
false;
   }
  
</script>
 </head>
 <body>
  <form>
   Enter Value: 
   <input type="text" name="test" length="15" 

onKeyPress="return(currencyFormat(this,',','.',event))">
  </form>
 </body>
</html> 
Saludos.
  #3 (permalink)  
Antiguo 20/11/2002, 16:09
 
Fecha de Ingreso: abril-2001
Ubicación: Puerto Montt
Mensajes: 89
Antigüedad: 23 años, 1 mes
Puntos: 1
Muchisimas gracias, funciona perfecto, tal como lo necesitaba, muchas gracias por tu tiempo
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:35.