当前位置 博文首页 > JavaScript email邮箱/邮件地址的正则表达式及分析

    JavaScript email邮箱/邮件地址的正则表达式及分析

    作者:毛三胖 时间:2021-09-10 19:18

    简言

    在做用户注册时,常会用到邮箱/邮件地址的正则表达式。本文列举了几种方案,大家可以根据自己的项目情况,选择最适合的方案。

    Email正则表达式

    方案1 (常用)

    规则定义如下:

    • 以大写字母[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>
    <div ></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)

    规则补充如下:

    • 用户名可以包括中文[\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>
    <div ></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 ></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种邮箱域名,大家可以根据需要适当补充或删减。

    以上为三胖对邮箱正则表达式的理解和分析,如有不足请大家予以指正。

    jsjbwy
    下一篇:没有了