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

Discussion in 'Code Snippets' started by Douglas Farias, Dec 2, 2018.

  1. Douglas Farias

    Douglas Farias Expert Licensed User

    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)

    Code:
    Sub is_CpfouCnpj(texto As StringAs 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
    Code:
    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: Dec 2, 2018
    asales likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice