Foros del Web » Programando para Internet » Javascript »

utilizar validarut.js en un input de un formulario

Estas en el tema de utilizar validarut.js en un input de un formulario en el foro de Javascript en Foros del Web. que tal amigos: tal como lo dice el titulo:: quiero utilizar validarut.js en un input de un formulario HTML ....de modo que me valide el ...
  #1 (permalink)  
Antiguo 10/12/2011, 21:47
 
Fecha de Ingreso: noviembre-2010
Mensajes: 208
Antigüedad: 13 años, 5 meses
Puntos: 3
utilizar validarut.js en un input de un formulario

que tal amigos:

tal como lo dice el titulo:: quiero utilizar validarut.js en un input de un formulario HTML
....de modo que me valide el rut si es que es posible en el primer input de mi formulario.

aka va el codigo validarut.js
Código Javascript:
Ver original
  1. function revisarDigito( dvr )
  2. {  
  3.     dv = dvr + ""  
  4.     if ( dv != '0' && dv != '1' && dv != '2' && dv != '3' && dv != '4' && dv != '5' && dv != '6' && dv != '7' && dv != '8' && dv != '9' && dv != 'k'  && dv != 'K')
  5.     {      
  6.         alert("Debe ingresar un digito verificador valido");       
  7.         window.document.form1.rut.focus();     
  8.         window.document.form1.rut.select();    
  9.         return false;  
  10.     }  
  11.     return true;
  12. }
  13.  
  14. function revisarDigito2( crut )
  15. {  
  16.     largo = crut.length;   
  17.     if ( largo < 2 )   
  18.     {      
  19.         alert("Debe ingresar el rut completo")     
  20.         window.document.form1.rut.focus();     
  21.         window.document.form1.rut.select();    
  22.         return false;  
  23.     }  
  24.     if ( largo > 2 )       
  25.         rut = crut.substring(0, largo - 1);
  26.     else       
  27.         rut = crut.charAt(0);  
  28.     dv = crut.charAt(largo-1); 
  29.     revisarDigito( dv );   
  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.     res = suma % 11
  47.     if (res==1)    
  48.         dvr = 'k'  
  49.     else if (res==0)       
  50.         dvr = '0'  
  51.     else   
  52.     {      
  53.         dvi = 11-res       
  54.         dvr = dvi + "" 
  55.     }
  56.     if ( dvr != dv.toLowerCase() ) 
  57.     {      
  58.         alert("EL rut es incorrecto")      
  59.         window.document.form1.rut.focus();     
  60.         window.document.form1.rut.select();    
  61.         return false   
  62.     }
  63.  
  64.     return true
  65. }
  66.  
  67. function Rut(texto)
  68. {  
  69.     var tmpstr = "";   
  70.     for ( i=0; i < texto.length ; i++ )    
  71.         if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
  72.             tmpstr = tmpstr + texto.charAt(i); 
  73.     texto = tmpstr;
  74.     largo = texto.length;  
  75.  
  76.     if ( largo < 2 )   
  77.     {      
  78.         alert("Debe ingresar el rut completo")     
  79.         window.document.form1.rut.focus();     
  80.         window.document.form1.rut.select();    
  81.         return false;  
  82.     }  
  83.  
  84.     for (i=0; i < largo ; i++ )
  85.     {          
  86.         if ( texto.charAt(i) !="0" && texto.charAt(i) != "1" && texto.charAt(i) !="2" && texto.charAt(i) != "3" && texto.charAt(i) != "4" && texto.charAt(i) !="5" && texto.charAt(i) != "6" && texto.charAt(i) != "7" && texto.charAt(i) !="8" && texto.charAt(i) != "9" && texto.charAt(i) !="k" && texto.charAt(i) != "K" )
  87.         {          
  88.             alert("El valor ingresado no corresponde a un R.U.T valido");          
  89.             window.document.form1.rut.focus();         
  90.             window.document.form1.rut.select();        
  91.             return false;      
  92.         }  
  93.     }  
  94.  
  95.     var invertido = "";
  96.     for ( i=(largo-1),j=0; i>=0; i--,j++ )     
  97.         invertido = invertido + texto.charAt(i);   
  98.     var dtexto = "";   
  99.     dtexto = dtexto + invertido.charAt(0); 
  100.     dtexto = dtexto + '-'; 
  101.     cnt = 0;   
  102.  
  103.     for ( i=1,j=2; i<largo; i++,j++ )  
  104.     {      
  105.         //alert("i=[" + i + "] j=[" + j +"]" );    
  106.         if ( cnt == 3 )    
  107.         {          
  108.             dtexto = dtexto + '.';         
  109.             j++;           
  110.             dtexto = dtexto + invertido.charAt(i);         
  111.             cnt = 1;       
  112.         }      
  113.         else       
  114.         {              
  115.             dtexto = dtexto + invertido.charAt(i);         
  116.             cnt++;     
  117.         }  
  118.     }  
  119.  
  120.     invertido = "";
  121.     for ( i=(dtexto.length-1),j=0; i>=0; i--,j++ )     
  122.         invertido = invertido + dtexto.charAt(i);  
  123.  
  124.     window.document.form1.rut.value = invertido.toUpperCase()      
  125.  
  126.     if ( revisarDigito2(texto) )       
  127.         return true;   
  128.  
  129.     return false;
  130. }

y quiero utilizar esto en un archivo aparte HTML. en el primer input de un formulario.

algo asi

formulario.html

Código HTML:
Ver original
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  3. <title>Documento sin t&iacute;tulo</title>
  4. <script language="javascript" type="text/javascript" src="validarut.js"></script>
  5. </head>
  6.  
  7. <form action="ingresar.php" method="post">
  8. <table width="200" border="0">
  9.   <tr>
  10.     <td>Rut:</td>
  11.     <td><input name="rut" type="text" /></td>
  12.      </tr>
  13.   <tr>
  14.     <td>Nombre:</td>
  15.     <td><input name="nombre" type="text"/></td>
  16.    
  17.   </tr>
  18.  
  19. </form>
  20. </body>
  21. </html>
  22.  
  23.  
  24. gracias de antemano!
  #2 (permalink)  
Antiguo 10/12/2011, 22:37
 
Fecha de Ingreso: noviembre-2010
Mensajes: 208
Antigüedad: 13 años, 5 meses
Puntos: 3
Respuesta: utilizar validarut.js en un input de un formulario

bueno me respondo yo mesmo...

probe con esto

<input name="rut" type="text" onclick="Rut(rut)" />

pero me valida el rut solo al clikear dentro de la caja
  #3 (permalink)  
Antiguo 10/12/2011, 23:17
 
Fecha de Ingreso: noviembre-2010
Mensajes: 208
Antigüedad: 13 años, 5 meses
Puntos: 3
Respuesta: utilizar validarut.js en un input de un formulario

bueno.....en el transcurso de la noche y como nadie lo respondio....y probando y probando....me resulto


el asunto era agregar en el formulario lo siguiente

<form action="ingresar.php" method="post" name=form1>

y en el input lo siguiente:

<input name="rut" type="text" onblur="Rut(rut)" />


entonces onblur....va a la funcion Rut y si es incorrecto o incompleto el rut....se mantiene en el campo rut......como??.....con el focus y select que son las lineas que aparecen en el codigo validarut.js....en ke parte ...aka

alert("Debe ingresar el rut completo")
window.document.form1.rut.focus();
window.document.form1.rut.select();


eso

Etiquetas: formulario, html, input, js, php
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 12:13.