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
2 Answers
Reset to default 3Try 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]"));
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745394941a4625834.html
评论列表(0条)