Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 a@b.com a valid email address?
- sReason = sEmail & " : 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 = 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement