VB6 Email Address Validation Function

Email address validation is a little awkward, but here is a VB6 function to do the validation for you.

Simply paste this function into a module in your VB6 project and call it from anywhere in the project.

Public Function isValidEmailAddress(ByVal sIn As String) As Boolean

    ' VB6 Email address validation routine
    ' 11/01/10 - www.jack-frost.co.uk
    
    Dim sNames() As String
    Dim vName As Variant
    Dim i As Integer
    Dim sChar As String

    isValidEmailAddress = False

    sNames = Split(sIn, "@")

    ' Ensure only one @
    If UBound(sNames) <> 1 Then
        Exit Function
    End If
  
    ' Make sure the @ isn't at one end of the string
    For Each vName In sNames
        If Len(CStr(vName)) <= 0 Then
            Exit Function
        End If

        ' Validate each character.  Can only allow alphanumeric,
        ' underscore, dash or dot
        For i = 1 To Len(CStr(vName))
        sChar = UCase$(Mid(CStr(vName), i, 1))
        
        If InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ_-.", sChar) <= 0 And _
                    Not IsNumeric(sChar) Then
            Exit Function
            End If
        Next
        
        ' Either end of string must not be dot
        If Left(CStr(vName), 1) = "." Or Right(CStr(vName), 1) = "." Then
            Exit Function
        End If
    Next

    ' Must have at least one dot after the @
    If InStr(sNames(1), ".") <= 0 Then
        Exit Function
    End If
  
    i = Len(sNames(1)) - InStrRev(sNames(1), ".")

    ' Last part of email address must be 2 or 3 chars long
    If i <> 2 And i <> 3 Then
        Exit Function
    End If
  
    ' Dot dot is not allowed
    If InStr(sIn, "..") > 0 Then
        Exit Function
    End If
    
    ' If we get here, email address is valid
    isValidEmailAddress = True

End Function
Page Updated 15/01/10