Ver Mensaje Individual
  #2 (permalink)  
Antiguo 05/07/2010, 11:40
ncmaster
 
Fecha de Ingreso: octubre-2009
Mensajes: 30
Antigüedad: 15 años, 4 meses
Puntos: 0
Respuesta: Validar Rut Chileno

Dos funciones que hacen lo mismo te entregan el digito verificador. Solo tienes que adaptarlo a lo tuyo.

Código vb:
Ver original
  1. Public Class Rut
  2.  
  3.     Private Sub Tb_rut_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Tb_rut.KeyPress
  4.         If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
  5.             e.Handled = True
  6.             Btn_consulta.Focus()
  7.         End If
  8.  
  9.         If InStr(1, "0123456789" & Chr(8), e.KeyChar) = 0 Then
  10.             e.Handled = True
  11.             e.KeyChar = CChar("")
  12.         End If
  13.     End Sub
  14.  
  15.     Private Sub Btn_consulta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_consulta.Click
  16.         Tb_rut.Text = Format(CInt(Tb_rut.Text), "00000000")
  17.         ComprobarRut()
  18.     End Sub
  19.  
  20.     Sub ComprobarRut()
  21.         Dim elnumero = Tb_rut.Text
  22.         Dim Resultado As String = ""
  23.         Dim Multiplicador As Integer = 2
  24.         Dim iNum As Integer = 0
  25.         Dim Suma As Integer = 0
  26.  
  27.         For i As Integer = 8 To 1 Step -1
  28.             iNum = CInt(Mid(elnumero, i, 1))
  29.             Suma += iNum * Multiplicador
  30.             Multiplicador += 1
  31.             If Multiplicador = 8 Then Multiplicador = 2
  32.         Next
  33.         Resultado = CStr(11 - (Suma Mod 11))
  34.         If Resultado = "10" Then Resultado = "K"
  35.         If Resultado = "11" Then Resultado = "0"
  36.         Tb_digito.Text = Resultado
  37.         Return
  38.     End Sub
  39.  
  40.     Private Sub Btn_consulta2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_consulta2.Click
  41.         Tb_rut.Text = Format(CInt(Tb_rut.Text), "00000000")
  42.         RutDigito()
  43.     End Sub
  44.  
  45.     Sub RutDigito()
  46.         Dim Rut As Integer = CInt(Tb_rut.Text)
  47.         Dim Digito As Integer
  48.         Dim Contador As Integer
  49.         Dim Multiplo As Integer
  50.         Dim Acumulador As Integer
  51.         Dim RutDigito As String
  52.  
  53.         Contador = 2
  54.         Acumulador = 0
  55.         While Rut <> 0
  56.             Multiplo = (Rut Mod 10) * Contador
  57.             Acumulador = Acumulador + Multiplo
  58.             Rut = Rut \ 10
  59.             Contador = Contador + 1
  60.             If Contador = 8 Then
  61.                 Contador = 2
  62.             End If
  63.         End While
  64.         Digito = 11 - (Acumulador Mod 11)
  65.         RutDigito = CStr(Digito)
  66.         If Digito = 10 Then RutDigito = "K"
  67.         If Digito = 11 Then RutDigito = "0"
  68.         Tb_digito.Text = RutDigito
  69.     End Sub
  70.  
  71. End Class

Saludos desde Chile.