In order to validate the email address, you need to define the rules that are appropriate. As a starting point, you could try something like
Private Function eMailOk(Address As String) As Boolean
If InStr(Address, "@" = 0 Then
eMailOk = False
Exit Function
End If
If InStr(Address, "." = 0 Then
eMailOk = False
Exit Function
End If
If InStr(Address, " " <> 0 Then
eMailOk = False
Exit Function
End If
If InStr("@.", Left(Trim(Address), 1)) <> 0 Then
eMailOk = False
Exit Function
End If
If InStr("@.", Right(Trim(Address), 1)) <> 0 Then
eMailOk = False
Exit Function
End If
eMailOk = True
End Function
This tests for the existence of "@" and "." in the address, and that the address cannot start or end with "@" or "." (eg cant have a@b. or .@ab or a.b@), and cant include a space.
Note that as a part of this, you will need to define the rules that you require for validating an email address. What characters are invalid or valid? How much to you want to test the form of the address? - the above will catch .@com or .junk@com, but it wont catch a.@com or a@.com for instance.
There may be a WinAPI call that can do this - I had a quick look but couldnt see anything - perhaps someone else knows more??
'first make a reference to Microsoft VBScript Regular Expressions Library
'Then try this code.
Option Explicit
Private MyRegExp As RegExp
Private Sub Command1_Click()
MsgBox IsValidEmailAddress("MyEmail@MyDomain.com"
End Sub
Public Function IsValidEmailAddress(ByVal strExp As String) As Boolean
MyRegExp.Pattern = "^[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]$"
IsValidEmailAddress = MyRegExp.Test(strExp)
End Function
Private Sub Form_Load()
Set MyRegExp = New RegExp
End Sub
A note, though, that there are a lot of technically valid email address characters, including (before the @)
Code:
!#$%&'*+\-/=?^_`{|}~
.
I just use a very simple pattern that avoids the complexities of RFC 2822 and RFC 2396 (where the entire valid patterns are described). My simple pattern is:
Code:
^[^@ ]+@[^@ ]+\.[^@ ]+$
It bascially just looks for some stuff that's not an @ sign, followed by an @ sign, followed by some more stoff that's not an @ sign up to a final dot, followed by some more stuff that's not an @ sign. Simple, true, and invalid addresses can get through, but they'll at least match the most basic email pattern.
Yeah, I've been able to pick up easier patterns on my own. Normally I wouldn't even mind the email one but I'm under a time crunch on it... strongm, you have definitely convinced me to use them... they are slick!
Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.