Best practices for email address validation (including the + in gmail addresses)

后端 未结 5 1965
长情又很酷
长情又很酷 2021-02-09 12:46

I know there are a lot of questions on here about email validation and specific RegEx\'s. I\'d like to know what the best practices are for validating emails with respect to hav

5条回答
  •  梦如初夏
    2021-02-09 13:13

    If you need to validate emails via regexp, then read the standard or at least this article.

    The standard suggests to use this regexp:

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

    If that doesn't scare you, it should :)

提交回复
热议问题