Visual Basic Programming Code Examples
Visual Basic > API and Miscellaneous Code Examples
Validating An Email Address
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Validating An Email Address
Public Function Validate_EmailAddress(ByVal EAddress As String) As Boolean
'Dimension Variables.
Const AllowChars = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" + _
"abcdefghijklmnopqrstuvwxyz._-"
Dim UserName As String
Dim ServerName As String
Dim x As Long
'Validate email address.
x = InStr(1, EAddress, "@")
If x = 0 Then GoTo BadAddress
If InStr(x + 1, EAddress, "@") > 0 Then GoTo BadAddress
UserName = Left$(EAddress, x - 1)
ServerName = Right$(EAddress, Len(EAddress) - x)
If Left$(UserName, 1) = "." Or Right$(UserName, 1) = "." Then GoTo BadAddress
If Left$(ServerName, 1) = "." Or Right$(ServerName, 1) = "." Or _
InStr(1, ServerName, ".") = 0 Then GoTo BadAddress
For i = 1 To Len(UserName)
If InStr(1, AllowChars, Mid$(UserName, i, 1)) = 0 Then GoTo BadAddress
Next
For i = 1 To Len(ServerName)
If InStr(1, AllowChars, Mid$(ServerName, i, 1)) = 0 Then GoTo BadAddress
Next
Validate_EmailAddress = True
Exit Function
BadAddress:
Validate_EmailAddress = False
End Function