Which least bad regex rejects certain invalid email addresses?

Possible duplicate:
Verify Javascript email address?

I know that there is no way to verify the email address using only regular expression, but I think I should be able to reject some obviously invalid ones to help friendly users who mistakenly entered something else, for example their name or password in the address field Email. Therefore, I do not want to reject valid email addresses, but would like to reject as many invalid email formats as possible using only regular expression. But I think any javascript / jquery / ... will be fine too.

Here is what I still have:

^ .+@. +\..+$ 

Yes, there is no regex for a real check, but there must be something to reject. No, this does not stop anyone from entering fake addresses. Yes, you should still send a confirmation email to confirm.

The above regex will be used from javascript.

-

In .NET, I would instead allow .NET to check the email format using

  try { var mailAddress = new MailAddress(possibleMailAddress); } 

and if everything is ok, check the DNS records for part of the domain to see if there can be a mail server. Only if the DNS records clearly indicate that there can be no mail server or no such domain, the email address is rejected. Then cache some of the results to speed up your search in the future.

However, I must send confirmation letters to verify ...

+6
source share
1 answer

The official RFC 2822 standard describes the syntax for valid email addresses with this regular expression:

 (?:[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])+)\]) 
+4
source

Source: https://habr.com/ru/post/921177/


All Articles