Yesterday I spent some time on googling looking for a decent LS function that validates an email internet address.
The evaluate method on
@ValidateInternetAddress( [ addressFormat ] ; Address )
validates only on characters before and after the @ sign, so it does not check the top level domain (.com, .org or .se) and was therefor of no use.
Well, what did Google bring me then? Nothing of interest unfortunately.
Tomas sent me the function below but when testing it it does not properly checked if a username is given (‘@acme.com’ would validate).
A simple combination of these two checks will do the job but this looks a bit poor. Maybe you have a better solution?
Function CheckSMTPAddressChar( smtpAddress As String) As Boolean
%REM – Check to see if any incorrect characters is used in the SMTP address
smtpAddress : the SMTP address to validate
return : will return true if valid email address
%END REM
Dim notAllowed As Variant
Dim noNotAllowed As Integer
Dim char As String
Dim charNum As Integer
Dim noAtChar As Integer
Dim i As Integer
CheckSMTPAddressChar = True
For i = 1 To Len( smtpAddress)
char = Mid( smtpAddress, i, 1)
If char = ” ” Then char = “<space>”
charNum = Asc( char)
REM No local characters is allowed only … normal once
Select Case charNum
Case 65 To 90 ‘ 65 = a, 90 = z
Case 97 To 122 ‘ 97 = A, 122 = Z
Case 48 To 58 ‘ 48 = 0, 58 = 9
Case 45 ‘ 45 = –
Case 46 ‘ 46 = .
Case 64 ‘ 64 = @
REM We can have 1 char 46 = @ but not 2
noAtChar = noAtChar + 1
If noAtChar > 1 Then
CheckSMTPAddressChar = False
End If
Case 95 ‘ 57 = _
Case Else
CheckSMTPAddressChar = False
End Select
Next
End Function
===//===
Dim smtpAddress As String
smtpAddress = “patrick.kwinten@acme.com”
If CheckSMTPAddressChar(smtpAddress) And smtpAddress Like “*@*.*” Then
‘do a double check
Dim emailChecker As Variant
Dim checkThis()
Redim checkThis(0)
checkThis(0) = smtpAddress
emailChecker = Evaluate(|@ValidateInternetAddress([Address821];”| + checkThis(0) + |”)|)
If emailChecker(0) = “” Then
Print “Valid Email address!”
Else
Print “Email address not valid!”
End If
Else
Print “Email address not valid!”
End If
End Sub
A little update: changing the Like into “*?@?*.?*” makes the second check redundant…
Validating email addresses is hard, there is a big difference in what is allowed by the RFC822 (RFC2822) and what mail servers excepts as a valid email address.
There is also a difference what a local MTA/MDA accepts and what a MTA in Internet will accept as a valid address.
Often addresses of the type “local-part@mail-domain@mail-domain@mail-domain” are accepted internally but never by a MTA in the Internet, at least if you want to prevent them from being used as a relay host.
But how to evaluate an email address then. The suggestion I made to Patrick is a simple check that will do in most cases, but it will not validate all different email addresses correctly. If you want to validate according to RFC822 I suggest you take a look at this link how to do it with RegEx [http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html].
You will then very quickly realizes that you want a simpler solution.
In reality MTA/MDA are more restrictive then RFC822 and you want them to be more restrictive to prevent spamming.
Practically a correct RFC822 validation will give you OK to email addresses which will then be rejected by the receiving MTA.
Finally I would like to make my like test a little better by change it to “*?@??*.??*”. This will assure there is at least 2 char in the domain-part before and after the dot.
Now I hope you guys can make this even better.
Sorry it should say “*?@*??.??*”
If you want to hear a reader’s feedback 🙂 , I rate this post for 4/5. Decent info, but I just have to go to that damn google to find the missed bits. Thank you, anyway!
Just want to drop you a quick note to thank you for the code. I know it’s been around here for a while, but it proved useful to me.
Thanks again.