We help IT Professionals succeed at work.

Regular expression email validation

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?

Your input is greatly appreciated.


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;

Open in new window

Comment
Watch Question

Terry WoodsIT 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;

Author

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._'-]...

Open in new window

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;