-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 [email protected] . It not produce error.

Please suggest me any other reg expression which not accept email address for [email protected]

user3440583
  • 337
  • 1
  • 4
  • 8

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