Article View: chile.comp.programacion
Article #559Re: ayuda con validar rut
From: catfart
Date: Sun, 16 Mar 2008 20:29
Date: Sun, 16 Mar 2008 20:29
158 lines
4411 bytes
4411 bytes
wena llevai mas de una semana y con toda la ayuda todavia no lo sacas
Rick_oj escribi�:
> "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:
<frjvu5$4tf$1@news.nivel5.cl>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!news.kjsl.com!news.nivel5.cl!not-for-mailReferences:
<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> <frhnof$g2h$1@news.nivel5.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!news2.google.com!news.glorb.com!news.kjsl.com!news.nivel5.cl!not-for-mail From: catfart <me@here.com> Newsgroups: chile.comp.programacion Subject: Re: ayuda con validar rut Date: Sun, 16 Mar 2008 16:29:35 -0400 Organization: Varios Lines: 158 Message-ID: <frjvu5$4tf$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> <frhnof$g2h$1@news.nivel5.cl> NNTP-Posting-Host: pc-73-4-120-200.cm.vtr.net Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.nivel5.cl 1205699333 5039 200.120.4.73 (16 Mar 2008 20:28:53 GMT) X-Complaints-To: news@news.nivel5.cl NNTP-Posting-Date: Sun, 16 Mar 2008 20:28:53 +0000 (UTC) User-Agent: Thunderbird 1.5.0.14ubu (X11/20080227) In-Reply-To: <frhnof$g2h$1@news.nivel5.cl>