-1

I wanna find something that would work like:

Exclude all emails that are @aaa.* like someone@aaa.com or someoneElse@aaa.net But let NoEmail@aaa.com and Exception@aaa.com

I don't know how to combine to emphasize the first rule but have exceptions.

Also i would like to add a email pattern verification so it has an @ then a . (i have this rule, just need to combine all 3 items)

Zuzzieh
  • 7
  • 2
  • What you have tried so far? – Devang Feb 18 '20 at 08:00
  • Sincerely a lot of combinations, I have managed to work it to do the 3rd rule but not in combination with rule 1 and 2. (?=([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$|NoEmail@aaa.com|Exception@aaa.com)(?=@aaa.+[\w]) – Zuzzieh Feb 18 '20 at 08:05
  • I don't get it why i got close on this question, as no answer is in either the 2 "duplicate posts" there is no answer on how can i combine the 2 to exclude all but keep a whitelist.... how can I reopen this? – Zuzzieh Feb 18 '20 at 08:55

1 Answers1

0

Why not execute these conditions in separate statements? First check if it's email at all, then check if it's in whitelist. And finally reject any not allowed emails.

var emails = {
  'notEmailAtAlll': false,
  'someone@aaa.com': false,
  'someoneElse@aaa.net': false,
  'ExcludeEmail@aaa.com': false,
  'NoEmail@aaa.com': true,
  'Exception@aaa.com': true
};

var whitelist = [
  'NoEmail@aaa.com',
  'Exception@aaa.com'
];

for (let [email, condition] of Object.entries(emails)) {
  console.log(email, condition, validateEmail(email));
}

function validateEmail(email) {
  // Check if email. Replace regex with proper email checker regex
  if (!/\w+@\w+\./.test(email)) {
    return false;
  }
  
  if (whitelist.includes(email)) {
    return true;
  }

  if (email.indexOf('@aaa.') !== -1) {
    return false;
  }
  
  return true;
}
Justinas
  • 34,232
  • 3
  • 56
  • 78
  • Yeah, i thought about this, but I was thinking if there is a cleaner way to go just with RegEx. – Zuzzieh Feb 18 '20 at 08:34