我想检查用户输入是否是JavaScript的电子邮件地址,然后将其发送到服务器或试图发送电子邮件,以防止最基本的误解。
当前回答
如果您正在使用 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
其他回答
与平板相反,这里是一个复杂的解决方案,但它在正确验证电子邮件方面做出了很好的工作:
function isEmail(email) {
return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(email);
}
使用如下:
if (isEmail('youremail@yourdomain.com')){ console.log('This is email is valid'); }
如果你正在使用NG模式和材料,这就是工作。
vm.validateEmail = '([a-zA-Z0-9_.]{1,})((@[a-zA-Z]{2,})[\\\.]([a-zA-Z]{2}|[a-zA-Z]{3}))';
只需检查输入的电子邮件地址是否有效,或者不使用 HTML。
<input type="email"/>
没有必要写一个函数来验证。
我认为这是最好的解决方案:
/^[^\s@]+@[^\s@]+\.[^\s@]+$/
它允许以下格式:
1. prettyandsimple@example.com 2. very.common@example.com 3. disposable.style.email.with+symbol@example.com 4. other.email-with-dash@example.com 9. #!$%&'*+-/=?^_`{}|~@example.org 6. "()[]:,;@\\\"!#$%&'*+-/=?^_`{}| ~.a"@example.org 7. " "@example.org (space between the quotes) 8. üñîçøðé@example.com (Unicode characters in local part) 9. üñîçøðé@üñîçøðé.com (Unicode characters in domain part) 10. Pelé@example.com (Latin) 11. δοκιμή@παράδειγμα.δοκιμή (Greek) 12. 我買@屋企.香港 (Chinese) 13. 甲斐@黒川.日本 (Japanese) 14. чебурашка@ящик-с-апельсинами.рф (Cyrillic)
它是显而易见的多元化,并允许所有重要的国际角色,同时仍然执行基本的 anything@anything.anything格式,它将阻止技术上允许的空间,但它们是如此罕见,我很高兴做到这一点。
我正在寻找一个 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-.]+]*/
推荐文章
- 使伸缩项目正确浮动
- Babel 6改变了它导出默认值的方式
- 如何配置历史记录?
- ES6模板文字可以在运行时被替换(或重用)吗?
- [Vue警告]:找不到元素
- 可以在setInterval()内部调用clearInterval()吗?
- AngularJS控制器的生命周期是什么?
- 无法读取未定义的属性“msie”- jQuery工具
- 形式内联内的形式水平在twitter bootstrap?
- 我的蛋蛋怎么不见了?
- JavaScript中的排列?
- 自定义元素在HTML5中有效吗?
- JavaScript中有睡眠/暂停/等待功能吗?
- 如何触发自动填充在谷歌Chrome?
- 创建圈div比使用图像更容易的方法?