Check for valid email address

CanWest

Registered User.
Local time
Today, 04:51
Joined
Sep 15, 2006
Messages
272
I have searched and found nothing on this.

I need a way to validate that an email address that is entered in to a field is in fact a valid email address format. I am not looking to check if the email address is a valid address just that it is formated correctly.

Anyone have any ideas.
 
google this. there is no built in solution to this in vb like there is in PHP or vb.net, etc...

you have no way to do it, IMO, other than by using a combination of built in resource functions.
 
This hack uses the BeforeUpdate event of a Textbox to check for formatting and common extensions (com. org, net,edu)

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

End Sub
If this doesn't fit your needs exactly it can easily be modified to do so.

Linq ;0)>
 
That is great. Thanks it worked perfectly. I was wondering if there is a way to handle things like .ca or .uk as they are only two characters.

I tried this but it did not work

Code:
Private Sub GeneralEmail_BeforeUpdate(Cancel As Integer)

 Dim InputErr As Integer
 Dim DotEnding As String
 DotEnding = Right(GeneralEmail, 4)
 Dim DotEnding2 As String
 DotEnding2 = Right(GeneralEmail, 3)
  
 If InStr(Me.GeneralEmail, " ") > 0 Then
  InputErr = InputErr + 1
 End If
  
If Len(Me.GeneralEmail) - Len(Replace(Me.GeneralEmail, "@", "")) <> 1 Then
  InputErr = InputErr + 1
 End If
 
If DotEnding <> ".com" And DotEnding <> ".org" And DotEnding <> ".edu" And DotEnding <> ".net" And DotEnding <> ".biz" And DotEnding <> ".gov" Then
  InputErr = InputErr + 1
End If

If DotEnding2 <> ".ca" And DotEnding2 <> ".us" And DotEnding2 <> ".uk" And DotEnding2 <> ".fr" Then
  InputErr = InputErr + 1
End If

If InputErr > 0 Then
 MsgBox "You Have Entered An Invalid Email Address!"
 Cancel = True
End If


End Sub

It seemed to have no effect at all
 
You need to check to see how long the extension of the Domain name is then apply the test that is appropriate for that type of extension.

I did this by subtracting the right-most position of the dot from the over-all length of the textbox. This works for me:
Code:
Private Sub GeneralEmail_BeforeUpdate(Cancel As Integer)

 Dim InputErr As Integer
 Dim DotEnding As String
 DotEnding = Right(GeneralEmail, 4)
 Dim DotEnding2 As String
 DotEnding2 = Right(GeneralEmail, 3)

 If InStr(Me.GeneralEmail, " ") > 0 Then
  InputErr = InputErr + 1
 End If

If Len(Me.GeneralEmail) - Len(Replace(Me.GeneralEmail, "@", "")) <> 1 Then
  InputErr = InputErr + 1
 End If

[B]If Len(Me.GeneralEmail) - InStrRev(Me.GeneralEmail, ".") = 3 Then[/B]
  If DotEnding <> ".com" And DotEnding <> ".org" And DotEnding <> ".edu" And DotEnding <> ".net" And DotEnding <> ".biz" And DotEnding <> ".gov" Then
    InputErr = InputErr + 1
  End If[B]
End If[/B]

[B]If Len(Me.GeneralEmail) - InStrRev(Me.GeneralEmail, ".") = 2 Then[/B]
  If DotEnding2 <> ".ca" And DotEnding2 <> ".us" And DotEnding2 <> ".uk" And DotEnding2 <> ".fr" Then
    InputErr = InputErr + 1
  End If
[B]End If[/B]
If InputErr > 0 Then
 MsgBox "You Have Entered An Invalid Email Address!"
 Cancel = True
End If


End Sub
Linq ;0)>
 
That is awesome. Thanks.

I noticed one flaw. If you enter an invalid email address and backspace over it after clicking ok on the error message and then try to move to another field you get a runtime error that says invalid use of null. However if you use the escape key everything is ok.
 
Thought I'd take a crack at this using the Split() function. Select Case is handy too if you have a list of things to check against...
Code:
Function CheckEmailAddressFormat(Address As String, ByRef Message As String) As Boolean
On Error GoTo handler
   Dim var1
   var1 = Split(Address, "@")
   If UBound(var1) <> 1 Then err.Raise -1, , "Address must contain one '@' character"
   
   Dim var2
   var2 = Split(var1(1), ".")
   If UBound(var2) < 1 Then err.Raise -1, , "Domain must contain at least one '.' character"
   
   Select Case var2(UBound(var2))
      Case "com", "net", "ca", "info"
[COLOR="Green"]         'these are OK, so ignore them
[/COLOR]      Case Else
[COLOR="Green"]         'everything else is deemed an error[/COLOR]
         err.Raise -1, , "Unrecognized domain extension"
   End Select
   
[COLOR="Green"]   'if execution makes it here then...[/COLOR]
   CheckEmailAddressFormat = True
   Message = "OK"
   Exit Function

handler:
   Message = err.Description
   
End Function
And here's the little test driver I was using...
Code:
Sub EMailFormatTest()
   Dim addresses
   Dim var
   Dim msg As String
   
   addresses = Split("me@nomail.com thisisatest.address@testing.uk.info alpha@bet.soup alpha@bet.com this.should.raise.error this@should@fail")
   For Each var In addresses
      CheckEmailAddressFormat CStr(var), msg
      Debug.Print var & ": " & msg
   Next
   
End Sub
Obviously I have far to much time on my hands right now ... :)
Mark
 
Please excuse my ignorance but I am new to this. I created a module using your code and the called the function from the before update event on the email field in the form. It returned an error message saying argument not optional. I am not sure what argument it is looking for. Any help would be greatly appreciated.

Thanks
 
Try the modification in red...
Code:
Function CheckEmailAddressFormat(Address As String, [B][COLOR="Red"]Optional[/COLOR][/B] ByRef Message As String) As Boolean
But don't you want to return that message? You can use it to tell your user what failed about the address they supplied.
 
I do I just do not know exactly what to put into the beforeupdate event. I know it is asking for an argument, I just do not know what it is.

My current code on the before update event is

Code:
Private Sub GeneralEmail_BeforeUpdate(Cancel As Integer)

CheckEmailAddressFormat

End Sub
 
Oh, OK. So it's a function and it returns a boolean, so we can use it in an expression. In addition it might return a message if we give it a variable in the Message parameter that it can put a result into, so try...

Code:
Private Sub Text2_BeforeUpdate(Cancel As Integer)
[COLOR="Green"]   'used to receive an error message from the validation routine[/COLOR]
   Dim msg As String
   
[COLOR="Green"]   'the function itself returns 'True' if the format is valid
   'so here, if it's Not valid we execute the If block[/COLOR]
   If Not CheckEmailAddressFormat(Me.Text2, msg) Then
[COLOR="Green"]      'and the function call returned the message, which we use here[/COLOR]
      MsgBox "Address Format Error: " & msg, vbCritical
[COLOR="Green"]      'and we cancel the update[/COLOR]
      Cancel = True
   End If
End Sub
Mark
 
Oh, OK. So it's a function and it returns a boolean, so we can use it in an expression. In addition it might return a message if we give it a variable in the Message parameter that it can put a result into, so try...

This worked absolutely perfectly. Thank you so much.....
 

Users who are viewing this thread

Back
Top Bottom