Solved

Valid email addresses

Posted on 2016-11-10
6
53 Views
Last Modified: 2016-11-10
I have an MS Access database and need to find an accurate way of establishing if an email address is technically correct.  I have found several snippets of code online, with my favourite one is here:

Public Function ValidEmail(pAddress As String) As Boolean 
     '-----------------------------------------------------------------
    Dim oRegEx As Object 
    Set oRegEx = CreateObject("VBScript.RegExp") 
    With oRegEx 
        .Pattern = "^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$" 
        ValidEmail = .Test(pAddress) 
    End With 
    Set oRegEx = Nothing 
End Function 
 
Sub Test() 
    If ValidEmail("me@excel-it.com") Then 
        MsgBox "OK" 
    Else: MsgBox "Check email address" 
    End If 
End Sub 

Open in new window


However, this doesn't take into account anything with four characters in the suffix (.info etc.).  Also, if there are two email addresses within the string it will also return as false (j can get over this last issue easily enough).

Ideally, I just want to find the most up to date/reliable way to return a true/false value.  Or, figure out how to tweak this one to take that into account.

Any suggestions would be greatly appreciated.
0
Comment
Question by:Andy Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 51

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 41882012
Hi

to change to 4 for the suffix

pls try
Public Function ValidEmail(pAddress As String) As Boolean 
     '-----------------------------------------------------------------
    Dim oRegEx As Object 
    Set oRegEx = CreateObject("VBScript.RegExp") 
    With oRegEx 
        .Pattern = "^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,4}$" 
        ValidEmail = .Test(pAddress) 
    End With 
    Set oRegEx = Nothing 
End Function 
 
Sub Test() 
    If ValidEmail("me@excel-it.com") Then 
        MsgBox "OK" 
    Else: MsgBox "Check email address" 
    End If 
End Sub 

Open in new window

Regards
0
 

Author Closing Comment

by:Andy Brown
ID: 41882013
Fantastic - it does the trick perfectly.

Thank you.
0
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 41882019
As a commentary nowadays the number is no more limited to 4 but I think 255. you could have .university or .international
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Andy Brown
ID: 41882024
You are right.

I have also found this pattern that seems to be quite good:

.Pattern = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$"

Ignore case needs to be set to true, but it seems pretty reliable.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 41882192
You might also consider parsing the pAddress argument using the split command and then loop through the array to check individual elements of the array.  This would allow you to pass multiple values in a single string.  Or alternatively, use a parameter array as the functions argument, so that you can pass in multiple values, rather than concatenating multiple emails into a single string and then passing that value.
1
 

Author Comment

by:Andy Brown
ID: 41882257
Great suggestion - thank you.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question