简言
在做用户注册时,常会用到邮箱/邮件地址的正则表达式。本文列举了几种方案,大家可以根据自己的项目情况,选择最适合的方案。
方案1 (常用)
规则定义如下:
利用以上规则给出如下正则表达式:
var pattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
完整测试代码
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>邮箱/邮件地址的正则表达式及分析(JavaScript,email,regex)</title> </head> <body> <div id="main"></div> <script> var pattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; w("pattern.test('cn42du@163.com') = "+pattern.test('cn42du@163.com')+";"); w("pattern.test('ifat3@sina.com.cn') = "+pattern.test('ifat3@sina.com.cn')+";"); w("pattern.test('ifat3.it@163.com') = "+pattern.test('ifat3.it@163.com')+";"); w("pattern.test('ifat3_-.@42du.cn') = "+pattern.test('ifat3_-.@42du.cn')+";"); w("pattern.test('ifat3@42du.online') = "+pattern.test('ifat3@42du.online')+";"); w("pattern.test('毛三胖@42du.cn') = "+pattern.test('毛三胖@42du.cn')+";"); function w(val) { document.getElementById("main").innerHTML += val +"<br />"; } </script> </body> </html>
测试结果:
pattern.test('cn42du@163.com') = true;
pattern.test('ifat3@sina.com.cn') = true;
pattern.test('ifat3.it@163.com') = true;
pattern.test('ifat3_-.@42du.cn') = true;
pattern.test('ifat3@42du.online') = false;
pattern.test('毛三胖@42du.cn') = false;
pattern.test('cn42du@163.com') = true;
pattern.test('ifat3@sina.com.cn') = true;
pattern.test('ifat3.it@163.com') = true;
pattern.test('ifat3_-.@42du.cn') = true;
pattern.test('ifat3@42du.online') = false;
pattern.test('毛三胖@42du.cn') = false;
方案1说明
方案1是最常用的邮件正则表达式验证方案,也适合大多数的应用场景。从以上测试可以看出,该表达式不支持.online及.store结尾的域名。如需兼容这类域名(大于4位),调整正则结尾{2,4}的限制部分即可(例:{2,8})。另一个问题是邮件用户名不能包括中文。
方案2 (修订方案1)
规则补充如下:
var pattern = /^([A-Za-z0-9_\-\.\u4e00-\u9fa5])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/;
完整测试代码
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>邮箱/邮件地址的正则表达式及分析(JavaScript,email,regex)</title> </head> <body> <div id="main"></div> <script> var pattern = /^([A-Za-z0-9_\-\.\u4e00-\u9fa5])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/; w("pattern.test('cn42du@163.com') = "+pattern.test('cn42du@163.com')+";"); w("pattern.test('ifat3@sina.com.cn') = "+pattern.test('ifat3@sina.com.cn')+";"); w("pattern.test('ifat3.it@163.com') = "+pattern.test('ifat3.it@163.com')+";"); w("pattern.test('ifat3_-.@42du.cn') = "+pattern.test('ifat3_-.@42du.cn')+";"); w("pattern.test('ifat3@42du.online') = "+pattern.test('ifat3@42du.online')+";"); w("pattern.test('毛三胖@42du.cn') = "+pattern.test('毛三胖@42du.cn')+";"); function w(val) { document.getElementById("main").innerHTML += val +"<br />"; } </script> </body> </html>
测试结果:
pattern.test('cn42du@163.com') = true;
pattern.test('ifat3@sina.com.cn') = true;
pattern.test('ifat3.it@163.com') = true;
pattern.test('ifat3_-.@42du.cn') = true;
pattern.test('ifat3@42du.online') = true;
pattern.test('毛三胖@42du.cn') = true;
方案3 (安全)
在手机验证码出现之前,差不多邮箱验证是保证用户唯一性的唯一条件。而临时邮箱(也称10分钟邮箱或一次性邮箱)的出现,则使得邮箱验证及帐户激活这种机制失去了意义。而临时邮箱的地址是不可枚举的,我们只能才采取白名单的方式,只允许有限的邮箱域名通过验证。
根据方案1的补充如下规则:
邮箱域名只能是163.com,qq.com或者42du.cn。
给出正则表达式如下:
var pattern = /^([A-Za-z0-9_\-\.])+\@(163.com|qq.com|42du.cn)$/;
完整测试代码
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>邮箱/邮件地址的正则表达式及分析(JavaScript,email,regex)</title> </head> <body> <div id="main"></div> <script> var pattern = /^([A-Za-z0-9_\-\.])+\@(163.com|qq.com|42du.cn)$/; w("pattern.test('cn42du@163.com') = "+pattern.test('cn42du@163.com')+";"); w("pattern.test('ifat3@sina.com.cn') = "+pattern.test('ifat3@sina.com.cn')+";"); w("pattern.test('ifat3.it@163.com') = "+pattern.test('ifat3.it@163.com')+";"); w("pattern.test('ifat3_-.@42du.cn') = "+pattern.test('ifat3_-.@42du.cn')+";"); w("pattern.test('ifat3@42du.online') = "+pattern.test('ifat3@42du.online')+";"); w("pattern.test('毛三胖dd@42du.cn') = "+pattern.test('毛三胖@42du.cn')+";"); function w(val) { document.getElementById("main").innerHTML += val +"<br />"; } </script> </body> </html>
测试结果:
pattern.test('cn42du@163.com') = true; pattern.test('ifat3@sina.com.cn') = false; pattern.test('ifat3.it@163.com') = true; pattern.test('ifat3_-.@42du.cn') = true; pattern.test('ifat3@42du.online') = false; pattern.test('毛三胖dd@42du.cn') = false;
方案3验证虽然能保证安全性,但是如果白名单太长会造成模式字符串太长。这时可以将邮箱域名白名单写成数组,利用正则表达式做初步验证,用白名单做域名的二次验证。
现给出邮箱验证函数如下:
var isEmail = function (val) { var pattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; var domains= ["qq.com","163.com","vip.163.com","263.net","yeah.net","sohu.com","sina.cn","sina.com","eyou.com","gmail.com","hotmail.com","42du.cn"]; if(pattern.test(val)) { var domain = val.substring(val.indexOf("@")+1); for(var i = 0; i< domains.length; i++) { if(domain == domains[i]) { return true; } } } return false; } // 输出 true isEmail(cn42du@163.com);
上述isEmail()函数列举了常用的11种邮箱域名,大家可以根据需要适当补充或删减。
以上为三胖对邮箱正则表达式的理解和分析,如有不足请大家予以指正。
本文转载自微信公众号「SQL数据库」,作者丶平凡世界 。转载本文请联系开发公众...
本文转载自微信公众号「SH的全栈笔记」,作者SH。转载本文请联系SH的全栈笔记公...
idea官方推送了2020.2.4版本的更新,那么大家最关心的问题来了,之前激活idea202...
本文实例讲述了AJAX+Servlet实现的数据处理显示功能。分享给大家供大家参考,具...
CKeditor,以前叫FCKeditor,已经使用过好多年了,功能自然没的说。最近升级到3....
大家好,我是狂聊君。 今天来聊一聊 Mysql 缓存池原理。 提纲附上,话不多说,直...
来源:DeepenStudy 漏洞文件:js.asp % Dimoblog setoblog=newclass_sys oblog.a...
前言 项目开发中不管是前台还是后台都会遇到烦人的null,数据库表中字段允许空值...
在Flash Player 10.1及以上版本中,adobe新增了全局错误处理程序UncaughtErrorEv...
问题:我们在做flex的开发中,如果用到别人搭建好的框架,而别人的server名称往...