🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: chile.comp.programacion
Article #557

Re: ayuda con validar rut

#557
From: Rickoj
Date: Sun, 16 Mar 2008 00:57
155 lines
4118 bytes
"Alberto Hormazabal C." <tuto@lj.cl> escribi� en el mensaje 
news:47daed3c@news.vcd.cl...
> Rick_oj visito chile.comp.programacion y balbuceo:
>> baje ese de ahi, y no me funciono
>> voy a revisar nuevamente
>
> tengo un par de funciones en js pa validar rut, pero no me acuerdo cual de 
> las 2
> es la que funciona bien bien.
> te dejo otras utiles tambienb
>
> ojala te sirvan
>
> :)


vale te aviso cual me funciono gracias



>
>
>
> Validador de Rut 1
> ==================
> function validaRut(rut)
> {
> var rutlimpio = "";
> for (i=0; i < rut.length; i++){
> if ((rut.charAt(i)!=".") && (rut.charAt(i)!="-"))
> rutlimpio= rutlimpio + rut.charAt(i);
> }
> rut=rutlimpio;
> var drut=rut.charAt((rut.length-1));
> var dvr='0'
> suma=0
> mul=2
> for (i=rut.length -2 ; 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 != drut.toLowerCase())){
> //alert("El RUT ingresado no es v�lido.");
> return false;
> }
> return true;
> }
>
>
> Validador Rut 2
> ===============
> function esRutValido(rut)
> {
> if(rut.length < 2) return false;
>
> digito=rut.charAt(rut.length-1).toUpperCase();
> cuerpo=rut.substr(0,rut.length-1);
>
> intSuma = 0
> intFactor  = 2
> for (i= (cuerpo.length - 1)  ; i >= 0; i--)
> {
> intSuma = intSuma + cuerpo.charAt(i) * intFactor
> if (intFactor == 7)
> intFactor = 2
> else
> intFactor++
> }
> intResto = intSuma % 11
> if (intResto == 1)
> dvr = 'K'
> else if (intResto == 0)
> dvr = '0'
> else
> {
> dvi = 11 - intResto
> dvr = dvi + ""
> }
> if(dvr == digito)
> {
> return true;
> }
> else
> {
> return false;
> }
> }
>
>
>
> Otras funciones para RUT
> ========================
>
>
> // Desformatea un rut de la forma nnn.nnn.nnn-k
> // a la forma nnnnnnnnnk
> // acepta tanto puntos, comas, guiones como
> // cualquier otro caracter como separador de
> // miles y de digito verificador.
> function decodeRut(s)
> {
>  var rut = "";
>  for(i=0;i<s.length;i++)
>  {
>    a=s.charAt(i);
>    if( (a>='0' && a<='9') || a=='k' || a=='K' )
>      rut=rut+a;
>  }
>  return rut;
> }
>
>
>
> // Agrega el caracter <separador> como
> // separador de miles en el numero <nStr>
> function formatNumber(nStr,separador)
> {
>        nStr += '';
>        x = nStr.split('.');
>        x1 = x[0];
>        x2 = x.length > 1 ? '.' + x[1] : '';
>        var rgx = /(\d+)(\d{3})/;
>        while (rgx.test(x1)) {
>                x1 = x1.replace(rgx, '$1' + separador + '$2');
>        }
>        return x1 + x2;
> }
>
> // Convierte un rut de la forma nnnnnnnnnk a
> // la forma nnn.nnn.nnn-k
> function toRut(s)
> {
>  var dv;
>  var len;
>  len = s.length;
>  dv=s.substr(len-1,1);
>  return ( formatNumber(s.substr(0,len-1),'.') + "-" + dv );
> }
> -- 
> El Tuto
> tuto@aalc.lj.cl
> 
Technical Details
Message-ID: <frhnof$g2h$1@news.nivel5.cl>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news.kjsl.com!news.nivel5.cl!not-for-mail
References: <fqq6gv$uj2$1@news.nivel5.cl> <fr62f0$80a$1@news.nivel5.cl> <fr7ao1$d1s$1@news.nivel5.cl> <fr7eq4$diq$1@news.nivel5.cl> <fr7feh$dm2$1@news.nivel5.cl> <fr8mbi$hh0$1@news.nivel5.cl> <47daed3c@news.vcd.cl>

Show Full Headers
Complete Article Headers
X-Google-Thread: 1593cb,80adbb2b571c3e5b
X-Google-Attributes: gid1593cb,public,usenet
X-Google-Language: SPANISH,ASCII
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news.kjsl.com!news.nivel5.cl!not-for-mail
From: "Rick_oj" <rick_oj[sin-esto]@tutopia.com>
Newsgroups: chile.comp.programacion
Subject: Re: ayuda con validar rut
Date: Sat, 15 Mar 2008 20:57:00 -0400
Organization: Varios
Lines: 157
Message-ID: <frhnof$g2h$1@news.nivel5.cl>
References: <fqq6gv$uj2$1@news.nivel5.cl> <fr62f0$80a$1@news.nivel5.cl> <fr7ao1$d1s$1@news.nivel5.cl> <fr7eq4$diq$1@news.nivel5.cl> <fr7feh$dm2$1@news.nivel5.cl> <fr8mbi$hh0$1@news.nivel5.cl> <47daed3c@news.vcd.cl>
NNTP-Posting-Host: 190.160.68.32
X-Trace: news.nivel5.cl 1205625423 16465 190.160.68.32 (15 Mar 2008 23:57:03 GMT)
X-Complaints-To: news@news.nivel5.cl
NNTP-Posting-Date: Sat, 15 Mar 2008 23:57:03 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198