建议使用 javaScript 验证电子邮件的好模式?
问候大家 我想用 javaScript 验证电子邮件 我需要使用最好的模式来匹配 请给我推荐一个好的图案
greetings all
i want to validate an email with javaScript
and i need to use the best pattern for the matching
please suggest me a good pattern
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了让您避免重新发明轮子,我推荐这篇高质量的关于regular-expressions.info的文章。
In order for you to avoid reinventing the wheel I recommend this quality article on regular-expressions.info.
这个
^([0-9a-zA-Z]([-\.\+\_\w]*[0-9a-zA-Z])*@([0-9a-zA-Z] [-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$
应匹配大多数情况。我可能会因为不包括每一个边缘情况而遭到大规模否决(电子邮件地址可以有各种疯狂的组合),但我认为我毫不夸张地说这将匹配 99.99% 的电子邮件地址。This
^([0-9a-zA-Z]([-\.\+\_\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$
should match most cases. I might get massively down-voted for not including every single edge case (email addresses can have all kinds of crazy combinations) but I don't think I'm exaggerating in saying this will match 99.99% of email addresses.这是标准验证正则表达式: http://www.ex-parrot .com/pdw/Mail-RFC822-Address.html for RFC 822 ;)
您可能想要更简单的东西(取自 http://www.regular-expressions.info/email.html):
/^[A-Z0-9._%+-]+@[A- Z0-9.-]+\.[AZ]{2,4}$/i
对于 JavaScriptThis is the standard validation regular expression: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html for RFC 822 ;)
You probably want something more simple (taken from http://www.regular-expressions.info/email.html):
/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
for JavaScript如果我没记错的话,您可能需要警惕的是,某些电子邮件服务器不符合 RFC822,因此对验证非常严格可能会排除一些“有效”电子邮件地址。根据您需要的验证级别,可以只检查电子邮件地址是否具有正确的基本格式 - 例如用句点分隔的一个或多个单词,后跟 @ 符号,后跟两个或多个分隔的单词按时期。
话虽如此,您可能还需要考虑为什么要首先验证电子邮件地址。
如果您只是想确保用户没有输入错误,则要求提供电子邮件地址并确认电子邮件地址,然后比较两者以确定该地址是否有效。 (这是很多网站使用的策略)
如果您想知道电子邮件地址是否真实,作为注册过程的一部分,那么注册可以分为两步过程,并带有确认电子邮件发送到用户在第一步中提供的地址,并且该电子邮件包含指向该过程第二步的链接。
我可能会对您的需求做出疯狂的假设,但我可能只是触发适当的思维过程。
What you might want to be wary of, if I remember correctly, is the fact that some email servers don't conform to RFC822, so being very strict on the validation might exclude some 'valid' email addresses. Depending on the level of validation that you need, it may be possible to just check that the email address has the correct basic format - something like one or more words separated by periods, followed by an @ symbol, followed by two or more words separated by periods.
Having said this, you may also want to consider why you are validating the email address in the first place.
If you simply want to make sure that the user didn't type it incorrectly, then ask for the email address and a confirmation of the email address, then compare the two to decide whether the address is valid or not. (This is the strategy used by quite a lot of websites)
If you want to know whether the email address is real or not, as part of a registration process, then the registration could be made into a two step process, with a confirmation email being sent to the address that the user supplies in the frist step, and that email contains a link to the second step of the process.
I may be making wild assumptions about your needs, but I may just trigger the appropriate thought processes.