Android Code Snippet Check if a string is CPF or CNPJ (is_CpfouCnpj)

Hi
Like isBase64 posted here
https://www.b4x.com/android/forum/threads/check-if-a-string-is-base64-isbase64.98444/
and isJson posted here
https://www.b4x.com/android/forum/threads/check-if-a-string-is-valid-json-isjson.99974/

Here is the check CPF or CNPJ (Brasil - people or companies)

B4X:
Sub is_CpfouCnpj(texto As String) As Boolean
    If Regex.IsMatch($"([0-9]{2}[\.]?[0-9]{3}[\.]?[0-9]{3}[\/]?[0-9]{4}[-]?[0-9]{2})|([0-9]{3}[\.]?[0-9]{3}[\.]?[0-9]{3}[-]?[0-9]{2})"$, texto.Trim) And texto.Length > 10  Then
        Return True
    Else
        Return False
    End If
End Sub


example of use
B4X:
    Log(is_CpfouCnpj("887.479.110-00"))
    Log(is_CpfouCnpj("88.479.110-00"))

The result log is
true
false

Credits: stackoverflow, Douglas Farias and JSON documentation
 
Last edited:
Top