Validating an Email Address

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have created a form for a user to email data to a vendor. The vendor email addresses are not in a table, therefore the user must type the email address each time [this will be addressed in a new version of the application]. I have created a button on the form to email the request. I currently do some rudimentary checking - like for the @ sign and to make sure there isn't a comma in the address, but I would like to check to make sure that the email address entered is a valid one. Because the emails are being sent to outside vendors, the email address can not be verified using our company's Outlook address book.

Here is the code for what the button currently does. I use the SendObject method to send the email with the attached file

Dim sRptName As Strin
Dim sAddy As Strin
Dim sEMSG As Strin
Dim sMsg As Strin
Dim sSubject As Strin
Dim lngPos As Lon

If IsNull(Me.txtEmailAddy) The
MsgBox "You must enter an email address before clicking on the Send Button", vbInformatio
Me.txtEmailAddy.SetFocu
GoTo Exit_cmdSend_Clic
End I
lngPos = InStr(1, Me.txtEmailAddy, "@"
If lngPos > 0 The
MsgBox "The email address does not contain the @ sign, please re-enter", vbInformatio
Me.txtEmailAddy.SetFocu
GoTo Exit_cmdSend_Clic
ElseIf InStr(1, Me.txtEmailAddy, ",") > 0 The
MsgBox "The email address contains a comma, please re-enter", vbInformatio
Me.txtEmailAddy.SetFocu
GoTo Exit_cmdSend_Clic
Els
sAddy = Me.txtEmailAdd
End I

Any help would be greatly appreciated. Thank you ..

Nancy Welc
Programmer/Analyst
King County IT
 
Hi Nancy

Ultimately the only check that an email address *is valid* is whether or not
it is accepted by the mail server at the destination domain.

However, there are several things you can test for to check that it is *not
invalid*. These include:
1. exactly one @ sign
2. at least one dot after the @
3. no invalid characters

Below is a function that I use to test these criteria.

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

=========== start code ==============
Public Function IsValidEmail(em) As Boolean
Dim i As Integer
i = InStr(em & "", "@")
If i Then
IsValidEmail = IsValidEmail2(Left(em, i - 1), False) _
And IsValidEmail2(Mid(em, i + 1), True)
End If
End Function

Private Function IsValidEmail2(ByRef sTest As String, ByVal fDomain As
Boolean) As Boolean
Dim iStart As Integer, iNext As Integer, s As String, sBadChars As String
If fDomain Then
sBadChars = "*[!a-z0-9_$&-]*"
Else
sBadChars = "*[!a-z0-9_$&'/-]*"
End If
iStart = 1
Do
iNext = InStr(iStart, sTest, ".")
If iNext Then
s = Mid(sTest, iStart, iNext - iStart)
iStart = iNext + 1
Else
If iStart = 1 And fDomain Then Exit Function
s = Mid(sTest, iStart)
End If
If Len(s) = 0 Then Exit Function
If s Like sBadChars Then Exit Function
Loop While iNext
IsValidEmail2 = True
End Function
================ end code =================
Nancy said:
I have created a form for a user to email data to a vendor. The vendor
email addresses are not in a table, therefore the user must type the email
address each time [this will be addressed in a new version of the
application]. I have created a button on the form to email the request. I
currently do some rudimentary checking - like for the @ sign and to make
sure there isn't a comma in the address, but I would like to check to make
sure that the email address entered is a valid one. Because the emails are
being sent to outside vendors, the email address can not be verified using
our company's Outlook address book.
Here is the code for what the button currently does. I use the SendObject
method to send the email with the attached file.
 
Back
Top