Validating e-mail addresses

  • Thread starter Thread starter Rob Hill
  • Start date Start date
Rob Hill said:
Has anyone out there got a bit of foolproof code
forvalidating e-mail addreses?

There's no way to completely validate an e-mail address short of sending
a message and seeing if it's delivered, but here's a little routine that
checks to see if a candidate string at least *looks* like an e-mail
address. I don't promise it's perfect.

---- start of code ----
Function IsValidEmailAddress(Candidate As String) As Boolean

If Trim(Candidate) Like "?*@[!.]*.[!.]*" Then
If Not Candidate Like "*@*@*" Then
IsValidEmailAddress = True
End If
End If

End Function

---- end of code ----
 
Dirk Goldgar said:
There's no way to completely validate an e-mail address short of sending
a message and seeing if it's delivered, (snip)


I don't think there's any way to see if an email was delivered.

SMTP has a "receipt verification" thingy (I can't quite remember what it is
called), but I believe that it is optional, so some servers don't support
it.

TC
 
Had to work it out for myself - here it is if anyone wants
it.

No leading or trailing dot.
There must be a name part, then an @, then a domain part
Name can only contain a-z, 0-9, hyphen, underscore or dot -
dot is not mandatory
Domain can only contain a-z, 0-9, hypen or dot and the dot
is mandatory

Here's the after update code for the field EMail :-

Dim tCount, tWheresAt, tNameError, tDomainError,
tDomainDot As Long
Dim tName, tDomain As String

tWheresAt = InStr(Me.EMail, "@")
tName = Left(Me.EMail, tWheresAt - 1)
tDomain = Right(Me.EMail, Len(Me.EMail) - tWheresAt)

If tWheresAt = 0 Then
MsgBox "No @ sign in e-mail address - please re-
enter", vbOKOnly, "EMail validation"
Me.EMail = Null
DoCmd.GoToControl "EMail"
Exit Sub
End If

If tName = "" Or Left(tName, 1) = "." Or Right(tName,
1) = "." Then
tNameError = 1
End If

tCount = 0
For tCount = 1 To Len(tName)
If (Mid(tName, tCount, 1) Like "[a-z]" Or Mid
(tName, tCount, 1) Like "[0-9]" Or Mid(tName, tCount, 1)
= "-" Or Mid(tName, tCount, 1) = "_" Or Mid(tName, tCount,
1) = ".") And Asc(Mid(tName, tCount, 1)) <> 42 Then
' okay
Else
tNameError = 1
End If
Next
If tNameError = 1 Then
MsgBox "Name part of e-mail address contains
invalid characters - please re-enter", vbOKOnly, "EMail
validation"
Me.EMail = Null
DoCmd.GoToControl "EMail"
Exit Sub
End If

tDomainError = 0
tDomainDot = 0

If tDomain = "" Or Left(tDomain, 1) = "." Or Right
(tDomain, 1) = "." Then
tDomainError = 1
End If

tCount = 0
For tCount = 1 To Len(tDomain)
If (Mid(tDomain, tCount, 1) Like "[a-z]" Or Mid
(tDomain, tCount, 1) Like "[0-9]" Or Mid(tDomain, tCount,
1) = "-" Or Mid(tDomain, tCount, 1) = ".") And Asc(Mid
(tDomain, tCount, 1)) <> 42 Then
' okay
Else
tDomainError = 1
End If
If Mid(tDomain, tCount, 1) = "." Then
tDomainDot = 1#
End If
Next

If tDomainError = 1 Then
MsgBox "Domain name part of e-mail address
contains invalid characters - please re-enter",
vbOKOnly, "EMail validation"
Me.EMail = Null
DoCmd.GoToControl "EMail"
Exit Sub
End If

If tDomainDot = 0 Then
MsgBox "Domain name part of e-mail address does
not contain a dot - please re-enter", vbOKOnly, "EMail
validation"
Me.EMail = Null
DoCmd.GoToControl "EMail"
Exit Sub
End If

End Sub
 
Back
Top