代码之家  ›  专栏  ›  技术社区  ›  Ahmad Tahhan

使用JavaScript[duplicate]验证电子邮件后缀

  •  -1
  • Ahmad Tahhan  · 技术社区  · 7 年前

    我有一个mailchimp订阅表单,我正在尝试验证输入值,以便它只接受特定类型的电子邮件, @gmail.com

    [name]@gmail.com ,如果用户插入@gmail.com表单不应该提交,我试图用regex来实现这个功能,但是没有成功,那么我如何在JS中实现这样的功能呢。

    3 回复  |  直到 7 年前
        1
  •  2
  •   Toto    7 年前

    这就是你想要的: /@gmail\.com$/

    var test = [
        'abc@gmail.com',
        'abc@not_gmail.com',
    ];
    console.log(test.map(function (a) {
      return a+' :'+/@gmail\.com$/.test(a);
    }));
        2
  •  1
  •   Илья Зелень    7 年前

    你可以分开后检查零件@

    var test = [
        'abc@gmail.com',
        'abc@not_gmail.com',
    ]
    
    test.forEach(i => {
      console.log(i.split('@')[1] === 'gmail.com')
    })
        3
  •  0
  •   Tianjiao Huang    7 年前

    Toto's answer ,但我还想使用中的regex检查电子邮件前缀是否正确 How to validate an email address using a regular expression?

    var test = [
        'abc@gmail.com',
        'abc@not_gmail.com',
    ];
    console.log(test.map(function (a) {
      return a + ': ' + /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@gmail\.com$/.test(a);
    }));
    推荐文章