1

I have an array of "banned domains", and I'm trying to find an easy way to check if a particular email is from one of those domains. Consider the following:

var bannedDoms = ["gmail.com", "hotmail.com", ".le.salesforce.com"]
if(bannedDoms.indexOf(email.split("@")[1]) === -1){
   // It's a good domain!
}

This works fine, except for the last example, as the salesforce emails are from weird domains like emailtosalesforce@t-1l9sefi2sef5wlowk59bvm0uuh78mkdfuhioamfu7vxv8agjjh.o-h7zieac.na17.le.salesforce.com - the common factor being that they all have .le.salesforce.com in the address.

Searching via Array.prototype.indexOf() is quite an elegant solution and I'd like to use something similar if possible - but to also catch parts of strings, rather than a whole string match.

What's the most efficient and simple way to do this in Javascript?

1
  • 1
    You will have iterate through the array and check Commented Sep 5, 2015 at 11:18

4 Answers 4

1

I would go for a regex:

/gmail\.com|hotmail\.com|\le\.salesforce\.com/.test("emailtosalesforce@t-1l9sefi2sef5wlowk59bvm0uuh78mkdfuhioamfu7vxv8agjjh.o-h7zieac.na17.le.salesforce.com")

A fiddle is here

var bannedDoms = ["gmail.com", "hotmail.com", ".le.salesforce.com"];
r=new RegExp(bannedDoms.map(function(x){ return x.replace(/\./g,'\\.') }).join("|"));

Explanation:

You could simply build up a regular expression, taking each banned domain and combine them with or. The simplest form would be a|b, read a or b. In principle gmail.com or hotmail.com would become gmail.com|hotmail.com - with one exception: . is a special character in a regular expression meaning any character. To cirumvent this, we need to escape the dot to \..

r=new RegExp(bannedDoms.map(function(x){ return x.replace(/\./g,'\\.') }).join("|"));

Array.prototype.map() is a function, which applies a function onto each element of an array - returning the resulting array.

The map-part does nothing more than replacing every occuring . with an escaped version \..

Array.prototype.join() joins the resulting array-elements with a pipe = or.

Thanks @torazaburo

Sign up to request clarification or add additional context in comments.

2 Comments

I was impeded by external factors - so to say ;)
Shouldn't it be replace(".", "\\.")? Otherwise JS will eat the backslash when parsing the string.
0

I think you will have to iterate and test the domains like

var bannedDoms = ["gmail.com", "hotmail.com", ".le.salesforce.com"];

function isBanned(email) {
  var domain = email.split("@")[1];

  var banned = bannedDoms.some(function(value) {
    return domain.indexOf(value) > -1;
  })
  if (!banned) {
    // It's a good domain!
  }

  snippet.log(email + ' : ' + banned);
}

isBanned('[email protected]');
isBanned('[email protected]');
isBanned('[email protected]');
isBanned('[email protected]');
isBanned('[email protected]');
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Comments

0

Probably a good candidate for the endsWith simulation:

var bannedDoms = ["gmail.com", "hotmail.com", ".le.salesforce.com"];
var email1 = "[email protected]";
var email2 = "[email protected]";

console.log(bannedDoms.some(function (e) {
    return email1.indexOf(e, email1.length - e.length) !== -1;
})); // true

console.log(bannedDoms.some(function (e) {
    return email2.indexOf(e, email2.length - e.length) !== -1;
})); // false

Comments

0

I think you'd have to do a string indexOf, if you want to catch those. So, You can try something like this..

var bannedDoms = ["gmail.com", "hotmail.com", ".le.salesforce.com"]
for(var i=0; i<bannedDoms.length; i++) {
    if(email.indexOf(bannedDoms[i]) === -1){
         // It's a good domain!
    }
}

1 Comment

Use for instead of for ...in

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.