Apart from not having a space, valid addresses also have to have a single ampersand (
@) and a valid generic top-level domain on the end.
The first two items are easy to check, but the sticking point comes when trying to verify the last point, the valid host domain types, i.e. com, edu, org, etc. because the possibilities are almost endless, especially if you're dealing with government workers' addresses.
Here's a routine that checks for spaces, checks for a single ampersand and checks the most common generic top-level domains, such as .com, .org, .edu and .net., and throws up a messagebox if the address doesn't fall within these parameters.
- Private Sub txtEmailAddress_BeforeUpdate(Cancel As Integer)
-
-
Dim DotEnding As String
-
Dim InputErr As Integer
-
-
DotEnding = Right(txtEmailAddress, 4)
-
InputErr = 0
-
-
If InStr(Me.txtEmailAddress, " ") > 0 Then
-
InputErr = InputErr + 1
-
End If
-
-
If Len(Me.txtEmailAddress) - Len(Replace(Me.txtEmailAddress, "@", "")) <> 1 Then
-
InputErr = InputErr + 1
-
End If
-
-
If DotEnding <> ".com" And DotEnding <> ".org" And DotEnding <> ".edu" And DotEnding <> ".net" Then
-
InputErr = InputErr + 1
-
End If
-
-
If InputErr > 0 Then
-
MsgBox "You Have Entered An Invalid Email Address!"
-
Cancel = True
-
End If
-
-
End Sub
You'll have to decide what additional top-level domains, if any, that you want to accept, and modify the code according, following the example in Lines 17-19 above.
Here's an article on Wikipedia listing some of the more common ones:
http://en.wikipedia.org/wiki/Generic_top-level_domain
This code will also notify the user if an invalid address is copied then pasted into the field.
Linq
;0)>