1

I have created the following regular expression for validating my email

^[_a-z0-9-]+(\.[_a-z0-9-])+@[a-z0-9-]+\.[a-z0-9-]+$

Now the issue that I am facing with the expression is an email of the following format is also being accepted

abc_xyz@gmail

I want the regular expression to enforce that the email address should contain the ".com/info/net" at the end , and that the above mentioned email format should be marked as invalid.

how can i achieve this

Vivek
  • 2,091
  • 11
  • 46
  • 61
  • In fact, Google can already apply for a "gmail" top level domain. It's pretty expensive but it can be done :) – Álvaro González Feb 03 '12 at 13:36
  • You don't need to reinvent the wheel; there are plenty of examples and discussions on the web about email regexp's. – ori Feb 03 '12 at 13:37
  • @ÁlvaroG.Vicario Yea.. considering they got the money. :) But in my case i would like to enforce the `.com` at the end, how can i achieve that – Vivek Feb 03 '12 at 13:37
  • possible duplicate of [PHP email validation question?](http://stackoverflow.com/questions/2514810/php-email-validation-question). The question talks about PHP but the accepted answer is quite language agnostic. – Álvaro González Feb 03 '12 at 13:37
  • http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html – Mat Feb 03 '12 at 13:38
  • 1
    duplicate of http://stackoverflow.com/questions/201323/how-to-use-a-regular-expression-to-validate-an-email-addresses – Matt Feb 03 '12 at 13:38
  • 3
    You're also disallowing upper-case letters. Also, that regex does not actually accept the string you say it does. – Pointy Feb 03 '12 at 13:38
  • possible duplicate of [JavaScript Regular Expression Email Validation](http://stackoverflow.com/questions/940577/javascript-regular-expression-email-validation) – Mat Feb 03 '12 at 13:38
  • this is not a duplicate as he wishes to constrain the domains and none of those questions answer that – LordZardeck Feb 03 '12 at 13:44
  • @ori well, i thought of posting a domain specific question as that might be helpful to others who want to implement it. – Vivek Feb 03 '12 at 13:52
  • @Pointy Yes, i overlooked that one. Thanks for the tip – Vivek Feb 03 '12 at 13:53
  • Why are you trying to limit the tld's? What about .me, .us, .gov, .name, etc? – Brian Nickel Feb 03 '12 at 14:55

2 Answers2

1
^[_a-z0-9-]+(\.[_a-z0-9-])+@[a-z0-9-]+\.(?:[A-Z]{2}|com|org|net|info)$

Try that. You can add as many other domain restraints as you wish. Just separate them with |

You may wish to take a look at this site: http://www.regular-expressions.info/ for more info on regex, and http://www.regular-expressions.info/email.html for specific help on emails.

LordZardeck
  • 7,953
  • 19
  • 62
  • 119
  • Thanks... I'll have a look at the sites that you have mentioned to understand the regular expression that you have built :) – Vivek Feb 03 '12 at 13:50
1

You need to escape - inside your character classes, but still that email address should fail.

try:

^[a-zA-Z0-9][a-zA-Z0-9\._%\-\+]+@[a-zA-Z0-9\.\-]+\.[a-zA-Z]+$

even that prob does not cover all valid email addresses though...

Ian Wood
  • 6,515
  • 5
  • 34
  • 73
  • Thanks... and yes you are right... it'll not cover all the valid email addresses... i can continue playing with it.. :) – Vivek Feb 03 '12 at 13:51