lunes, mayo 14, 2012

Validación del NIT

Este es otro código que he conseguido y lo paso para los usos que les convengan.
Es el código para VB para la validación del Número de Identificación Tributaria NIT de Guatemala.

 
Public Function ValidaNit(ByVal NIT As String) As Boolean
    Dim POS As Integer

    Dim Correlativo As String

    Dim DigitoVerificador As String

    Dim Factor As Integer

    Dim Suma As Integer = 0

    Dim Valor As Integer = 0

    Dim X As Integer

    Dim xMOD11 As Double = 0

    Dim S As String = Nothing


    ValidaNit = False


    Try
        POS = NIT.IndexOf("-")
        If POS = 0 Then Exit Function
        Correlativo = NIT.Substring(0, POS)

        DigitoVerificador = NIT.Substring(POS + 1)
        Factor = Correlativo.Length + 1

        For X = 0 To (NIT.IndexOf("-") - 1)
            Valor = Convert.ToInt32(NIT.Substring(X, 1))

            Suma += (Valor * Factor)

            Factor -= 1
        Next


        xMOD11 = (11 - (Suma Mod 11)) Mod 11

        S = Convert.ToString(xMOD11)


        If (xMOD11 = 10 And DigitoVerificador = "K") Or (S = DigitoVerificador) Then
         ValidaNit = True

        End If


    Catch ex As Exception

        MessageBox.Show(ex.Message, "Validando NIT", MessageBoxButtons.OK, _
                        MessageBoxIcon.Exclamation)
    End Try
End Function

 Esta función funciona con VB Net y cambiandole un par de cositas tambien con vb 6 y VBA.

Devuelve un falso cuando el NIT no sea correcto