-5

Possible Duplicate:
How to use a regular expression to validate an email addresses?

\A[\w.-]+\w@[a-z\d\-.]+\.[a-z]+\z

What do you think of the above expression for email validation. Any errors, any loopholes? Thank in advance for the support.

Community
  • 1
  • 1
Yaw Boakye
  • 10,352
  • 1
  • 17
  • 25
  • It's not actually a duplicate. My regular expression should only make sure the email is valid. A valid email could be `a@b.c` where `a` does not allow any symbols apart from `-`,`.` and `_`. `b` contains alphabets and/or numerals and `-`. It should ensure at least one TLD. Check it out with [Rubular](http://www.rubular.com) – Yaw Boakye Jun 05 '12 at 09:50
  • 1
    The other question is about a regular expression to make sure an email is valid. How is it not a duplicate? – Mechanical snail Jun 05 '12 at 09:52
  • 1
    Have you tested your regex? It does not even match your example "a@b.c" – stema Jun 05 '12 at 10:01
  • `a@b.c` is a shorthand not what should be matched. I tried to explain the parts in the rest of the comment. @Mechanical snail I checked your link and they have worries that I do not care about. Do you use any of those in your applications? – Yaw Boakye Jun 05 '12 at 10:14
  • 2
    It won't catch RFC-compliant plus-addresses. Please don't reinvent the wheel and create "Yet Another Invalid Validator." – Todd A. Jacobs Jun 05 '12 at 10:48
  • The only way to see if an email is _really_ valid is to send a confirmation message and see if it arrives. – Matheus Moreira Jun 05 '12 at 11:22

2 Answers2

1

What do you think of the above expression for email validation.

For one, it doesn't accept my address. So, there's obviously a bug in there somewhere. I suggest you read RfC5322 carefully, it describes the valid syntax for addresses quite clearly, although unfortunately it hasn't yet been updated for IDN.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
0
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b

Regex from regular expressions info. It should match RFC 2822 standard.

Darshana
  • 2,462
  • 6
  • 28
  • 54