-3

I have a form which contain textbox for email. I am validating email address using reg Expression in javascript.

I used following reg :-

var regEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

This reg is validating email address but the issue is if i used email address like abc@gmail.com.com . It not produce error.

Please suggest me any other reg expression which not accept email address for abc@gmail.com.com

user3440583
  • 337
  • 1
  • 4
  • 8
  • In the future, please use the search feature to look for answers to your question, first. This particular question has been answered often, before. – Cerbrus Jun 02 '14 at 07:02
  • @Cerbrus i checked mostly links of this question but all result r accepting this email address (abc@gmail.com.com) that's why i asked this question again . – user3440583 Jun 02 '14 at 07:09
  • Why would `abc@gmail.com.com` be invalid? `name@position.company.com` is a pretty common email pattern. – Cerbrus Jun 02 '14 at 07:21

1 Answers1

0

To answer your question, this works: ^([a-zA-Z0-9_\.\-])+\@([a-zA-Z0-9\-])+\.[a-zA-Z0-9] But your regex won't work with all emails (like ones containing plus signs - that's a valid email). Yours also fails if I wanted to send an email to someone on a subdomain, or someone who is using one of the (now many) TLDs that are more than four characters. Also, gmail.com.com is a perfectly valid domain name.

Do not use Regex to validate an email address. It will not work.

If you really want to use regex to validate an email address, you can use this fully compliant regex to validate emails. Notice how long it is? Yes, that's why you shouldn't validate email addresses with regex. This answer covers it pretty well.

Community
  • 1
  • 1