且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Javascript和PHP中验证非英语(UTF-8)编码的电子邮件地址?

更新时间:2023-09-25 13:06:10

尝试验证电子邮件地址可能不是好主意。规范( RFC5321 RFC5322 )允许如此多的灵活性,使用正则表达式验证它们是不可能,并且使用函数进行验证是一项很大的工作。这样做的结果是大多数电子邮件验证方案最终拒绝了大量有效的电子邮件地址,这对用户的不方便性很大。 (到目前为止,最常见的例子是不允许 + 字符。)

Attempting to validate email addresses may not be a good idea. The specifications (RFC5321, RFC5322) allow for so much flexibility that validating them with regular expressions is literally impossible, and validating with a function is a great deal of work. The result of this is that most email validation schemes end up rejecting a large number of valid email addresses, much to the inconvenience of the users. (By far the most common example of this is not allowing the + character.)

更有可能用户将(意外地或故意地)输入一个不正确的电子邮件地址而不是一个无效的电子邮件地址,所以实际上验证是很大的工作,很少的好处,如果你不正确地执行可能的费用。

It is more likely that the user will (accidentally or deliberately) enter an incorrect email address than in an invalid one, so actually validating is a great deal of work for very little benefit, with possible costs if you do it incorrectly.

我建议您检查客户端是否存在 @ 字符,然后发送确认电子邮件以进行验证;这是最实际的验证方式,它确认地址也是正确的。

I would recommend that you just check for the presence of an @ character on the client and then send a confirmation email to verify it; it's the most practical way to validate and it confirms that the address is correct as well.