• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 716
  • 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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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