I feel like i'm coming up against a brick wall.
I've made a profanity filter, however this currently works when one bad word is entered, when more words (bad or otherwise) are entered it no longer filters and submits fine.
I'd like it to not submit if there are bad words present inside the input.
Here's what i've written so far:
var filterArray;
var inputArray;
$("button").on("click",function() {
var input = $("input:text").val().toLowerCase();
var inputArray = input.split(' ');
console.log(inputArray);
//for each item in inputArray
$.each(inputArray, function() {
if($.inArray(input, filterArray) !==-1)
{
console.log('Sorry you should not say ' + input + '!');
return false;
} else {
console.log('passes');
}
});
});
$.ajax({
async: false,
url : "js/vendor/badwords.txt",
dataType: "text",
success : function (data) {
filterArray = data.split(',');
}
});
Any help would be appreciated, thank you!
you need to return true/false from the click handler instead you are just returning from the
each()
callback method.Demo: Fiddle