我想检查用户输入是否是JavaScript的电子邮件地址,然后将其发送到服务器或试图发送电子邮件,以防止最基本的误解。
当前回答
这个问题比一眼看起来更难回答。
世界各地有大量的人正在寻找“统治他们所有”,但事实是,有大量的电子邮件提供商。
为什么? 根据 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标签中,您知道,清楚地显示他们输入的电子邮件,然后一个“是的,提交”按钮。
其他回答
在 nodeJS 中,您也可以使用验证器节点模块,并简单地使用此类
使用 npm 安装验证器安装图书馆
var validator = require('validator');
validator.isEmail('foo@bar.com'); //=> true
ES6 样品
const validateEmail=(email)=> /^(([^<>()\[\]\\.,;:\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,}))$/.test(email);
下面是一个简单的 regex 会只是检查一个电子邮件的基本格式,例如,X@Y.C:
上一篇:S + S + S + S + S
这是由数千个网站使用的官方Rails指南提出的验证的JavaScript翻译:
/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
相对简单,但对大多数常见的错误进行测试。
测试了数千个电子邮件的数据集,并且有零的虚假负面 / 积极。
使用例子:
const emailRegex = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i;
emailRegex.test('email@example.com'); // true
// Multi-word domains
emailRegex.test('email@example.co.uk'); // true
emailRegex.test('email@mail.gmail.com'); // true
// Valid special characters
emailRegex.test('unusual+but+valid+email1900=/!#$%&\'*+-/=?^_`.{|}~@example.com') // true
// Trailing dots
emailRegex.test('email@example.co.uk.'); // false
// No domain
emailRegex.test('email@example'); // false
// Leading space
emailRegex.test(' email@example.com'); // false
// Trailing space
emailRegex.test('email@example.com '); // false
// Incorrect domains
emailRegex.test('email@example,com '); // false
// Other invalid emails
emailRegex.test('invalid.email.com') // false
emailRegex.test('invalid@email@domain.com') // false
emailRegex.test('email@example..com') // false
通用电子邮件 regex (RFC 5322 官方标准): https://emailregex.com/
JavaScript 的内容:
/^(([^<>()\[\]\\.,;:\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,}))$/
推荐文章
- JavaScript: override alert()
- 重置setTimeout
- 如何确保<select>表单字段被禁用时提交?
- 如何在HTML文本换行?
- jQuery有不聚焦的方法吗?
- Java中的正则表达式命名组
- 反应钩子-正确的方式清除超时和间隔
- 使用正则表达式搜索和替换Visual Studio代码
- TypeScript枚举对象数组
- 在React.js中正确的img路径
- 在React.js中更新组件onScroll的样式
- onClick ReactJS调用多个函数
- 如何在JavaScript中转义单引号(')?
- Ng-repeat结束事件
- 谷歌MAP API未捕获的类型错误:无法读取属性“offsetWidth”为空