Reputation: 8970
I have a page where admins can set up words that are blacklisted and not allowed to be used in email addresses. On the signup page, I have an array populated with all of those words. This page is internal and is for requesting multiple email addresses to be created so for each of the requested email addresses, I need to see if the word exists in the array.
I can do this with the typical jQuery inArray
, however I need to figure out how to implement partial matches as well.
-- Example
var blackList = Array('admin', 'webform', 'spam');
... Loop over the requested email addresses and put them into an array
var requestedEmails = Array('[email protected]', '[email protected]', '[email protected]');
// Check for the exact match in the arrays
$(requestedEmails).each(function(){
if(jQuery.inArray(this, blackList) != -1) {
// Word is in the array
} else {
// Word is not in the array
}
});
In the example above, I would need it to catch webform1
and admins
as both of those words exist in the blacklist in some way.
What is the best way to add in the partial matches from here?
Upvotes: 0
Views: 1936
Reputation: 4292
there is no need using jQuery. Just plain old vanilla ES2015
check if any of the given emails in array contains blacklisted word:
var containsBlacklisted = requestedEmails
.some(email => blackList
.some(word => ~email.search(RegExp(word))));
filtering only emails not containing blacklisted word would be like this:
var approvedEmails = requestedEmails
.filter(email => !blackList
.some(word => ~email.search(RegExp(word))));
Upvotes: 2