我想检查用户输入是否是JavaScript的电子邮件地址,然后将其发送到服务器或试图发送电子邮件,以防止最基本的误解。
当前回答
显然,这就是:
/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i
从 http://fightingforalostcause.net/misc/2006/compare-email-regex.php 获取于 10 月 1 日。
当然,这就是忽略国际化。
其他回答
以下是 Regex 认证:
没有 @(-) 和(.) 之前的空间字符不应该在 @ 之后聚集在一起 没有 @ 2 字符之后的特殊字符必须在 @ 电子邮件之前的长度不应该低于 128 字符
function validateEmail(email) {
var chrbeforAt = email.substr(0, email.indexOf('@'));
if (!($.trim(email).length > 127)) {
if (chrbeforAt.length >= 2) {
var re = /^(([^<>()[\]{}'^?\\.,!|//#%*-+=&;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
//var re = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
return re.test(email);
} else {
return false;
}
} else {
return false;
}
}
Sectrean的解决方案工作得很好,但它失败了我的镜头,所以我添加了一些漏洞:
function validateEmail(email){
var re = /^(([^<>()[]\\.,;:\s@\"]+(\.[^<>()[]\\.,;:\s@\"]+)*)|(\".+\"))@(([[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
这里是一个非常好的讨论,关于使用常规表达式来验证电子邮件地址; “比较电子邮件地址验证常规表达式”
下面是当前的顶级表达式,即JavaScript兼容,用于参考目的:
/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i
这个问题比一眼看起来更难回答。
世界各地有大量的人正在寻找“统治他们所有”,但事实是,有大量的电子邮件提供商。
为什么? 根据 RFC: https://en.wikipedia.org/wiki/Email_address#RFC_specification。
The local-part of the email address may use any of these ASCII characters:
- uppercase and lowercase Latin letters A to Z and a to z;
- digits 0 to 9;
- special characters !#$%&'*+-/=?^_`{|}~;
- dot ., provided that it is not the first or last character unless quoted, and provided also that it does not appear consecutively unless quoted (e.g. John..Doe@example.com is not allowed but "John..Doe"@example.com is allowed);[6]
Note that some mail servers wildcard local parts, typically the characters following a plus and less often the characters following a minus, so fred+bah@domain and fred+foo@domain might end up in the same inbox as fred+@domain or even as fred@domain. This can be useful for tagging emails for sorting, see below, and for spam control. Braces { and } are also used in that fashion, although less often.
- space and "(),:;<>@[\] characters are allowed with restrictions (they are only allowed inside a quoted string, as described in the paragraph below, and in addition, a backslash or double-quote must be preceded by a backslash);
- comments are allowed with parentheses at either end of the local-part; e.g. john.smith(comment)@example.com and (comment)john.smith@example.com are both equivalent to john.smith@example.com.
A__z/J0hn.sm{it!}h_comment@example.com.co
如果你尝试这个地址我赌它将失败在所有的或大部分的regex发布在整个网络上. 但记住这个地址遵循RFC规则,所以它是公平有效的。
想象一下我的失望,因为我无法在任何地方注册与这些 regex 检查!!!
如何处理,是的?
"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com
在这样做时,一个好做法是“重新输入您的电子邮件”输入,以避免用户输入错误,如果这对您来说不够,请添加一个提前提交的模型窗口,标题为“这是您的当前电子邮件吗?”然后用户输入的电子邮件在一个H2标签中,您知道,清楚地显示他们输入的电子邮件,然后一个“是的,提交”按钮。
我稍微修改了Jaymon的答案,对于那些想要真正简单的验证的人来说:
anystring@anystring.anystring
常规表达:
/^\S+@\S+\.\S+$/
防止多种 @ 标志相匹配:
/^[^\s@]+@[^\s@]+\.[^\s@]+$/
上面的序列匹配整个序列,删除领先和 ^ 并追踪 $ 如果你想匹配在序列中的任何地方。
如果你想匹配整个字符串,你可能想先切割()字符串。
示例 JavaScript 功能:
函数验证电子邮件(电子邮件) { var re = /\S+@\S+\.\S+/; return re.test(电子邮件); } console.log(验证电子邮件(我的电子邮件是 anystring@anystring.any')); // true console.log(验证电子邮件(我的电子邮件是 anystring@anystring.any')); // false
推荐文章
- 将lodash导入到angular + typescript应用程序中
- 什么时候使用setAttribute vs .attribute=在JavaScript?
- 每60秒调用一个函数
- 如何隐藏元素而不让他们在页面上占用空间?
- 我如何链接到谷歌地图与特定的经度和纬度?
- 精度为2的小数的简单正则表达式
- 我如何上传一个文件与JS获取API?
- 使用管道字符("|")分割字符串
- 在JavaScript中是否有一个“not in”操作符来检查对象属性?
- 防止跨度元件断行
- 我如何能得到滚动条的位置与JavaScript?
- JavaScript中的yield关键字是什么?
- 如何显示一个对象的所有方法?
- 如何防止滚动条重新定位网页?
- .js和.mjs文件有什么区别?