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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validate email format 4

Status
Not open for further replies.

jass1220

Programmer
May 20, 2003
88
AE
how i can validate that the email address in this format
(whatever@wherever.any)
 
Hi jass1220

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, &quot; &quot;) <> 0 Then
eMailOk = False
Exit Function
End If
If InStr(&quot;@.&quot;, Left(Trim(Address), 1)) <> 0 Then
eMailOk = False
Exit Function
End If
If InStr(&quot;@.&quot;, Right(Trim(Address), 1)) <> 0 Then
eMailOk = False
Exit Function
End If
eMailOk = True
End Function

This tests for the existence of &quot;@&quot; and &quot;.&quot; in the address, and that the address cannot start or end with &quot;@&quot; or &quot;.&quot; (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??

Hope this helps

David
 
'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(&quot;MyEmail@MyDomain.com&quot;)
End Sub
Public Function IsValidEmailAddress(ByVal strExp As String) As Boolean
MyRegExp.Pattern = &quot;^[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]$&quot;
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.
 
DrJavaJoe and Genimuse tried to use your way but it seems that i didn't get it well !! a bit comlicated for me[ponder]
 
Dim MyRegExp As RegExp
Set MyRegExp = New RegExp

MyRegExp.Pattern = &quot;^[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]$&quot;

If MyRegExp.Test(Me.Text1) = False Then
MsgBox (&quot;invalid email adsress&quot;)
Else
'allow to save
End If

thanks :-D
 

PS42

i'll use ur way in validating the time to be in this format (9:00)

thanks programers
 
Another Start For DrJ. I would have taken me days to figure out this 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!
 
[bigsmile]

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'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top