Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Email Validation

Status
Not open for further replies.

rozzay

Programmer
Jan 3, 2002
142
0
0
US
hi I am working on this email validation and was wondering if you guys could help. I am trying to validate the user does not enter more than one '@' or '.' . Assuming of course that most emails contain only one ampersand and period. below i have included my code but it does not work correctly. Please help!!

thanks in advance,

===========================================================

Private Function CheckEmail()
Dim strMsg As String
Dim iX As Long

iX = InStr(1, strEmail, "@", vbBinaryCompare)
If iX Then
If InStr(iX, strEmail, "@", vbBinaryCompare) Then
strMsg = "Only 1 '@' for Email Address."
If InStr(iX, strEmail, ".", vbBinaryCompare)Then
If InStr(iX, strEmail, ".", vbBinaryCompare) Then
strMsg = "Only 1 period for Email Address."
Else
bValidEmail = True
End If
End If
end if
End If

If bValidEmail = False Then
MsgBox strMsg, vbOKOnly
End If

End Function
 
Watch where you end your if statements. Try the modified code below...


Private Function CheckEmail()
Dim strMsg As String
Dim iX As Long


iX = InStr(1, strEmail, "@", vbBinaryCompare)
If iX Then
If InStr(iX, strEmail, "@", vbBinaryCompare) Then
strMsg = "Only 1 '@' for Email Address."
else
If InStr(iX, strEmail, ".", vbBinaryCompare)Then
If InStr(iX, strEmail, ".", vbBinaryCompare) Then
strMsg = "Only 1 period for Email Address."
Else
bValidEmail = True
End If

endif
end if


If bValidEmail = False Then
MsgBox strMsg, vbOKOnly
End If

End Function Troy Williams B.Eng.
fenris@hotmail.com

 
I did it like this:

bValidEmail = True
strEmail = Text1.Text

iX = InStr(1, strEmail, "@", vbBinaryCompare)
If iX Then
If InStr(iX + 1, strEmail, "@", vbBinaryCompare) Then
strMsg = "Only 1 '@' for Email Address."
bValidEmail = False
End If
Else
strMsg = "Must have '@' for Email Address."
bValidEmail = False
End If

If bValidEmail Then
iX = InStr(1, strEmail, ".", vbBinaryCompare)
If iX Then
If InStr(iX + 1, strEmail, ".", vbBinaryCompare) Then
strMsg = "Only 1 period for Email Address."
bValidEmail = False
End If
Else
strMsg = "Must have period for Email Address."
bValidEmail = False
End If
End If

If bValidEmail = False Then
MsgBox strMsg, vbOKOnly
End If


I would be careful about assuming that all emails must have only one period; mine has two.
 
Public Function ValidEmail(strAddress As String) As Boolean
Dim re As Object

Set re = CreateObject("vbscript.regexp")
re.IgnoreCase = True
re.Pattern = "^\w+@\w+.\w+$"
ValidEmail = re.Test(strAddress)
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top