🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: chile.comp.programacion
Article #547

Re: ayuda con validar rut

#547
From: Mauricio Gomez
Date: Wed, 12 Mar 2008 02:52
267 lines
7805 bytes
Cual es el problema? que no te funciona o queda feo con muchos charat()?

Pagina:

http://www.juque.cl/demos/validador.rut.javascript/


Codigo:

//
// Validador de Rut
// Descargado desde http://www.juque.cl/
//
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;
}

rro


Rick_oj wrote:
> vale pero no me sirve en vb
> :(
> 
> "gato2008chile" <gato2008chileSINESTO@gmail.com> escribi� en el mensaje 
> news:fr7ao1$d1s$1@news.nivel5.cl...
>> En VB hice uso de dos eventos y funciones, una validaba el formato y otra 
>> el digito.
>> chRut valida el formato, debe ser de la forma 123456-7,solo permite el 
>> ingreso de numeros y "-", verifica que si el caracter es ingresado es "-" 
>> este en la posicion largo-1,etc.
>>
>> En el evento change
>> txtRut.Text = chRut(txtRut.Text)
>> txtRut.SelStart = Len(txtRut.Text)
>>
>> En el evento keypress
>>
>> txtRut.Text = chRut(txtRut.Text)
>> If KeyAscii = 13 Then
>> ValRut(texto1.text)
>> End If
>>
>>
>>
>> Public Function ValRut(ByVal cadena As String) As Boolean
>> Dim ln As Integer, tp As Integer, i As Integer, _
>> j As Integer, sum As Integer, ctemp As String
>>
>> ln = Len(cadena)
>> If ln < 3 Then
>>     ValRut = False
>> ElseIf ln >= 3 Then
>>     tp = InStr(1, cadena, "-", vbTextCompare)
>>     If tp = 0 Then
>>         ValRut = False
>>     ElseIf tp <> (ln - 1) Then
>>         ValRut = False
>>     Else
>>         sum = 0
>>         j = 2
>>         For i = (tp - 1) To 1 Step -1
>>             st = Val(Mid(cadena, i, 1))
>>             If st >= 0 And st <= 9 Then
>>                 sum = sum + st * j
>>                 j = j + 1
>>                 If j = 8 Then
>>                     j = 2
>>                 End If
>>             Else
>>                 ValRut = False
>>                 Exit Function
>>             End If
>>         Next
>>         dv = sum Mod 11
>>         ctemp = Mid(cadena, ln, 1)
>>         If dv = 0 Then
>>             If ctemp = "k" Or ctemp = "K" Then
>>                 ValRut = True
>>             Else
>>                 ValRut = False
>>             End If
>>         Else
>>
>>             If (11 - dv) = Val(ctemp) Then
>>                 ValRut = True
>>             Else
>>                 ValRut = False
>>             End If
>>         End If
>>     End If
>> End If
>> End Function
>>
>>
>> Public Function chRut(ByVal cadena As String) As String
>> Dim ln As Integer
>> Dim pos As Integer
>> Dim cs As Integer
>> Dim cadt As String
>>
>> ln = Len(cadena)
>> pos = InStr(cadena, "-")
>> If pos = 0 Then
>>     chRut = IIf(Val(cadena) = 0, "", Format(Val(cadena)))
>> ElseIf pos <= 4 Then
>>     chRut = ""
>> ElseIf pos = ln Then
>>     chRut = Format(Val(Left(cadena, ln - 1))) & "-"
>> ElseIf pos <= (ln - 1) Then
>>     cadt = Left(cadena, pos + 1)
>>     cs = Asc(Right(cadt, 1))
>>     If ((cs < 48 Or cs > 57) And cs <> 13) _
>>         And cs <> 45 Then
>>             chRut = Left(cadt, ln - 1)
>>     Else
>>             chRut = cadt
>>     End If
>> End If
>> End Function
>>
>>
>> Rick_oj escribi�:
>>> "Rick_oj" <rick_oj[sin-esto]@tutopia.com> escribi� en el mensaje 
>>> news:fqq6gv$uj2$1@news.nivel5.cl...
>>>> necesito validar el dichoso rut en un mismo campo de texto con el guion, 
>>>> he encontrado varios script qu ehacen lo mismo, pero al aplicarlo no me 
>>>> funciona, encontre este que es mas sencillo, pero lo hace en dos campos 
>>>> de textos, y lo hace automatico (el digito verificador)
>>> nadie?
>>> :( 
> 
> 
Technical Details
Message-ID: <fr7feh$dm2$1@news.nivel5.cl>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!out04b.usenetserver.com!news.usenetserver.com!in01.usenetserver.com!news.usenetserver.com!news-xfer.nntp.sonic.net!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>

Show Full Headers
Complete Article Headers
X-Google-Thread: 1593cb,80adbb2b571c3e5b
X-Google-Attributes: gid1593cb,public,usenet
X-Google-Language: ENGLISH,ASCII
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!out04b.usenetserver.com!news.usenetserver.com!in01.usenetserver.com!news.usenetserver.com!news-xfer.nntp.sonic.net!news.kjsl.com!news.nivel5.cl!not-for-mail
From: Mauricio Gomez <pregunte@sileinteresa.com>
Newsgroups: chile.comp.programacion
Subject: Re: ayuda con validar rut
Date: Tue, 11 Mar 2008 22:52:54 -0400
Organization: Varios
Lines: 267
Message-ID: <fr7feh$dm2$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>
NNTP-Posting-Host: 190.161.112.177
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.nivel5.cl 1205289233 14018 190.161.112.177 (12 Mar 2008 02:33:53 GMT)
X-Complaints-To: news@news.nivel5.cl
NNTP-Posting-Date: Wed, 12 Mar 2008 02:33:53 +0000 (UTC)
User-Agent: Thunderbird 2.0.0.6 (X11/20071022)
In-Reply-To: <fr7eq4$diq$1@news.nivel5.cl>