Ver Mensaje Individual
  #1 (permalink)  
Antiguo 07/12/2011, 06:55
milafa
 
Fecha de Ingreso: noviembre-2011
Mensajes: 29
Antigüedad: 13 años, 3 meses
Puntos: 0
Formatear Rut Chileno

Hola amigos del foro... mi consulta es la siguiente estoy haciendo un formulario donde debo darle formato a un rut chileno de la siguiente manera... a travez de un input el usuario ingresara su rut sin puntos ni guines y a travez de una funcion en javascript este automaticamente le pondra los puntos y el guion donde corresponda seria algo asi

123456789 y automaticamnete quedarian 12.345.678-9.... para formatear el rut tengo un codigo que me ayudaron a hacer y que realiza lo que mencione arriba el problema esta en que cuando se trata de un rut que es menor a 10 millones lo formatea asi

98765432 y lo deja asi.... 98.765.43-2 siendo que deberia dejarlo asi 9.876.543-2

no se si explique bien mi problema.... adjunto codigo para ver si alguien me puede echar una mano con esto que la verdad me tiene media complicada pq no entiendo mucho javascript



Código Javascript:
Ver original
  1. function Valida_Rut( Objeto )
  2. {
  3.     var tmpstr = "";
  4.     var intlargo = Objeto.value
  5.     if (intlargo.length> 0)
  6.     {
  7.         crut = Objeto.value
  8.         largo = crut.length;
  9.         if ( largo <2 )
  10.         {
  11.             alert('rut inválido')
  12.             Objeto.focus()
  13.             return false;
  14.         }
  15.         for ( i=0; i <crut.length ; i++ )
  16.         if ( crut.charAt(i) != ' ' && crut.charAt(i) != '.' && crut.charAt(i) != '-' )
  17.         {
  18.             tmpstr = tmpstr + crut.charAt(i);
  19.         }
  20.         rut = tmpstr;
  21.         crut=tmpstr;
  22.         largo = crut.length;
  23.    
  24.         if ( largo> 2 )
  25.             rut = crut.substring(0, largo - 1);
  26.         else
  27.             rut = crut.charAt(0);
  28.    
  29.         dv = crut.charAt(largo-1);
  30.    
  31.         if ( rut == null || dv == null )
  32.         return 0;
  33.    
  34.         var dvr = '0';
  35.         suma = 0;
  36.         mul  = 2;
  37.    
  38.         for (i= rut.length-1 ; i>= 0; i--)
  39.         {
  40.             suma = suma + rut.charAt(i) * mul;
  41.             if (mul == 7)
  42.                 mul = 2;
  43.             else
  44.                 mul++;
  45.         }
  46.    
  47.         res = suma % 11;
  48.         if (res==1)
  49.             dvr = 'k';
  50.         else if (res==0)
  51.             dvr = '0';
  52.         else
  53.         {
  54.             dvi = 11-res;
  55.             dvr = dvi + "";
  56.         }
  57.    
  58.         if ( dvr != dv.toLowerCase() )
  59.         {
  60.             alert('El Rut Ingreso es Invalido')
  61.             Objeto.value = "";
  62.             Objeto.focus()
  63.             return false;
  64.         }
  65.        
  66.         Objeto.focus()
  67.         return true;
  68.     }
  69. }
  70.  
  71. function renameRutAndProcess(obj) {
  72.     if (Valida_Rut(obj)) {
  73.         var rut = obj.value;
  74.         if (!/\./.test(rut)) {
  75.             var nuevo = "";
  76.             for (var i = 0; i < rut.length; i++) {
  77.                 if (i == 1 || i == 4) {
  78.                     nuevo += rut[i] + '.';
  79.                 } else if (i == (rut.length - 1)) {
  80.                     nuevo += '-' + rut[i];
  81.                 } else {
  82.                     nuevo += rut[i];
  83.                 }
  84.             }
  85.             obj.value = nuevo;
  86.         }
  87.     }
  88. }

espero anciosa que alguien me pueda ayudar.....

saludos ;)