Results 1 to 5 of 5

Thread: Email Address Format Validation

  1. #1

    Thread Starter
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Email Address Format Validation

    The function below will help to validate whether an Email Address is in correct format.
    Code:
    Private Function IsValidEmailAddress(ByVal sEmail As String, _
                                         Optional ByRef sReason As String) As Boolean
       '-- Coded by Hoang Nguyen (anhn @ VBForums)
       '-- There may be some missing conditions
       '   or I am not sure whether they are valid or not.
       '-- Not validating format like:  peter@[10.11.12.13]
       '   or email address formats with explicit source route.
       '-- PLEASE USE WITH CAUTIONS, MODIFY IT AS YOU NEED
       '-------------------------------------------------------
       sEmail = LCase(Trim(sEmail))
       'IsValidEMailAddress = False
       If Len(sEmail) < 7 Then '-- Is [email protected] a valid email address?
          sReason = "Too short"
       ElseIf sEmail Like "*[!0-9a-z@._+-]*" Then
          '-- not sure about these characters: ! $ & ` ' * / \ = ? ^ | # % { } ~
          '   if required, add in to the above string after letter z and before the last hyphen -
          sReason = "Invalid character"
       ElseIf Not sEmail Like "*@*.*" Then
          sReason = "Missing the @ or ."
       ElseIf sEmail Like "*@*@*" Then
          sReason = "Too many @"
       ElseIf sEmail Like "[@.]*" Or sEmail Like "*[@.]" _
          Or sEmail Like "*..*" Or Not sEmail Like "?*@?*.*?" Then
          sReason = "Invalid format"
       Else
          Dim n As Integer
          n = Len(sEmail) - InStrRev(sEmail, ".")
          If n > 3 Then
             sReason = "Suffix too long"
          ElseIf n < 2 Then
             sReason = "Suffix too short"
          Else
             sReason = Empty
             IsValidEmailAddress = True
          End If
       End If
    End Function
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  2. #2

    Re: Email Address Format Validation

    I just made a little change to your pretty function, so that unaware or clumsy end-user can have more informations about email they type.

    Code:
    Private Function IsValidEmailAddress(ByVal sEmail As String, _
                                         Optional ByRef sReason As String) As Boolean
       '-- Coded by Hoang Nguyen (anhn @ VBForums)
       '-- There may be some missing conditions
       '   or I am not sure whether they are valid or not.
       '-- Not validating format like:  peter@[10.11.12.13]
       '   or email address formats with explicit source route.
       '-- PLEASE USE WITH CAUTIONS, MODIFY IT AS YOU NEED
       '-------------------------------------------------------
      
      'Made by alell: Changes for Multi-reason function for clumsy end-user ;-))
      
      sEmail = LCase(Trim(sEmail))
      'IsValidEMailAddress = False
      If Len(sEmail) < 7 Then '-- Is [email protected] a valid email address?
         sReason = "> Too short" & vbCrLf
      End If
      
      If sEmail Like "*[!0-9a-z@._+-]*" Then
         '-- not sure about these characters: ! $ & ` ' * / \ = ? ^ | # % { } ~
         '   if required, add in to the above string after letter z and before the last hyphen -
         sReason = sReason & "> Invalid character" & vbCrLf
      End If
      
      If Not sEmail Like "*@*.*" Then
         sReason = sReason & "> Missing the @ or ." & vbCrLf
      End If
      If sEmail Like "*@*@*" Then
         sReason = sReason & "> Too many @" & vbCrLf
      End If
      'Added by alell to avoid validation of "aaaa.bbbb.com"
      If sEmail Like "*.*.*" Then
         sReason = sReason & "> Too many ." & vbCrLf
      End If
      If sEmail Like "[@.]*" Or sEmail Like "*[@.]" _
         Or sEmail Like "*..*" Or Not sEmail Like "?*@?*.*?" Then
         sReason = sReason & "> Invalid format" & vbCrLf
      End If
      
      Dim n As Integer
      If InStrRev(sEmail, ".") > 0 Then
        n = Len(sEmail) - InStrRev(sEmail, ".")
        If n > 3 Then
           sReason = sReason & "> Suffix too long"
        ElseIf n < 2 Then
           sReason = sReason & "> Suffix too short"
        Else
          If sReason <> "" Then Exit Function
           sReason = Empty
           IsValidEmailAddress = True
        End If
      End If
        
    End Function
    Bye

  3. #3

    Thread Starter
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Email Address Format Validation

    Initial idea is the function will detect one error at a time. An email address will be failed straight away if one condition is not matched, instead of checking all conditions.

    As stated, "MODIFY IT AS YOU NEED", however one feature you added that fails all non-US email addresses:
    Code:
      'Added by alell to avoid validation of "aaaa.bbbb.com"
      If sEmail Like "*.*.*" Then
         sReason = sReason & "> Too many ." & vbCrLf
      End If
    Have you ever received a non-US email address?
    These are valid email addresses:
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  4. #4
    New Member
    Join Date
    Aug 2010
    Posts
    1

    Re: Email Address Format Validation

    many thanks i have aadapted it as i get a tring separated by ;
    Code:
    Public Function IsValidEmailAddress(ByVal strEmail As String, Optional ByRef sReason As String) As Boolean
    
    On Error GoTo IsValidEmailAddress_Err
        
        Dim strEmailAdr As Variant
        Dim sEmail As String
        Dim i As Integer
        
        'cas if the string ends with only ;
        If InStrRev(strEmail, ";") = Len(strEmail) Then strEmail = Left(strEmail, Len(strEmail) - 1)
        strEmailAdr = Strings.Split(strEmail, ";")
        For i = LBound(strEmailAdr) To UBound(strEmailAdr)
            sEmail = LCase(Trim(CStr(strEmailAdr(i))))
            'sEmail = LCase(Trim(sEmail))
            IsValidEmailAddress = False
            If Len(sEmail) < 7 Then '-- Is [email protected] a valid email address?
               sReason = sEmail & " : Too short!"
            ElseIf sEmail Like "*[!0-9a-z@._+-]*" Then
               '-- not sure about these characters: ! $ & ` ' * / \ = ? ^ | # &#37; { } ~
               '   if required, add in to the above string after letter z and before the last hyphen -
               sReason = sEmail & " : Invalid character in email!"
            ElseIf Not sEmail Like "*@*.*" Then
               sReason = sEmail & " : Missing the @ or .!"
            ElseIf sEmail Like "*@*@*" Then
               sReason = sEmail & " : Too many @!"
            ElseIf sEmail Like "[@.]*" Or sEmail Like "*[@.]" _
               Or sEmail Like "*..*" Or Not sEmail Like "?*@?*.*?" Then
               sReason = sEmail & " : Invalid format!"
            Else
               Dim n As Integer
               n = Len(sEmail) - InStrRev(sEmail, ".")
               If n > 3 Then
                  sReason = sEmail & " : Suffix too long!"
               ElseIf n < 2 Then
                  sReason = sEmail & " : Suffix too short!"
               Else
                  sReason = Empty
                  IsValidEmailAddress = True
               End If
            End If
        Next i
    
      Exit Function
    IsValidEmailAddress_Err:
      MsgBox "PublicFonctions.IsValidEmailAddress : " & vbCrLf & _
             "Error " & Err.Number & " " & Err.Description
      On Error GoTo 0
    End Function
    
    '---------------------------------------------
    Sub testmail()
        Dim str As String
        MsgBox PublicFonctions.IsValidEmailAddress("[email protected];[email protected];aezaeaz.com", str)
        MsgBox str
    End Sub
    Last edited by si_the_geek; Aug 26th, 2010 at 04:36 AM. Reason: added Code tags

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    47

    Re: Email Address Format Validation

    Wow! Cool code. Super advance... Many thanks to you...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width