ADS

Featured

CPF Validation in ASP (VBSCRIPT)

Suddenly we need a validator via server-side CPF, since several on the internet do not work correctly and others accept incorrect values, following is a code found that is fully functional for any type of CPF.

Remember, server-side validation is essential to ensure that data is not going wrong on your system.




 

Function IsCPF(Cpf)
    Dim multiplic1,multiplic2
    multiplic1=Array(10,9,8,7,6,5,4,3,2)
    multiplic2=Array(11,10,9,8,7,6,5,4,3,2)
    Dim tempCpf,digit,sum,remainder,i,RegXP
    Cpf = Trim(Cpf)
    Cpf = Replace(Cpf,".", "")
    Cpf = Replace(Cpf,"-", "")
    If (Len(Cpf) <> 11) Then
        IsCPF = False
    Else
        tempCpf = Left(Cpf,9)
        sum = 0
        Dim intCounter
        Dim intLen
        Dim arrChars()
        intLen = Len(tempCpf)-1
        Redim arrChars(intLen)
        For intCounter = 0 to intLen
            arrChars(intCounter) = Mid(tempCpf, intCounter + 1,1)
        Next
        i=0
        For i = 0 to 8
            sum =sum + CInt(arrChars(i)) * multiplic1(i)
        Next
        remainder = sum Mod 11
        If (remainder < 2) Then
            remainder = 0
        Else
            remainder = 11 - remainder
        End If
        digit = CStr(remainder)
        tempCpf = tempCpf & digit
        sum = 0
        intLen = Len(tempCpf)-1
        Redim arrChars(intLen)
        intCounter= 0
        For intCounter = 0 to intLen
            arrChars(intCounter) = Mid(tempCpf, intCounter + 1,1)
        Next
        i=0
        For i = 0 to 9
            sum =sum + CInt(arrChars(i)) * multiplic2(i)
        Next     
        remainder = sum Mod 11
        If (remainder < 2) Then
            remainder = 0
        Else
            remainder = 11 - remainder
        End If   
        digit = digit & CStr(remainder)
        Set RegXP=New RegExp
            RegXP.IgnoreCase=1
            RegXP.Pattern=digit & "$"
        If RegXP.test(Cpf) Then
RegXP.Pattern="\b(\d)\1+\b"
If Not RegXP.test(Cpf) Then
IsCPF = True
Else
IsCPF = False
End if
        Else
            IsCPF = False
        End If
    End If
End Function

Source: http://stackoverflow.com/questions/11668350/cpf-validation-in-classic-asp

2 comments:

  1. Com essa alteraçaõ, se o número do cpf começar com 0 da erro de divisão por 0.

    ReplyDelete