Home » c# » What is the simplest regular expression to validate emails to not accept them blindly?

What is the simplest regular expression to validate emails to not accept them blindly?

Posted by: admin November 30, 2017 Leave a comment

Questions:

When users create an account on my site I want to make server validation for emails to not accept every input.

I will send a confirmation, in a way to do a handshake validation.

I am looking for something simple, not the best, but not too simple that doesn’t validate anything. I don’t know where limitation must be, since any regular expression will not do the correct validation because is not possible to do it with regular expressions.

I’m trying to limit the sintax and visual complexity inherent to regular expressions, because in this case any will be correct.

What regexp can I use to do that?

Answers:
^\[email protected]\S+$

Questions:
Answers:

It’s possible to write a regular expression that only accept email addresses that follow the standards. However, there are some email addresses out there that doesn’t strictly follow the standards, but still work.

Here are some simple regular expressions for basic validation:

Contains a @ character:

@

Contains @ and a period somewhere after it:

@.*?\.

Has at least one character before the @, before the period and after it:

[email protected]+\..+

Has only one @, at least one character before the @, before the period and after it:

^[^@][email protected][^@]+\.[^@]+$

User AmoebaMan17 suggests this modification to eliminate whitespace:

^[^@\s][email protected][^@\s]+\.[^@\s]+$

Questions:
Answers:

Take your pick.

Here’s the one that complies with RFC 2822 Section 3.4.1

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

Just in case you are curious. 🙂

Questions:
Answers:

I think this little tweak to the expression by AmoebaMan17 should stop the address from starting/ending with a dot and also stop multiple dots next to each other. Trying not to make it complex again whilst eliminating a common issue.

(?!.*\.\.)(^[^\.][^@\s][email protected][^@\s]+\.[^@\s\.]+$)

It appears to be working (but I am no RegEx-pert). Fixes my issue with users copy&pasting email addresses from the end of sentences that terminate with a period.

i.e: Here’s my new email address [email protected]

Questions:
Answers:

^[a-zA-Z0-9_.+-][email protected][a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$

  • Only 1 @
  • Several domains and subdomains
Questions:
Answers:

“^\w[^@]*@\w[^@.]+.\w[^.]{2,4}$”. Does not allow more than one @ or period, works great so far.

Also, to test out different regular expressions, download Expresso from http://www.ultrapico.com/. It really helped me.