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

后端 未结 5 1966
长情又很酷
长情又很酷 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:07

    + is a valid character in an email address. It doesn't matter if the domain isn't gmail.com or googlemail.com

    Regexes aren't actually a very good way of validating emails, but if you just want to modify your regex to handle the plus, change it to the following:

    /^([a-zA-Z0-9_.-\+])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/
    

    As an example of how this regex doesn't validate against the spec: The email ..@-.com is valid according to it.

提交回复
热议问题