Ver Mensaje Individual
  #5 (permalink)  
Antiguo 22/05/2012, 13:26
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 17 años, 3 meses
Puntos: 1567
Respuesta: Formato numérico

Es solo para enteros?, o tambien puede haber valores del tipo
1.000,32

Para enteros esto te podría servir
Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <title>Formatear Numero</title>
  5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6. <script type="text/javascript">
  7. //<![CDATA[
  8. function valida_envia(){
  9.      //valido el numero
  10.      var num = document.getElementById('numero');  
  11.      if (num.value.length==0){
  12.             alert("Tiene que escribir un valor")
  13.             num.focus()
  14.             return 0;
  15.      } else{
  16.      
  17.     var n=num.value.replace(/\./g,"");
  18.      alert(n);
  19.      num.value = n;
  20.      }
  21. }
  22.  
  23. function formatear(input){
  24. var num = input.value.replace(/\./g,'');
  25. if(!isNaN(num)){
  26. num = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1.');
  27. num = num.split('').reverse().join('').replace(/^[\.]/,'');
  28. input.value = num;
  29. }else{
  30. input.value = input.value.replace(/[^\d\.]*/g,'');
  31. }
  32. }
  33.  
  34. //]]>
  35. </head>
  36. <form id="fvalida" action="#" onsubmit="valida_envia();">
  37. <p>
  38. <input type="text" onkeyup="formatear(this)" onchange="formatear(this)" name="numero" id="numero" />
  39. <input type="submit" value="Enviar" />
  40. </p>
  41. </form>
  42.  
  43. </body>
  44. </html>

saludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.