Foros del Web » Programación para mayores de 30 ;) » .NET »

Validar Rut Chileno

Estas en el tema de Validar Rut Chileno en el foro de .NET en Foros del Web. Tengo una función para validar rut pero por ABC motivo, me valida solo algunos rut, lo cual no es lo esperado. Favor si alguien tubiese ...
  #1 (permalink)  
Antiguo 02/07/2010, 17:04
 
Fecha de Ingreso: mayo-2010
Mensajes: 5
Antigüedad: 14 años
Puntos: 0
Validar Rut Chileno

Tengo una función para validar rut pero por ABC motivo, me valida solo algunos rut, lo cual no es lo esperado.

Favor si alguien tubiese uno por ahi, que lo facilite porfavor

Saludos
  #2 (permalink)  
Antiguo 05/07/2010, 11:40
 
Fecha de Ingreso: octubre-2009
Mensajes: 30
Antigüedad: 14 años, 6 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.

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 00:56.