最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to validate email by whitelist domain regex - Stack Overflow

programmeradmin1浏览0评论

I want to validate domain by whitelist such as : , .co.id, ,

here i have a regex pattern :

/^[_a-z0-9-]+(\.[_a-z0-9-]+)*(\+[a-z0-9-]+)?@[a-z0-9-]+(\.[a-z0-9-]+)*$/i;

so if the user input :

  • [email protected] -> invalid
  • [email protected] -> valid

anyone can help me out ? Thank you

I want to validate domain by whitelist such as : . , .co.id, ,

here i have a regex pattern :

/^[_a-z0-9-]+(\.[_a-z0-9-]+)*(\+[a-z0-9-]+)?@[a-z0-9-]+(\.[a-z0-9-]+)*$/i;

so if the user input :

  • [email protected] -> invalid
  • [email protected] -> valid

anyone can help me out ? Thank you

Share Improve this question asked Jan 8, 2019 at 8:08 Not a PwntesterNot a Pwntester 751 silver badge10 bronze badges 3
  • @[a-z0-9-]+(.[a-z0-9-]+)$/i; fiddle this part with something like @[a-z0-9-]+\.[|...|other-tld] you can fiddle on regex101 it also had helpful guide on the lower right. – Bagus Tesa Commented Jan 8, 2019 at 8:08
  • Do you have a list of domains that you want to check inside the regex (e.g. [|org]), or do you want to extract the domain with the regex and then pare it some other way? – ChatterOne Commented Jan 8, 2019 at 8:21
  • solved, thank you guys ... – Not a Pwntester Commented Jan 8, 2019 at 9:00
Add a ment  | 

2 Answers 2

Reset to default 3

Try this

const isValidEmail = (email, allowedDomains) => {
  const p = `(${allowedDomains.join('|').replace(/\./g,'\\.')})$`;
  if(! new RegExp(p).test(email) ) return false

  const r = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*(\+[a-z0-9-]+)?@[a-z0-9-]+(\.[a-z0-9-]+)*$/i;
  if (!r.test(email)) return false;
  
  return true
}

const isValidEmail = (email, allowedDomains) => {
  const p = `(${allowedDomains.join('|').replace(/\./g,'\\.')})$`;
  if(! new RegExp(p).test(email) ) return false

  const r = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*(\+[a-z0-9-]+)?@[a-z0-9-]+(\.[a-z0-9-]+)*$/i;
  if (!r.test(email)) return false;
  
  return true
}



// Test 

let domains = [".", ".co.id", ""];

let validEmails = [
  "[email protected]",
  "[email protected]",
  "[email protected]",
   "[email protected]"
].filter(e=> isValidEmail(e,domains))


console.log(validEmails);

Explanation: in regexp It try to match group (\.|\.co\.id|\)$ where "$" means end of string and "\." is escape for dot in regexp.

You can proceed in two steps:

  • Check that the email is well formed -> example: https://www.regular-expressions.info/email.html, there are many sources on this subject

  • Validate that the domain is in the whitelist of domains

function validateEmail(email) {
   
    //check that the input string is an well formed email 
    var emailFilter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    if (!emailFilter.test(email)) {
        return false;
    }

    //check that the input email is in the whitelist
    var s, domainWhitelist = [".", "co.id", ""];
    for (s of domainWhitelist)
      if(email.endsWith(s))
        return true;
    
    //if we reach this point it means that the email is well formed but not in the whitelist
    return false;
}

console.log("validate ->" + validateEmail(""));
console.log("validate abc ->" + validateEmail("abc"));
console.log("validate [email protected] ->" + validateEmail("[email protected]"));
console.log("validate [email protected] ->" + validateEmail("[email protected]"));

发布评论

评论列表(0)

  1. 暂无评论