Article View: chile.comp.programacion
Article #543Re: ayuda con validar rut
From: gato2008chile
Date: Wed, 12 Mar 2008 02:13
Date: Wed, 12 Mar 2008 02:13
108 lines
3937 bytes
3937 bytes
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:
<fr7ao1$d1s$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-mailReferences:
<fqq6gv$uj2$1@news.nivel5.cl> <fr62f0$80a$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!news1.google.com!news.glorb.com!news.kjsl.com!news.nivel5.cl!not-for-mail From: gato2008chile <gato2008chileSINESTO@gmail.com> Newsgroups: chile.comp.programacion Subject: Re: ayuda con validar rut Date: Tue, 11 Mar 2008 22:13:13 -0400 Organization: Varios Lines: 108 Message-ID: <fr7ao1$d1s$1@news.nivel5.cl> References: <fqq6gv$uj2$1@news.nivel5.cl> <fr62f0$80a$1@news.nivel5.cl> NNTP-Posting-Host: pc-191-74-44-190.cm.vtr.net Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.nivel5.cl 1205284417 13372 190.44.74.191 (12 Mar 2008 01:13:37 GMT) X-Complaints-To: news@news.nivel5.cl NNTP-Posting-Date: Wed, 12 Mar 2008 01:13:37 +0000 (UTC) User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.8.1.12) Gecko/20080213 Thunderbird/2.0.0.12 Mnenhy/0.7.5.0 In-Reply-To: <fr62f0$80a$1@news.nivel5.cl>