none
Como Validar um CNPJ em Visual Basic.NET 2010 RRS feed

  • Discussão Geral

  • Public Shared Function IsValidaCNPJ(ByVal value As String) As Boolean
            Dim multiplier1() As Integer = New Integer() {5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}
            Dim multiplier2() As Integer = New Integer() {6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}
            Dim sum As Integer = 0
            Dim leftOver As Integer
            Dim digit As String
            value = value.Trim()
            value = value.Replace(".", "")
            value = value.Replace("/", "")
            value = value.Replace("-", "")
            Dim tempCPF As String = value
            If Not value.Length = 14 Then
                Return False ' Maior/Menor que 14 dígitos
            End If
            tempCPF = tempCPF.Substring(0, 12)
            For i As Integer = 0 To 11
                sum = sum + Integer.Parse(tempCPF(i).ToString * multiplier1(i))
            Next
            leftOver = sum Mod 11
            If leftOver < 2 Then
                leftOver = 0
            Else
                leftOver = 11 - leftOver
            End If
            digit = leftOver.ToString
            tempCPF = tempCPF + digit
            sum = 0
            For i As Integer = 0 To 12
                sum = sum + Integer.Parse(tempCPF(i).ToString * multiplier2(i))
            Next
            leftOver = sum Mod 11
            If leftOver < 2 Then
                leftOver = 0
            Else
                leftOver = 11 - leftOver
            End If
            digit = digit + leftOver.ToString
            Return value.EndsWith(digit)
        End Function ' IsValidaCNPJ


    segunda-feira, 10 de setembro de 2012 12:44