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

12
  • In fact, Google can already apply for a "gmail" top level domain. It's pretty expensive but it can be done :) Commented Feb 3, 2012 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. Commented Feb 3, 2012 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 Commented Feb 3, 2012 at 13:37
  • 1
    duplicate of stackoverflow.com/questions/201323/… Commented Feb 3, 2012 at 13:38
  • 3
    You're also disallowing upper-case letters. Also, that regex does not actually accept the string you say it does. Commented Feb 3, 2012 at 13:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks... I'll have a look at the sites that you have mentioned to understand the regular expression that you have built :)
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...

1 Comment

Thanks... and yes you are right... it'll not cover all the valid email addresses... i can continue playing with it.. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.