我想检查用户输入是否是JavaScript的电子邮件地址,然后将其发送到服务器或试图发送电子邮件,以防止最基本的误解。
当前回答
我正在使用这个功能
/**
* @param {*} email
*/
export const validateEmail = email => {
return new RegExp(/[\w-]+@([\w-]+\.)+[\w-]+/gm).test(email);
};
其他回答
我正在寻找一个 Regex 在 JS 通过所有电子邮件地址测试案例:
email@example.com 有效电子邮件 firstname.lastname@example.com 电子邮件包含地址字段 email@subdomain.example.com 电子邮件包含字段与域名 firstname+lastname@example.com Plus 标志被认为是有效的字符 email@192.0.2.123 域名是有效的 IP 地址 email@[192.0.2.123] IP 地址周围的广场 被认为是有效的“email”@example.com 电子邮件周围的引用是 c
这里我们去:
HTTP://regexr.com/3f07j
或雷格斯:
Regex = /(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@[*[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+]*/
我知道它不是雷格斯,但以任何方式......
这是一个节点和npm包电子邮件存在的例子,这是最终检查电子邮件是否存在,如果它在正确的形式:)
这将粘贴电子邮件,如果它的回复,如果它没有回复,它将返回虚假或其他真实。
function doesEmailExist(email) {
var emailExistence = require('email-existence');
return emailExistence.check(email,function (err,status) {
if (status) {
return status;
}
else {
throw new Error('Email does not exist');
}
});
}
在您的验证函数中使用此代码:
var emailID = document.forms["formName"]["form element id"].value;
atpos = emailID.indexOf("@");
dotpos = emailID.lastIndexOf(".");
if (atpos < 1 || ( dotpos - atpos < 2 ))
{
alert("Please enter correct email ID")
return false;
}
您可以使用 jQuery. 内部规则定义:
eMailId: {
required: true,
email: true
}
如果您正在使用 Closure 您可以使用内置的 goog.format.Email 地址类型:
http://docs.closure-library.googlecode.com/git/class_goog_format_EmailAddress.html
例如:
goog.format.EmailAddress.isValidAddrSpec("blah@blah.com")
请注意,通过阅读来源(上面的链接),您可以看到评论表示 IDN 不受支持,并且仅旨在覆盖大多数地址:
// This is a fairly naive implementation, but it covers 99% of use cases.
// For more details, see http://en.wikipedia.org/wiki/Email_address#Syntax
// TODO(mariakhomenko): we should also be handling i18n domain names as per
// http://en.wikipedia.org/wiki/Internationalized_domain_name
推荐文章
- 使伸缩项目正确浮动
- Babel 6改变了它导出默认值的方式
- 如何配置历史记录?
- ES6模板文字可以在运行时被替换(或重用)吗?
- [Vue警告]:找不到元素
- 可以在setInterval()内部调用clearInterval()吗?
- AngularJS控制器的生命周期是什么?
- 无法读取未定义的属性“msie”- jQuery工具
- 形式内联内的形式水平在twitter bootstrap?
- 我的蛋蛋怎么不见了?
- JavaScript中的排列?
- 自定义元素在HTML5中有效吗?
- JavaScript中有睡眠/暂停/等待功能吗?
- 如何触发自动填充在谷歌Chrome?
- 创建圈div比使用图像更容易的方法?