View Full Version : very basic email validation


ozinm
11-23-2005, 05:43 AM
someone could probably do a whole lot better but here goes:
Function ValidEmail(EMail As Variant) As Boolean
' Returns true if given a valid email address or Null
'setup
Dim TempCheck As Boolean
Dim TempCount, TempLoop As Integer
Dim TestData As Variant
TempCheck = True
TempCount = 0
TempLoop = 0
If Not IsNull(EMail) Then 'don't bother checking if email is empty
'check of invalid characters
For TempLoop = 1 To Len(EMail)
Select Case Asc(Mid(EMail, TempLoop, 1))
Case 0 To 45
TempCheck = False
Case 47
TempCheck = False
Case 58 To 63
TempCheck = False
Case 91 To 94
TempCheck = False
Case 96
TempCheck = False
Case Is > 122
Case Else
End Select
Next TempLoop

'check for 1 "@" character only
TempCount = 0
TempLoop = 0
Do
TempLoop = InStr(TempLoop + 1, EMail, "@")
If TempLoop > 0 Then
TempCount = TempCount + 1
End If
Loop Until TempLoop = 0
If TempCount > 1 Or TempCount < 1 Then TempCheck = False
End If
'return result
ValidEmail = TempCheck
End Function

ozinm
11-23-2005, 08:16 AM
Sorry,
just descovered that you can actually use regegex in Access.
See: http://www.access-programmers.co.uk/forums/showthread.php?t=97666

With it you should be able to evaluate an email with a regex something like this:

^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$

qqq
10-22-2006, 01:48 PM
Thanks for the hints. I found a fine tutorial on regex, how to create you own ones. Check out http://www.regular-expressions.info/