Comunidad de diseño web y desarrollo en internet online

Utilizar funciones de RUT.js en un formulario HML

Citar            
MensajeEscrito el 11 Dic 2011 03:40 am
Que tal...bueno resulta ke tengo un codigo validarut.js, el cual como su nombre lo indica...validar un rut(codigo unico identifacor de chile algo asi como DNI a nivel general)

validarut.js

Código Javascript :


function revisarDigito( dvr )
{   
   dv = dvr + ""   
   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')   
   {      
      alert("Debe ingresar un digito verificador valido");      
      window.document.form1.rut.focus();      
      window.document.form1.rut.select();      
      return false;   
   }   
   return true;
}

function revisarDigito2( crut )
{   
   largo = crut.length;   
   if ( largo < 2 )   
   {      
      alert("Debe ingresar el rut completo")      
      window.document.form1.rut.focus();      
      window.document.form1.rut.select();      
      return false;   
   }   
   if ( largo > 2 )      
      rut = crut.substring(0, largo - 1);   
   else      
      rut = crut.charAt(0);   
   dv = crut.charAt(largo-1);   
   revisarDigito( dv );   

   if ( rut == null || dv == null )
      return 0   

   var dvr = '0'   
   suma = 0   
   mul  = 2   

   for (i= rut.length -1 ; i >= 0; i--)   
   {   
      suma = suma + rut.charAt(i) * mul      
      if (mul == 7)         
         mul = 2      
      else             
         mul++   
   }   
   res = suma % 11   
   if (res==1)      
      dvr = 'k'   
   else if (res==0)      
      dvr = '0'   
   else   
   {      
      dvi = 11-res      
      dvr = dvi + ""   
   }
   if ( dvr != dv.toLowerCase() )   
   {      
      alert("EL rut es incorrecto")      
      window.document.form1.rut.focus();      
      window.document.form1.rut.select();      
      return false   
   }

   return true
}

function Rut(texto)
{   
   var tmpstr = "";   
   for ( i=0; i < texto.length ; i++ )      
      if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
         tmpstr = tmpstr + texto.charAt(i);   
   texto = tmpstr;   
   largo = texto.length;   

   if ( largo < 2 )   
   {      
      alert("Debe ingresar el rut completo")      
      window.document.form1.rut.focus();      
      window.document.form1.rut.select();      
      return false;   
   }   

   for (i=0; i < largo ; i++ )   
   {         
      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" )
       {         
         alert("El valor ingresado no corresponde a un R.U.T valido");         
         window.document.form1.rut.focus();         
         window.document.form1.rut.select();         
         return false;      
      }   
   }   

   var invertido = "";   
   for ( i=(largo-1),j=0; i>=0; i--,j++ )      
      invertido = invertido + texto.charAt(i);   
   var dtexto = "";   
   dtexto = dtexto + invertido.charAt(0);   
   dtexto = dtexto + '-';   
   cnt = 0;   

   for ( i=1,j=2; i<largo; i++,j++ )   
   {      
      //alert("i=[" + i + "] j=[" + j +"]" );      
      if ( cnt == 3 )      
      {         
         dtexto = dtexto + '.';         
         j++;         
         dtexto = dtexto + invertido.charAt(i);         
         cnt = 1;      
      }      
      else      
      {            
         dtexto = dtexto + invertido.charAt(i);         
         cnt++;      
      }   
   }   

   invertido = "";   
   for ( i=(dtexto.length-1),j=0; i>=0; i--,j++ )      
      invertido = invertido + dtexto.charAt(i);   

   window.document.form1.rut.value = invertido.toUpperCase()      

   if ( revisarDigito2(texto) )      
      return true;   

   return false;
}




y kiero implementar un formulario que utilizando este script me haga la correspondiente validacion...en un formulario algo asi

Código HTML :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin t&iacute;tulo</title>
<script language="javascript" type="text/javascript" src="validarut.js"></script>
</head>

<body>
<form action="ingresar.php" method="post">
<table width="200" border="0">
  <tr>
    <td>Rut:</td>
    <td><input name="rut" type="text" /></td>
     </tr>
  <tr>
    <td>Nombre:</td>
    <td><input name="nombre" type="text"/></td>
   
  </tr>
</table>

</form>
</body>
</html>



en el primer input es donde quiero que me valide!

gracias!

Por vipeers

8 de clabLevel



 

chrome
Citar            
MensajeEscrito el 11 Dic 2011 05:14 am
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

Por vipeers

8 de clabLevel



 

chrome

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.