-2

I have a regex that is supposed to match email addresses.

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$

When I run the below code in my javascript, it returns null. Could it be an issue with my JS syntax, or is it an issue with the regex?

alert(emailString.match(regex));
Charmander
  • 196
  • 1
  • 14
  • 1
    What value does `emailString` contain? – George Cummins Aug 12 '11 at 15:00
  • 1
    As usual, the regex is wrong, although it's better than some I've seen. – SLaks Aug 12 '11 at 15:00
  • @George Cummins, I'm passing it a standard email address, and one that I know works. – Charmander Aug 12 '11 at 15:02
  • Most people enter email addresses in lower case letters (or use mixed case), your regex only seems to accept upper case letters. Also it will not match some tlds, like .museum and .travel. – Arjan Aug 12 '11 at 15:02
  • @SLacks, if you know how to fix it, write me an answer. – Charmander Aug 12 '11 at 15:02
  • @Arjan, I have read the article on email regular expressions [here](http://www.regular-expressions.info/email.html) and have decided that I will ignore .museum and .travel, seeing as most people don't use them. – Charmander Aug 12 '11 at 15:04
  • 3
    @Charmander: [he already has](http://stackoverflow.com/questions/1903356/email-validation-regular-expression/1903368#1903368) – Crescent Fresh Aug 12 '11 at 15:05

7 Answers7

4

This regular expression does not include lowercase letters. Try this:

^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$
defuz
  • 26,721
  • 10
  • 38
  • 60
  • 4
    @Charmander: if this worked for you, you were testing the [other answers wrong](http://stackoverflow.com/questions/7041998/email-regex-in-js/7042036#7042036), as they fix the case-sensitivity directly. – Crescent Fresh Aug 12 '11 at 15:11
2

Most probably you've forgotten to set the case-insensitive option.

var regex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;

And of course, you're missing lots of valid addresses (.museum etc.)...

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
1

The problem with using regex to validate emails is even the expression that is the "standard" misses completely valid addresses. You would be far better off checking to see if it contains the @ symbol and a . . Or to be really fancy you can poll the email address and if no response is given mark it as invalid, this of course comes with an overhead.

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
  • Why the downvote with no comment? This answer is correct in that writing a regex to match *all* valid email addresses is really hard, way harder than any trivial one-liner. – Flexo Aug 12 '11 at 15:08
  • @awoodland what are you doing your research in? – Woot4Moo Aug 12 '11 at 15:15
  • links between graphics and vision problems. I'm on chat at the moment if you want to talk more because it's a bit OT on this question! – Flexo Aug 12 '11 at 15:18
1

Posting a bit late but this regex works 100% across all email formats.

let rEmail = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;

console.log(rEmail.test("john@gmail.com")); //true
console.log(rEmail.test("john@gmail.com123")); //false
console.log(rEmail.test("john.something@gmail.com")); //true
console.log(rEmail.test("john123@gmail.com")); //true
Muhammad Atif Akram
  • 1,204
  • 1
  • 4
  • 12
0

You can use this one. it will support after [dot] 2 ,3 character as per your domain

var email_filter  = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
if (email_filter.test('yourEmail@gmail.com')) {
  alert('Valid Email');
}
Swadesh Ranjan Dash
  • 544
  • 1
  • 4
  • 17
0

I assume you have not specified the case-insensitive modifier:

var regex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
//                                                    ^

Otherwise the expression only matches upper case letters.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 3
    @Charmander: What's the adress you are testing it with? `"foo@bar.com".match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)` works fine. – Felix Kling Aug 12 '11 at 15:08
0

You might want to see RFC 5322, in particular Section 3.4.1

Flexo
  • 87,323
  • 22
  • 191
  • 272