JavaScript to validate email address using a regex

By Sam Deering

This is a code snippet for basic JavaScript to validate email address using a regular expression. This is building on my previous post on how to use regular expressions with jQuery. You can also load the code in jsfiddle below.

Update 12/05/13: Separated into versions for testing.

Version 1

var $email = $('form input[name="email'); //change form to id or containment selector
var re = /^(([^<>()[]\.,;:s@"]+(.[^<>()[]\.,;:s@"]+)*)|(
if ($email.val() == '' || !re.test($email.val()))
    alert('Please enter a valid email address.');
    return false;

Load code in jsfiddle

Version 2

var $email = $('form input[name="email'); //change form to id or containment selector
var re = /[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}/igm;
if ($email.val() == '' || !re.test($email.val()))
    alert('Please enter a valid email address.');
    return false;

Load code in jsfiddle


//reported to validate incorrectly: as true

//reported to validate incorrectly: as true

//current version

Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.

  • Teena

    @Sam : there is an error in this regex. The regex “/[A-Z0-9._%+-]+@[A-Z0-9-]+.+.[A-Z]{2,4}/igm” validates email address as a valid email id which is not true.

    If we use “/[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}/igm” instead of the regex used by you, it will validate the email as an invalid email address

    • jQuery4u

      Hi Teena, might be a stupid question but why is an invalid email address? Is a valid domain name for companies in India?

  • André Fontenelle

    There is another error. Thats isn’t a valid email – and passed true.

    • jQuery4u

      Hi Andre, I’ve updated the code try version 1 above.

  • Ritz

    Hi Sam, I am new in jquery can u please tell me why we using re.test in this statement if ($email.val() == ” || !re.test($email.val()))

    • jQuery4u

      Hi Ritz, the variable re stores our regular expression which matches valid email addresses. The JavaScript test() function can be applied to the regular expression passing the email address which is currently in the email field in our form. If the email is valid the test() function will return true so in the statement we are checking if the email value is present OR if the test returns false.

  • Doug

    Sam, thanks for the info. As a suggestion, it’s not too clear which of the three versions has the correct regex pattern. I’m assuming the last one from looking at the other comments.

  • Pingback: Javascript Email Regex | My Dev Note()

  • stephh

    Hi, I tried your version one, but the validation didn’t work. It also didn’t show the messages. Is there an issue with bootstrap template?

  • Carol Bauer

    Sorry for the ignorance, but I don’t know where to add this code. is it in the functions.php? Head? Inside the form code? it’s not very clear…

  • mikafabz

    Great… Thanks for that!

  • Hash

    Thank You So Much!!!

  • Sunil

    Thank you

  • nara

    how can i remove +” from the regular expression

  • Алексей Щепкин


    Verification IP is best done as follows:

    Sorry for the mistakes, I have bad English.

  • Никита Гаврилов

    You forgot to escape dot in your examples #2. They are invalid. And you don’t need `gm` flags, what you need is to wrap them with ^$

  • Florence Okosun

    thanks for this

Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.