Question: I am trying to validate email endings in an array
let input = 'test@gmail.com' // This is grabbed dynamically but for sake of explanation this works the same
let validEndings = ['@gmail.com', '@mail.com', '@aol.com'] //and so on
if(input.endsWith(validEndings)){
console.log('valid')
}else{
console.log('invalid')
}
I can get this to work when validEndings is just a singular string e.g let validEndings = '@gmail.com'
but not when its in an array comparing multiple things
Advertisement
Answer
You can solve the problem with regex. Example:
const input = 'test@gmail.com';
const validEndingsRegex = /@gmail.com$|@mail.com$|@aol.com$/g;
const found = input.match(validEndingsRegex);
if (found !== null) {
console.log('valid')
} else {
console.log('invalid')
}