6

I am validating an email address using the following regex

var regex=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

Now the problem is that it is showing unexpected behavior

If I enter email address like

[email protected]

This is accepted by the above regex as validate format of email address But when I use format

[email protected]

The regex don't validate it as email format

similarly

[email protected]
is a not a validate format while
[email protected]
is a validate email format I especially want the validity of email address of the format

[email protected]

What kind of mistake I am doing in above regex?

6
  • 3
    Can the problem be outside of the regex? Testing with this tool: spaweditor.com/scripts/regex/index.php there seems to be no problem.. Commented Jun 7, 2011 at 7:01
  • 1
    There are (public) top level domains with more than 4 letters in them, which that regex will reject. Commented Jun 7, 2011 at 7:02
  • 1
    Email addresses which include a + somewhere before the @ are quite common, and that regex will reject them. Commented Jun 7, 2011 at 7:03
  • @Quentin hi thanks for response. But in my format it is not having + sign? Iam only having . sign. Then why it not validating? Commented Jun 7, 2011 at 7:04
  • 1
    If I was answering the question, I would have given an answer. I'm just pointing out that the regex is horribly flawed in ways you aren't testing for. Commented Jun 7, 2011 at 9:03

5 Answers 5

6

An email address as defined by the RFC 822 (superseded by RFC 2822) is a very complex regex. In the original RFC it is defined as a recursive descent. This makes it very hard to express it in a regex. See this "article": http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html

In perl-style regex:

(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]
)+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:
\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(
?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ 
\t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\0
31]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\
](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+
(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:
(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z
|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)
?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\
r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[
 \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)
?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t]
)*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[
 \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*
)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]
)+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)
*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+
|\Z|(?=[\["()<>@,;:\\".\[\]]))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r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\
".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?
:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[
\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\".\[\] \000-
\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(
?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;
:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([
^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"
.\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\
]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\
[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\
r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] 
\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]
|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\".\[\] \0
00-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"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()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:
[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\
]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)
?[ \t])*(?:@(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["
()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)
?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>
@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[
 \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,
;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t]
)*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\
".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?
(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".
\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:
\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[
"()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])
*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])
+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\
.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z
|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(
?:\r\n)?[ \t])*))*)?;\s*)
Sign up to request clarification or add additional context in comments.

2 Comments

Another article on e-mail addresses and regular expressions: regular-expressions.info/email.html
I hate that article. "my claim only holds true when one accepts my definition of what a valid email address really is". Yes, well, I live in the real world and would rather not kick my customers because they have a real, working email address that doesn't meet some guy's non-standard definition.
3

Firstly, don't escape characters in a character class ie [.] means a literal dot. Don't code as [\.]

Secondly, the dash character must be either the first or last character in a character class, otherwise it's the range operator.

Try this: Edited to include @BenRoux's \w suggestion

var regex=/^([\w.-])+\@([\w.-])+\.([A-Za-z]{2,4})$/

p.s. I tested it and this regex matches "[email protected]"

4 Comments

+1 for your comment regarding a hyphen inside of brackets... never knew about that. I always escaped it (though I hated to do so)
its allowed to escape characters in a character class (but for most characters not needed), so [\.] will match a dot. If you want to match a backslash you have to escape it [\\], otherwise things like [\w] would match a backslash and a w and not a word character.
I have used your both formats. It is still not validating [email protected]. But is is validating [email protected]. Then what is the problem?
Sorry, I validated it using java and they both pass for me.
3

If you really want to validate using javascript + regex, try this ridiculously long one. I've been using it for quite some time and no problem has come up so far.

var regex = /^(?:"(?:[^\\"]|\\.)+"|[A-Za-z0-9!#$%&'\*\+\-\/=\?^_`{|}~]+(?:\.[A-Za-z0-9!#$%&'\*\+\-\/=\?^_`{|}~]+)*)@(?:[A-Za-z](?:[A-Za-z0-9\-]{0,61}[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25[0-5]|2[0-4]\d|[0-1]?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})|(?:(?:[0-9A-Fa-f]{1,4}\:){1,2}(?:\:[0-9A-Fa-f]{1,4}){1,3}\:(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})|(?:(?:[0-9A-Fa-f]{1,4}\:){1,3}(?:\:[0-9A-Fa-f]{1,4}){1,2}\:(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})|(?:(?:[0-9A-Fa-f]{1,4}\:){1,4}(?:\:[0-9A-Fa-f]{1,4}){1,1}\:(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})|(?:(?:(?:[0-9A-Fa-f]{1,4}\:){1,5}|\:)\:(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})|(?:\:(?:\:[0-9A-Fa-f]{1,4}){1,5}\:(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}))(?:\%\d+)?)\])$/;

p.s. it's RFC822 and RFC5322 conformant, except for the length validations which cannot be done in a single pure regex.

4 Comments

There is some double quote missing in your regex
@Awais Qarni Really? Could you specify?
Dude I copy you regex and pasted in my javascript file all the code below it turns to blue as we often experience missing some quote. When I only put " at the end it turns normal. Thant's why I said
@Awais Qarni, don't trust your editor. LOL I'm sure it works. ;) I just double checked.
2

For starters, \w is equivalent to [a-zA-Z0-9_], so changing that will make your regex decidedly simpler.

/^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,4}$/

I have tried this on all of your posted emails, and it works fine

1 Comment

Hi thanks for responce. I have used your regex but it is still not validating my format [email protected] but it is validating [email protected]. Then where Am I wrong?
0

try this

/^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,4}$/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.