Views

Histats

Vitrine

VBA Tips - Avalia o endereço do email - Validating An Email Address

Termo de Responsabilidade


Que tal validar um endereço de e-mail ou uma lista deles?

A função é esta: AvalMail ("bernardess@gmail.com")

Function AvalMail (ByVal EAddress As String) As Boolean
    ' Variáveis dimensionadas.
    Const AllowChars = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz._-"

    Dim UserName As String
    Dim ServerName As String
    Dim x As Long
    Dim i As Integer
    
    'Validate email address.
    Let x = InStr(1, EAddress, "@")
    
    If x = 0 Then GoTo BadAddress
    If InStr(x + 1, EAddress, "@") > 0 Then GoTo BadAddress
    
    Let UserName = Left$(EAddress, x - 1)
    Let ServerName = Right$(EAddress, Len(EAddress) - x)
    
    If Left$(UserName, 1) = "." Or Right$(UserName, 1) = "." Then GoTo BadAddress
    If Left$(ServerName, 1) = "." Or Right$(ServerName, 1) = "." Or InStr(1, ServerName, ".") = 0 Then GoTo BadAddress
    
    For i = 1 To Len(UserName)
        If InStr(1, AllowChars, Mid$(UserName, i, 1)) = 0 Then GoTo BadAddress
    Next
    
    For i = 1 To Len(ServerName)
        If InStr(1, AllowChars, Mid$(ServerName, i, 1)) = 0 Then GoTo BadAddress
    Next
    
    Let AvalMail = True

    Exit Function

BadAddress:
    Let AvalMail = False
End Function

References:

Tags: VBA, Tips, email, validade, avalia, checa, valida


Inline image 1

LinkWithinBrazilVBAExcelSpecialist

Related Posts Plugin for WordPress, Blogger...