El noveno número nos permitirá validar si los otros ochos números del DNI son validos mediante el uso de un algoritmo.
Aquí le adjunto la función en VB.NET que hace la validación el DNI o Documento Nacional de Identidad del Perú.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | Public Function ValidaDNI(ByVal identificationDocument As String) As Boolean Dim flag As Boolean = False If Not String.IsNullOrEmpty(identificationDocument) Then Dim addition As Integer = 0 Dim hash As Integer() = {5, 4, 3, 2, 7, 6, 5, 4, 3, 2} Dim identificationDocumentLength As Integer = identificationDocument.Length Dim identificationComponent As String = identificationDocument.Substring(0, identificationDocumentLength - 1) Dim identificationComponentLength As Integer = identificationComponent.Length Dim diff As Integer = hash.Length - identificationComponentLength For i As Integer = identificationComponentLength - 1 To 0 Step -1 addition += CInt(identificationComponent(i).ToString) * hash(i + diff) Next addition = 11 - (addition Mod 11) If addition = 11 Then addition = 0 End If Dim last As Char = Char.ToUpperInvariant(identificationDocument(identificationDocumentLength - 1)) If identificationDocumentLength = 11 Then flag = addition.Equals(CInt(last.ToString)) ElseIf Char.IsDigit(last) Then Dim hashNumbers As Char() = {"6", "7", "8", "9", "0", "1", "1", "2", "3", "4", "5"} flag = last.Equals(hashNumbers(addition)) ElseIf Char.IsLetter(last) Then Dim hashLetters As Char() = {"K", "A", "B", "", "D", "E", "F", "G", "H", "I", "J"} flag = last.Equals(hashLetters(addition)) End If End If Return flag End Function |
No hay comentarios:
Publicar un comentario