0

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.

5
  • \.com$ requires the string to end with .com, and it is certainly not omnitracs, xrscorp, nor roadnet, you misused the lookahead here. You need custom logic here, use \.([^.]+)$ and inspect the group value inside code. Commented Jun 20, 2018 at 8:16
  • xyz.domain.com or xyz.DOMAIN.com not be allowed. Is it [email protected] or [email protected] no ? Commented Jun 20, 2018 at 9:47
  • yes @sgrillon it was typo error Commented Jun 20, 2018 at 12:11
  • @WiktorStribiżew can you help me to correct that regex. I want to use it with ng-pattern Commented Jun 20, 2018 at 12:42
  • You can't do it with just ng-pattern regex. Commented Jun 20, 2018 at 13:13

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

for every email it says invalid
nope its taking [email protected] as valid which should be invalid
I've edited your regex to /^[a-zA-Z0-9_.+-]+@(?:(?:[a-zA-Z0-9-]+\.)?[a-zA-Z]+\.)?(domain|domain1|domain3)\.com$/g - now it works.
1

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

3 Comments

see new screeshot, domain.com is OK
Nope still it is passing [email protected] which is wrong
@Rahulgupta, you do not read the good link (version 2): regex101.com/r/9iLfck/2 see the scrennshot number 3 (1 match, 19 steps)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.