Spanish Calculo para Verificar CIF España

scsjc

Well-Known Member
Licensed User
Hola a todos,
Estoy trabajando en un codigo para verificar los CIF españoles, y por ahora tengo este ejemplo que me funciona.
Pero estoy abierto a mejorar en lo posible, porque seguro alguna casualistica no lo verificara correctamente.

B4X:
Public Sub Verificar_CIF(valor As String) As Boolean
    Dim strLetra, strNumero, strDigit, strDigitAux As String
    Dim auxNum, i, suma As Int
    Dim letras As String = "ABCDEFGHKLMPQS"
    valor = valor.ToUpperCase
 
    If (valor.Length) < 9 Or IsNumber(Mid(valor, 2, 7))=False Then
        Log("El dato introducido no corresponde a un CIF")
        Return False
    End If
 
    strLetra = Mid(valor, 1, 1) 'letra del CIF
    strNumero = Mid(valor, 2, 7) 'CIF menos primera y ultima posiciones
    strDigit = Right(valor,1) 'Codigo de Control
 
    If letras.Contains(strLetra) = False Then 'comprobamos la letra del CIF (1ª posicion)
        Log("la letra introducida no corresponde a un CIF")
        Return False
    End If
 
   'calcula suma
    i = 0
    For i = 1 To 7
        If i Mod 2 = 0 Then
            suma = suma + CInt(Mid(strNumero, i, 1))
        Else
            auxNum = CInt(Mid(strNumero, i, 1)) * 2
            suma = suma + (auxNum / 10) + (auxNum Mod 10)
        End If
    Next
    suma = (10 - (suma Mod 10)) Mod 10

    Select Case strLetra
        Case "K", "P", "Q", "S"
            suma = suma + 64
            strDigitAux = Chr(suma)
        Case Else
            If IsNumber(strDigit) Then
                strDigitAux = suma
            Else
                Dim cif_codes As String = "JABCDEFGHI"
                If strDigit = (cif_codes.SubString2(suma,suma+1)) Then
                    strDigitAux = strDigit
                Else
                    strDigitAux = ""
                End If
            End If
    End Select

    If strDigit = strDigitAux Then
        Log("CIF correcto")
        Return True
    Else
        Log("El cif no es correcto, el dígito de control no coincide")
        Return False
    End If
End Sub
public Sub CInt(o As Object) As Int
    Return Floor(o)
End Sub
Sub Left(txt As String, caracteres As Int) As String
    Dim txtlongitud As Int = txt.Length
    If caracteres > txtlongitud Then caracteres = txtlongitud

    If txt="" Or caracteres<=0 Then
        Return ""
    Else
        Return txt.SubString2(0,caracteres)
    End If
End Sub
Sub Mid(txt As String, start As Int, Length As Int) As String
    Return txt.SubString2 ( (start-1), (start-1)+ Length )
End Sub
 

oparra

Well-Known Member
Licensed User
prueba ( y me dices como fue):
B4X:
Private Sub Button1_Click
    Log(Cif_Validation("A58818501"))
    Log(Cif_Validation("A12345678"))
    Log(Cif_Validation("A1234567890"))
End Sub

Public Sub Cif_Validation(CIF As String) As Boolean
    Dim Result As Boolean = False
    Dim Matcher1 As Matcher = Regex.Matcher("(^[ABCDEFGHIJKLMUV])(\d{7})(\d$)", CIF.ToUpperCase)
    If Not(Matcher1.Find) Then Return Result
    Dim Checksum = 0, Pos = 0 As Int
    For Each Val As Int In Regex.Split("", Matcher1.Group(2))
        Dim Sum As Int
        For Each Num As Int In Regex.Split("", (Val*(2-(Pos Mod 2))).As(String))
            Sum = Sum + Num
        Next
        Checksum = Checksum + Sum
        Pos = Pos + 1
    Next
    Checksum = ((10 - (Checksum Mod 10)) Mod 10)
    Return Matcher1.Group(3) = Checksum
End Sub
resultado:

1635219966691.png
 
Last edited:
Top