我想检查用户输入是否是JavaScript的电子邮件地址,然后将其发送到服务器或试图发送电子邮件,以防止最基本的误解。
当前回答
你不能预测信是正确的方式,用户打算它们是 - 这是最常见的错误......错过一个字母或输入错误的字母。
最终,无论你在JavaScript中做什么,你总是需要你的备份脚本来检查电子邮件是否也成功发送,并且它可能会有一个验证过程。
所以,如果你想确保它是某种类型的电子邮件地址,而不是他们的用户名,你只需要真正检查是否有 @ 符号在那里,至少 1 点,并留下所有剩余的备份代码。
var email = 'hello@example.com'
if(email.split('@').length == 2 && email.indexOf('.') > 0){
// The split ensures there's only 1 @
// The indexOf ensures there's at least 1 dot.
}
最好避免阻止用户输入有效的电子邮件,而不是实施如此多的限制,以至于它变得复杂。
这只是我的观点!
其他回答
只需检查输入的电子邮件地址是否有效,或者不使用 HTML。
<input type="email"/>
没有必要写一个函数来验证。
这个问题比一眼看起来更难回答。
世界各地有大量的人正在寻找“统治他们所有”,但事实是,有大量的电子邮件提供商。
为什么? 根据 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标签中,您知道,清楚地显示他们输入的电子邮件,然后一个“是的,提交”按钮。
如果你得到这个错误:使用常规表达式是安全敏感的。
这个解决方案是免费的“定期表达拒绝服务(ReDoS)”
Regex 可验证无电子邮件(ReDoS):
/^[a-z0-9](?!.*?[^\na-z0-9]{2})[^\s@]+@[^\s@]+\.[^\s@]+[a-z0-9]$/
请告诉我这个解决方案是否适合你,谢谢。
在现代浏览器中,您可以使用纯粹的JavaScript和DOM创建 @Sushil的答案:
function validateEmail(value) {
var input = document.createElement('input');
input.type = 'email';
input.required = true;
input.value = value;
return typeof input.checkValidity === 'function' ? input.checkValidity() : /\S+@\S+\.\S+/.test(value);
}
我把一个例子聚集在一个字符串 http://jsfiddle.net/boldewyn/2b6d5/. 结合功能检测和Squirtle的答案的字符串验证,它释放了你从常规的表达屠杀,并不会在旧的浏览器上跳舞。
最好的做法是使用 HTML5 内置电子邮件标签。
<input type="email" name="email">
或常见的电子邮件合成作为识别 @ 和. 从字符串是下面的。
^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-.]+$
请注意,这仍然会产生不合格的电子邮件,这将仍然符合规则,几乎不可能抓住他们所有,但这将改善情况一点。
推荐文章
- 使伸缩项目正确浮动
- Babel 6改变了它导出默认值的方式
- 如何配置历史记录?
- ES6模板文字可以在运行时被替换(或重用)吗?
- [Vue警告]:找不到元素
- 可以在setInterval()内部调用clearInterval()吗?
- AngularJS控制器的生命周期是什么?
- 无法读取未定义的属性“msie”- jQuery工具
- 形式内联内的形式水平在twitter bootstrap?
- 我的蛋蛋怎么不见了?
- JavaScript中的排列?
- 自定义元素在HTML5中有效吗?
- JavaScript中有睡眠/暂停/等待功能吗?
- 如何触发自动填充在谷歌Chrome?
- 创建圈div比使用图像更容易的方法?