What is a good regular expression for catching typos in an email address?

C#RegexEmail Validation

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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:

.+@.+\..+

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

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

User AmoebaMan17 suggests this modification to eliminate whitespace:

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

And for accepting only one period [external edit: not recommended, does not match valid email adresses]:

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

Solution 2 - C#

^\S+@\S+$

Solution 3 - C#

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

  • Only 1 @
  • Several domains and subdomains

Solution 4 - C#

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]+@[^@\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].

Solution 5 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioneKek0View Question on Stackoverflow
Solution 1 - C#GuffaView Answer on Stackoverflow
Solution 2 - C#chaosView Answer on Stackoverflow
Solution 3 - C#cotoView Answer on Stackoverflow
Solution 4 - C#Carl HowarthView Answer on Stackoverflow
Solution 5 - C#JP AliotoView Answer on Stackoverflow