I am trying to learn if there is a javascript method to search a string to see if that string contains any of the values in an array of strings.
An example would be:
var a = 'How now brown cow';
var b = new Array('red', 'green', 'brown');
The resulting function would return true because the word brown is contained within the string a .
More specifically what I am trying to do (except using values from form input) is:
var a = '[email protected]';
var b = new Array('.com', '.net', '.org');
This should also return true. Then based on this I will go on to accept var a as valid.
My actual code as of right now (which always returns null) is as follows:
function check_domain(){
for(var i=0; i<domains.length; i++){
var d = domains[i];
if(elemen.value.indexOf(d) != d){
return null;
}
else{
vEmail.style.visibility = 'visible';
}
}
}
window.domains = new Array(....Array values here....);
[...]syntax is preferred over thenew Array(...)syntax unless you want to create a new array with a certain amount of undefined elements (in which case you would usenew Array(n)fornelements). Besides that, your code leaks globals, for example, you should really usevar iin thecheck_domain()function - you really do not want any loop variable to become global!var iissue, but the Array is not empty, i just left it empty and wrote what I did because it has over 100 strings in it that took up a lot of space irrelevant to the question.addEventListenermethod to call all functions. Any time a user clicks on the page all information about that element is stored aselemen; when they click something else the value ofelemenautomatically changes to the new element clicked. Should I just declare the variables as global variables and use them like that? I'm getting confused cause people keep telling me not to use global variables but I don't know how the heck to avoid it and still acomplish the purposes of my code.varat the top of scope. If you're interested in improving your JavaScript then take a look at closures and self invoking functions to control scope.