Vivek
Vivek

Reputation: 2101

Email Regular Expression Issue

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

Upvotes: 1

Views: 283

Answers (2)

LordZardeck
LordZardeck

Reputation: 8293

^[_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.

Upvotes: 1

Ian Wood
Ian Wood

Reputation: 6573

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...

Upvotes: 1

Related Questions