如何在Javascript和PHP中验证非英语(UTF-8)编码的电子邮件地址? [英] How to validate non-english (UTF-8) encoded email address in Javascript and PHP?

查看:123
本文介绍了如何在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

当他们输入他们的英文电子邮件地址(jonesmith@somemail.com)与自己的IME。这可以使用我们用于英文邮件验证的当前正则表达式进行验证。目前我不用担心电子邮件是不存在的。

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.

谢谢

推荐答案

尝试验证电子邮件地址可能不是好主意。规范( 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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆