javascript email validation function with regex

Hi Experts,

There are a zillion regex email validation functions that exist for javascript.  Unfortunately, none of them that I've seen are truly professional - and don't validate according to the RFC.

Can someone point me to one that truly works?  I won't tell you what my test is just yet, but it may make for an interesting discussion..  :-)

Thank you,
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsackerContract ERP Admin/ConsultantCommented:
This will cover most of the variations allowable in RFC:
^(?!\.)("([^"\r\\]|\\["\r\\])*"|([-a-z0-9!#$%&'*+/=?^_`{|}~] |(?@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$

Open in new window

The problem is, email validation works best when coupled with server-side validation, including pinging the domain. What good is it to use a robust regex and send your email to  :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
threadyAuthor Commented:
Thank you dsacker.  I'm a little reluctant to go to this regex just because we're releasing shortly and the expression we have is this:

var EMAIL_PATTERN =      "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                    + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

which does appear less accurate than yours.  But we need apostrophes to be allowed.  And I'm thinking I can just add them like this:

var EMAIL_PATTERN =      "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-']+)*@"
                    + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

However, I'm just trying to understand the expression we had.  Could you tell me why we have the double backslashes before the @ sign?  If you could just explain the ones coming before the '.' that occurs before the @ in the email, I'd really appreciate it!
dsackerContract ERP Admin/ConsultantCommented:
The double backslashes allow for the possibility that one may actually choose to have an @ sign in the local portion of his email. According to RFC you can have a legitimate email address as follows:

Crazy yes, and most mainstream domains (gmail, yahoo, etc) restrict that, but nonetheless, it is possible.
threadyAuthor Commented:
Thank you very much for the extra great help!!

Cheers, and have a nice weekend!  :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

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.