How to validate domains dynamically in javascript?
I have requirement where I need to validate domains (dynamically coming from other input) if it is matching the domain which we are getting from the company name selected then say valid. If it's not matching the domain should say like invalid domain.
For ex: if the
- company name is XYZ allowed domain is : @xyz.com
- company name is abc allowed domain is : @abc.com
so likewise when we change the company name the domain will change according to company selected.
How can we validate these domains dynamically ?
Below have code which will validate when we have one domain. Can someone help to get the domain validation dynamically?
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(re.test(email)){
if(email.indexOf("@gmail.com", email.length - "@gmail.com".length) !== -1){
console.log("VALID");
}
else {
console.log('INVALID');
}
}
}
validateEmail('zk@gmail.com'); //VALID
validateEmail('zk@mail.com'); //INVALID