We help IT Professionals succeed at work.

# Regular expression email validation

on
Hi,

I have the below javascript code that validates email address, unfortunately this regular expression does not accept the email address o'neil@yahoo.com. How do I revise the code below so that it will accept email address with apostrophe?

``````var objRegEx = /(^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})\$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*\$)/i;
``````
Comment
Watch Question

## View Solutions Only

IT Guru
Most Valuable Expert 2011

Commented:
Try this:

var objRegEx = /(^[a-zA-Z0-9._-']+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\$)|(^[a-z]([a-z_\.']*)@([a-z_\.]*)([.][a-z]{3})\$)|(^[a-z]([a-z_\.']*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*\$)/i;

Commented:
I receive syntax error in regular expression.
Most Valuable Expert 2011
Top Expert 2015
Commented:
Points to TerryAtOpus

Move the hyphen to the end of the bracket expression in the first bracket expression.

``````var objRegEx = /(^[a-zA-Z0-9._'-]...
``````
IT Guru
Most Valuable Expert 2011
Commented:
Thanks kaufmed, I think that definitely deserves some points. Corrected pattern:
var objRegEx = /(^[a-zA-Z0-9._'-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\$)|(^[a-z]([a-z_\.']*)@([a-z_\.]*)([.][a-z]{3})\$)|(^[a-z]([a-z_\.']*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*\$)/i;