• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 710
  • Last Modified:

Email Address Regular Expression

Hi,

I have a simple regex that I use in my MS Access database to validate email addresses.  Here is the VBA:

strEmailRegex = "^[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]$"

However, any email address with a 1 character domain name fails the regex.  Ie, "john@q.com" fails.  Can someone adjust the regex above to allow one character domains.

Also, I'm not sure the current rules on domain names.  It seems like one character domains are allowed (q.com and z.com) though they are not very common (most of the ones I tried failed).  Trying to register a.com on GoDaddy just says "a.com is an invalid domain name."  What domains are and are not allowed?

Thanks!
strEmailRegex = "^[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]$"

Open in new window

0
Chris
Asked:
Chris
  • 2
  • 2
  • 2
1 Solution
 
Gary DavisDir Internet SvcsCommented:
This works:
^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z\.]*[a-zA-Z]$
Gary Davis
0
 
ozoCommented:
that fails to match
fred&barney@stonehenge.com
0
 
ChrisIT ManagerAuthor Commented:
That doesn't seem to do the trick for me.  It still fails if the domain name is only one character.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ozoCommented:
@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]
allows one character domain names, but it may also allow other things that you may not want.
What exactly do you want to exclude?
0
 
Gary DavisDir Internet SvcsCommented:
The expression I gave above will fail to match fred&barney@stonehenge.com due to the & character.
 To include the ampersand as an allowed character before the @ (it's not legit), use
 ^[a-zA-Z][\w\.&-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z\.]*[a-zA-Z]$
Gary
0
 
ChrisIT ManagerAuthor Commented:
This regex worked:

    strEmailRegex = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now