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




Reply With Quote