Full Version: Check Email Address Format
UtterAccess Forums > Microsoft® Access > Access Forms
FersonS
Either I'm failing at a proper forum search or this question is so dumb that I am the only one who is asking it. Either way...
How do I have a form check that what was entered in a text box was formatted as an email address?
whoever@whatever.something
missinglinq
Here's some code that I ran up for someone a couple of years ago.
CODE
Private Sub txtEmailAddress_BeforeUpdate(Cancel As Integer)

Dim InputErr As Integer
Dim DotEnding As String
DotEnding = Right(txtEmailAddress, 4)

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
And Sub

The code snip
CODE
If DotEnding <> ".com" And DotEnding <> ".org" And DotEnding <> ".edu" And DotEnding <> ".net" Then
  InputErr = InputErr + 1
End If

checks for .com, org, .edu and .net, but could be modified to include other domains, if necessary.
Linq ;0)>
Teatime
For a crude check you could do a comparison:
CODE
if format(strEmail) like ""?*@?*.?*" = false then ...

dit: Added the "?" to ensure at least one character is present in each of the three sections.
Thomas
cheekybuddha
Hi,
You can use this function to check an email is in a valid format - it uses Regular Expressions to match the pattern of characters:
CODE
Public Function fValidEmail(strTest As String) As Boolean
    If Len(strTest) > 0 Then
        With CreateObject("vbScript.RegExp")
            .Pattern = "^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$"
            fValidEmail = .Test(strTest)
        End With
    End If
End Function

hth,
d
FersonS
Wow. I have to admit... I'm surprised Access doesn't already have this worked out as a format or something.
Thank you all for your help. All of the suggestions are helpful. I don't know which one I'll use yet. I'll go play with them.
Thanks again!
cheekybuddha
Glad we could help - good luck with your project. " emoid=":thumbup:" border="0" alt="thumbup.gif" />
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.