本文介绍了如何在 Javascript 和 PHP 中验证非英语 (UTF-8) 编码的电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发的网站的一部分包含用户必须提供其电子邮件地址的注册过程.就在最近,我意识到基于非 ASCII 的域是可能的(电子邮件也是如此).我的后端是 utf-8 编码的 MySQL,我希望任何用户(具有不同语言环境)都应该能够输入他们的电子邮件,但不知道如何验证这种电子邮件地址.

Part of a website I am currently working on contains registration process where users have to provide their email address. Just recently I became aware that non-ascii based domains are possible (so is email).My backend is utf-8 encoded MySQL where I am expecting any users (with differnt locales) should be able to enter their email but don't know how to validate this kind of email address.

目前我正在测试 jquery 工具,它可以正确验证英文电子邮件地址,但无法验证非 ascii 电子邮件.我也需要在服务器端用 php 做同样的事情.有没有可以验证这种电子邮件地址的正则表达式?

Currently I am testing out jquery tools and it validates the english email address correctly but fails to validate non ascii email. Also I need to do same at server side with php. Is there a regular expression that can validate this kind of email address?

我已经尝试过这个,但它在 jquery 工具中失败了(这只是演示示例,我也不明白这一点)

I have tried this but it fails in jquery tools (this is just example for demo, I don't understand this too)

闪耀发光@闪耀发光.com

闪闪发光@闪闪发光.com

此外,当他们使用自己的 IME 输入英文电子邮件地址 (jonesmith@somemail.com) 时会发生什么.这可以使用我们用于英语邮件验证的当前正则表达式进行验证.目前我不必担心该电子邮件是否存在.

Also what will happen when they type their English email address (jonesmith@somemail.com) with their own IME. Can this be validated with current regular expression we have for English mail validation. Currently I don't have to worry if that email exist for not.

谢谢

推荐答案

尝试验证电子邮件地址可能不是一个好主意.规范(RFC5321RFC5322) 提供了很大的灵活性,以至于使用正则表达式验证它们实际上是不可能的,并且使用函数进行验证是一个大量的工作.这样做的结果是,大多数电子邮件验证方案最终都会拒绝大量有效的电子邮件地址,这给用户带来了很大的不便.(到目前为止,最常见的例子是不允许使用 + 字符.)

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.

这篇关于如何在 Javascript 和 PHP 中验证非英语 (UTF-8) 编码的电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-17 01:36