Algoritm verificare CNP/CUI VB. NET 2010

Programare in Visual Bsic 2003 si ulterior
Visual Basic 2003 and later

Algoritm verificare CNP/CUI VB. NET 2010

Post Number:#1  Postby giv » 19 Dec 2014 15:09

Luate din VB6 si adaptate de mine.
CNP:
Code: Select all
    Public Function eValidCNP(ByVal cnp As String) As Boolean
        Dim evalid As Boolean = False
        Dim cnval As String = cnp
        Dim cn0 As String = cnval.ToString.Substring(0, 1)
        Dim cn1 As String = cnval.ToString.Substring(1, 1)
        Dim cn2 As String = cnval.ToString.Substring(2, 1)
        Dim cn3 As String = cnval.ToString.Substring(3, 1)
        Dim cn4 As String = cnval.ToString.Substring(4, 1)
        Dim cn5 As String = cnval.ToString.Substring(5, 1)
        Dim cn6 As String = cnval.ToString.Substring(6, 1)
        Dim cn7 As String = cnval.ToString.Substring(7, 1)
        Dim cn8 As String = cnval.ToString.Substring(8, 1)
        Dim cn9 As String = cnval.ToString.Substring(9, 1)
        Dim cn10 As String = cnval.ToString.Substring(10, 1)
        Dim cn11 As String = cnval.ToString.Substring(11, 1)
        Dim cnval1 As Double = cn0 * 2
        Dim cnval2 As Double = cn1 * 7
        Dim cnval3 As Double = cn2 * 9
        Dim cnval4 As Double = cn3 * 1
        Dim cnval5 As Double = cn4 * 4
        Dim cnval6 As Double = cn5 * 6
        Dim cnval7 As Double = cn6 * 3
        Dim cnval8 As Double = cn7 * 5
        Dim cnval9 As Double = cn8 * 8
        Dim cnval10 As Double = cn9 * 2
        Dim cnval11 As Double = cn10 * 7
        Dim cnval12 As Double = cn11 * 9
        cnval = cnval1 + cnval2 + cnval3 + cnval4 + cnval5 + cnval6 + cnval7 + cnval8 + cnval9 + cnval10 + cnval11 + cnval12
        Dim cnpcn As Double = cnval / 11
        Dim cnpcon As Double = cnpcn
        Dim cnc As String = Format(cnpcon, "0.00")
        Dim cncon As String = cnc
        Dim cncont As Array = cncon.ToCharArray
        Dim cifra As String = ""
        Dim cnpcontrolcifra As String = cncont(3) & cncont(4)
        Dim cncccc As String = cncont(3)
        Dim cnccc As Integer = cncccc
        If cnpcontrolcifra = "10" Then
            cifra = "1"
        Else
            If cncont(4) < "5" Then
                cifra = cnccc
            ElseIf cncont(4) >= "5" Then
                cifra = cnccc + 1
            End If
        End If
        If cnp.ToString.Substring(12, 1) <> cifra Then
            evalid = False
            MsgBox("CNP-ul nu este valid! Cifra de control neverificată!")
        ElseIf cnp.ToString.Substring(12, 1) = cifra Then
            evalid = True
        End If
        Return evalid
    End Function



CF:
Code: Select all
    Public Function IsValidCIF(ByVal CiF As String) As Boolean
        IsValidCIF = False
        Dim i As Integer
        On Error GoTo Eroare
        '****************************************
        'Pas preliminar: Se testeaza daca codul respecta formatul unui cod CIF.
        'Adica lungimea maxima sa fie de 10 cifre si sa contina doar caractere numerice.
        If CiF = "" Then
            IsValidCIF = False
            Exit Function
        End If
        CiF = Replace(CiF, " ", "")
        '****************************************
        If Len(CiF) < 2 Then
            IsValidCIF = False
            Exit Function
        End If
        '****************************************
        '- elimin caracterele de inceput RO sau altceva
        For i = 0 To Len(CiF) - 1
            If IsNumeric(Mid(CiF, i + 1, 1)) Then
                CiF = Mid(CiF, i + 1)
                GoTo sari
            End If
        Next i
sari:
        '****************************************
        '- verific fie maxim 10 caractere
        If Len(CiF) > 10 Then
            IsValidCIF = False
            Exit Function
        End If

        '****************************************
        'Pas 1: Se foloseste cheia de testare "753217532". Se inverseaza ordinea cifrelor codului
        'CIF precum si a cheii de testare.
        ' Array gata inversat
        Dim CheieTestare(8) As Integer
        CheieTestare(0) = 2
        CheieTestare(1) = 3
        CheieTestare(2) = 5
        CheieTestare(3) = 7
        CheieTestare(4) = 1
        CheieTestare(5) = 2
        CheieTestare(6) = 3
        CheieTestare(7) = 5
        CheieTestare(8) = 7
        '****************************************
        ' Array pentru CIF
        ' tot inversate
        Dim CIFArray(10)
        For i = 0 To Len(CiF) - 1
            CIFArray(i) = Mid(CiF, Len(CiF) - (i), 1)
            'MsgBox i & " -" & CIFArray(i)
        Next i
        'Pas 2: Se ignora prima cifra din codul CIF inversat (aceasta este cifra de control)
        'si se inmulteste fiecare cifra cu cifra corespunzatoare din cheia de testare inversata.
        Dim Total As Integer
        Total = 0
        For i = 1 To Len(CiF) - 1
            Total = Total + (CIFArray(i) * CheieTestare(i - 1))
            'MsgBox CIFArray(i) & " * " & CheieTestare(i - 1)
        Next i
        'Pas 3: Se aduna toate produsele obtinute. Suma rezultata se inmulteste cu 10 si
        'produsul este impartit la 11. Cifra obtinuta, in urma operatiei MODULO 11 reprezita
        'cifra de verificare. Daca in urma impartirii s-a obtinut restul 10 atunci
        'cifra de verificare va fi 0.
        Total = (Total * 10)
        Dim Rezultat As Integer
        Rezultat = Total Mod 11
        If Rezultat = 10 Then
            Rezultat = 0
        End If
        If Rezultat = CIFArray(0) Then
            IsValidCIF = True
        Else
            IsValidCIF = False
        End If
        Erase CIFArray
        Erase CheieTestare
        Exit Function
Eroare:
        IsValidCIF = False
    End Function
Best regards!
GIV
User avatar
giv
Admin
Admin
 
Status: Offline
Posts: 844
Age: 35
Joined: 02 Nov 2012 15:33
Location: Romania

Invitations sent: 3
Referrals: 25
National Flag:
Romania
Local time: 23 Sep 2017 12:18
Has thanked: 287 times
Been thanked: 325 times

Re: Algoritm verificare CNP/CUI VB. NET 2010

Post Number:#2  Postby giv » 31 Dec 2015 17:24

Dupa un timp am constatat ca acel cod vechi avea hibe.
Am facut un cod propriu.
Iata functia de validare CNP:

Code: Select all
 Public Shared Function validareCNP(ByVal CNP As String) As Boolean
        ' Codat de GIV
        ' giv@reversing.ro
        Dim evalid As Boolean = False
        If CNP.Length <> 13 Then
            evalid = False
            GoTo Sfarsit
        ElseIf CNP.Length = 13 Then
            Dim s1 As Integer = 2
            Dim s2 As Integer = 7
            Dim s3 As Integer = 9
            Dim s4 As Integer = 1
            Dim s5 As Integer = 4
            Dim s6 As Integer = 6
            Dim s7 As Integer = 3
            Dim s8 As Integer = 5
            Dim s9 As Integer = 8
            Dim s10 As Integer = 2
            Dim s11 As Integer = 7
            Dim s12 As Integer = 9
            Dim i0 As Integer = CInt(CNP.Substring(0, 1) * s1)
            Dim i1 As Integer = CInt(CNP.Substring(1, 1) * s2)
            Dim i2 As Integer = CInt(CNP.Substring(2, 1) * s3)
            Dim i3 As Integer = CInt(CNP.Substring(3, 1) * s4)
            Dim i4 As Integer = CInt(CNP.Substring(4, 1) * s5)
            Dim i5 As Integer = CInt(CNP.Substring(5, 1) * s6)
            Dim i6 As Integer = CInt(CNP.Substring(6, 1) * s7)
            Dim i7 As Integer = CInt(CNP.Substring(7, 1) * s8)
            Dim i8 As Integer = CInt(CNP.Substring(8, 1) * s9)
            Dim i9 As Integer = CInt(CNP.Substring(9, 1) * s10)
            Dim i10 As Integer = CInt(CNP.Substring(10, 1) * s11)
            Dim i11 As Integer = CInt(CNP.Substring(11, 1) * s12)
            Dim suma As Integer = i0 + i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9 + i10 + i11
            Dim rest As Integer = suma / 11
            Dim cifracontrol As Integer = suma - (11 * rest)
            If cifracontrol = 10 Then
                cifracontrol = 1
            End If
            If CNP.Substring(12, 1) = cifracontrol Then
                evalid = True
            Else
                evalid = False
            End If
        End If
Sfarsit:
        Return evalid
    End Function

Atasat e si un program care foloseste aceasta functie.
Attachments
Clipboard01.jpg
Clipboard01.jpg (4.74 KiB) Viewed 5443 times
ValidareCNP.rar
(9.59 KiB) Downloaded 374 times
Best regards!
GIV
User avatar
giv
Admin
Admin
 
Status: Offline
Posts: 844
Age: 35
Joined: 02 Nov 2012 15:33
Location: Romania

Invitations sent: 3
Referrals: 25
National Flag:
Romania
Local time: 23 Sep 2017 12:18
Has thanked: 287 times
Been thanked: 325 times


Return to VB .NET

Who is online

Users browsing this forum: No registered users and 1 guest

cron