Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. Public Function IsValidEmailAddress(ByVal strEmail As String, Optional ByRef sReason As String) As Boolean
  2.  
  3. On Error GoTo IsValidEmailAddress_Err
  4.  
  5. Dim strEmailAdr As Variant
  6. Dim sEmail As String
  7. Dim i As Integer
  8.  
  9. 'cas if the string ends with only ;
  10. If InStrRev(strEmail, ";") = Len(strEmail) Then strEmail = Left(strEmail, Len(strEmail) - 1)
  11. strEmailAdr = Strings.Split(strEmail, ";")
  12. For i = LBound(strEmailAdr) To UBound(strEmailAdr)
  13. sEmail = LCase(Trim(CStr(strEmailAdr(i))))
  14. 'sEmail = LCase(Trim(sEmail))
  15. IsValidEmailAddress = False
  16. If Len(sEmail) < 7 Then '-- Is a@b.com a valid email address?
  17. sReason = sEmail & " : Too short!"
  18. ElseIf sEmail Like "*[!0-9a-z@._+-]*" Then
  19. '-- not sure about these characters: ! $ & ` ' * / \ = ? ^ | # &#37; { } ~
  20. ' if required, add in to the above string after letter z and before the last hyphen -
  21. sReason = sEmail & " : Invalid character in email!"
  22. ElseIf Not sEmail Like "*@*.*" Then
  23. sReason = sEmail & " : Missing the @ or .!"
  24. ElseIf sEmail Like "*@*@*" Then
  25. sReason = sEmail & " : Too many @!"
  26. ElseIf sEmail Like "[@.]*" Or sEmail Like "*[@.]" _
  27. Or sEmail Like "*..*" Or Not sEmail Like "?*@?*.*?" Then
  28. sReason = sEmail & " : Invalid format!"
  29. Else
  30. Dim n As Integer
  31. n = Len(sEmail) - InStrRev(sEmail, ".")
  32. If n > 3 Then
  33. sReason = sEmail & " : Suffix too long!"
  34. ElseIf n < 2 Then
  35. sReason = sEmail & " : Suffix too short!"
  36. Else
  37. sReason = Empty
  38. IsValidEmailAddress = True
  39. End If
  40. End If
  41. Next i
  42.  
  43. Exit Function
  44. IsValidEmailAddress_Err:
  45. MsgBox "PublicFonctions.IsValidEmailAddress : " & vbCrLf & _
  46. "Error " & Err.Number & " " & Err.Description
  47. On Error GoTo 0
  48. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement