Article View: chile.comp.programacion
Article #555Re: ayuda con validar rut
From: Alberto Hormazabal C.
Date: Fri, 14 Mar 2008 21:25
Date: Fri, 14 Mar 2008 21:25
144 lines
3843 bytes
3843 bytes
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
:)
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:
<47daed3c@news.vcd.cl>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!news.kjsl.com!news.nivel5.cl!news.vcd.cl!not-for-mail!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>Show Full Headers
Complete Article Headers
X-Google-Thread: 1593cb,80adbb2b571c3e5b
X-Google-Attributes: gid1593cb,public,usenet
X-Google-Language: SPANISH,ASCII
Message-ID: <47daed3c@news.vcd.cl>
From: "Alberto Hormazabal C." <tuto@lj.cl>
Subject: Re: ayuda con validar rut
Newsgroups: chile.comp.programacion
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>
User-Agent: tin/1.9.2-20070201 ("Dalaruan") (UNIX) (Linux/2.6.18-5-686 (i686))
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
NNTP-Posting-Host: 200.27.116.178
X-Original-NNTP-Posting-Host: 200.27.116.178
Date: 14 Mar 2008 17:25:16 -0400
X-Trace: news.vcd.cl!not-for-mail 1205529916 200.27.116.178 (14 Mar 2008 17:25:16 -0400)
X-Original-Trace: 14 Mar 2008 17:25:16 -0400, 200.27.116.178
Lines: 145
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!news.kjsl.com!news.nivel5.cl!news.vcd.cl!not-for-mail!not-for-mail