Verify proper email address entry in text field

J

John Barnes

How can I check for proper format (name@domain) of a
user's email address in a text field on a form?

Thanks!
 
S

Steve Schapel

John,

Here is a function I use. I don't suppose it is perfect, but I think it
will catch most invalid syntax entries ...

**********
Public Function EmailCheck(EmAdd As String) As Boolean
Dim Tester As Boolean
If Len(EmAdd) = 0 Then
Tester = False
Else
Tester = True
Tester = Tester And InStr(EmAdd, "@") > 0
Tester = Tester And InStr(EmAdd, ",") = 0
Tester = Tester And InStr(EmAdd, ";") = 0
Tester = Tester And InStr(EmAdd, " ") = 0
Tester = Tester And InStr(Mid(EmAdd, InStr(EmAdd, "@") + 1),
"@") = 0
Tester = Tester And InStr(Mid(EmAdd, InStr(EmAdd, "@") + 1),
".") > 0
Tester = Tester And Right(EmAdd, 1) <> "."
End If
EmailCheck = Tester
End Function
***********

Put this code in a standard module, and then on your form, on the After
Update event of your email address control, put something like this...
If Not IsNull(Me.YourEmailAddress) Then
If EmailCheck(Me.YourEmailAddress) = False Then
MsgBox "Email address not valid"
End If
End If
 
A

Allen Browne

Another option is to open the table in design view, and add a Validation
Rule on the field (lower pane in table design).

Something like this:
Is Null Or ((Like "*?@?*.?*") And (Not Like "*[ ,;]*"))

List any characters that are not allowed (including the space) inside the
square brackets.
 
Top