Rohit Kotak
Rohit Kotak

Reputation: 305

Regex for not allowing specific domain email id angular js

I want to show error message if user enter the email from few specific doamins. I tried this but didn't work out.

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 />

[email protected] or [email protected] should not be allowed.

Upvotes: 0

Views: 1600

Answers (2)

GalAbra
GalAbra

Reputation: 5138

You can check the mail address via external JavaScript function:

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 />

Upvotes: 2

St&#233;phane GRILLON
St&#233;phane GRILLON

Reputation: 11884

Javascript:

const regex = /^[a-zA-Z0-9_.+-]+@((domain|domain1|domain)).com$/;
const str = `[email protected]`;
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

Upvotes: 1

Related Questions