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

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

    The specs allow for some really crazy ugly email addresses. I'm often very annoyed by websites even complaining about perfectly normal, valid email addresses, so please, try not to reject valid email addresses. It's better to accept some illegal addresses than to reject legal ones.

    Like others have suggested, I'd go with using a simple regexp like /.+@.+/ and then sending a verification email. If it's important enough to validate, it's important enough to verify, because a legal email address can still belong to someone other than your visitor. Or contain an unintended but fatal typo.

    *Edit: removed the dot from the domain part of the regex, because a@to is still a valid email address. So even my super simplified validation rejected valid addresses. Is there any downside at all to just accepting everything that contains an @ with something in front and behind it?

提交回复
热议问题