3

I am working on a website, where i have written the following regex to validate email ids and used the regex in a regular expression validator. The regex is

\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

Its accepting valid email ids as i expected. But when my QA went with negative input testing like ak@gmail.com.com.com.com then its allowing and saying the email id is valid. I have searched every where and almost tried in all aspects to solve this. Can any one pls help me to restrict a mail ids like above. Actually, the email ids like ak@gmail.co.in can be accepted, but problem comes when the last string after . repeats. Please help. I am using dot net 2005.

Sylvain Defresne
  • 42,429
  • 12
  • 75
  • 85
  • 11
    ak@gmail.com.com.com.com is a correctly formatted email address. If you want to do proper checking, then you need to extract the host from the address (in this case gmail.com.com.com.com) and request DNS MX record for it. If it doesn't exist, then the address is invalid. – Aleks G Dec 20 '12 at 12:21
  • No, @AleksG i hav tried to enter gmail.com.com.com.com in the url and its showing Oops! Google Chrome could not find gmail.com.com.com.com for me. – Sai Kalyan Kumar Akshinthala Dec 20 '12 at 12:36
  • @SaiKalyanAkshinthala This is because the domain is not registered, but syntactically, this is a valid domain. By using `gmail.com.com.com.com.com` in your browser, you did check that there is no DNS A record for it. This does not invalidate the point that @AlexsG made that the address is correctly formatted. – Sylvain Defresne Dec 20 '12 at 12:40
  • possible duplicate of [Validate email address in Javascript?](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript) – Sylvain Defresne Dec 20 '12 at 12:40

3 Answers3

1

The format of valid email as specified in RFC822 is really complex, and regular expression are probably not the best tool to parse and validate them. If you still want to use regexp, you should consider checking the answer to Validate email address in Javascript? or parse email with regex in c#.

TL; DR: do not use regular expression to validate email address. Instead use a real RFC822 parser, and check the domain for the existence of a DNS MX record, and maybe try to send a email to the address with a link that can be used to validate the email has been received and the user own the address.

Community
  • 1
  • 1
Sylvain Defresne
  • 42,429
  • 12
  • 75
  • 85
  • @ Sylvain, i have already told dat i am using .Net 2005 and i have already checked the question which is in the link you have given. The answer provided, didn't add any clews for me. – Sai Kalyan Kumar Akshinthala Dec 20 '12 at 12:44
  • @SaiKalyanAkshinthala You're question is tagged with `javascript`. If you don't want responses pointing you to javascript, don't use that tag. – Sylvain Defresne Dec 20 '12 at 12:52
1

ak@gmail.com.com.com.com is syntactically correct. However, there are no MX records for gmail.com.com.com.com, which makes it unreachable, but not invalid.

If you want to check for this, you should first check for syntax (with that regexp you have), then lookup MX records for the hostname.

I'd also like to point out that the regexp you're using is really bad, and will invalidate a lot of valid mail addresses.

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
-2

add following expression to check email

 ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"  
RVD
  • 37
  • 1