Link to home
Start Free TrialLog in
Avatar of thready
thready

asked on

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,
Mike
ASKER CERTIFIED SOLUTION
Avatar of dsacker
dsacker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thready
thready

ASKER

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!
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:

"Abc\@def"@example.com
Crazy yes, and most mainstream domains (gmail, yahoo, etc) restrict that, but nonetheless, it is possible.
Avatar of thready

ASKER

Thank you very much for the extra great help!!

Cheers, and have a nice weekend!  :-)