规则定义如下:
以大写字母[a-z]、小写字母[a-z]、数字[0-9]、下滑线[_]、减号[-]及点号[.]开头,并需要重复一次至多次[+]。
中间必须包括@符号。
@之后需要连接大写字母[a-z]、小写字母[a-z]、数字[0-9]、下滑线[_]、减号[-]及点号[.],并需要重复一次至多次[+]。
结尾必须是点号[.]连接2至4位的大小写字母[a-za-z]{2,4}。
利用以上规则给出如下正则表达式:
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><p id="main"></p><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)
规则补充如下:
用户名可以包括中文[\u4e00-\u9fa5]
域名结尾最长可为8位{2,8}
更新后的正则表达式如下:
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><p id="main"></p><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><p id="main"></p><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;}// 输出 trueisemail(cn42du@163.com);
上述isemail()函数列举了常用的11种邮箱域名,大家可以根据需要适当补充或删减。
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
axios中的302状态码
封装vue2路由导航钩子并在实战中使用
以上就是js做出验证邮箱/邮件地址正则的详细内容。
