正则表达式不允许特定的域电子邮件ID angular js

时间:2018-06-20 08:14:16

标签: angularjs regex validation

如果用户从几个特定区域输入电子邮件,我想显示错误消息。我试过了,但没有解决。

Email <input type="text" name="email" ng-model="txtmail" ng-pattern="/^[a-zA-Z0-9_.+-]+@(?:(?:[a-zA-Z0-9-]+\.)?[a-zA-Z]+\.)?(?!(domain|domain1|domain3))\.com$/" required />

xyz@domain.com或 不允许xyz@DOMAIN.com。

2 个答案:

答案 0 :(得分:0)

Javascript:

const regex = /^[a-zA-Z0-9_.+-]+@((domain|domain1|domain)).com$/;
const str = `xyz@domain1.com`;
let m;

if ((m = regex.exec(str)) !== null) {
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

Online sample here

enter image description here

enter image description here

enter image description here

答案 1 :(得分:0)

您可以通过外部JavaScript函数检查邮件地址:

let success = () => {console.log("Valid address")};
let fail    = () => {console.log("ERROR - Invalid address")};
let regex = /^[a-zA-Z0-9_.+-]+@(?:(?:[a-zA-Z0-9-]+\.)?[a-zA-Z]+\.)?(domain|domain1|domain3)\.com$/g;

function checkInput(element) {
  if(regex.test(element.value)) {
    fail();
  }
  else {
    success();
  }
}
Email <input type="text" name="email" onchange="checkInput(this)" ng-model="txtmail" required />