Restrict Bad Words from Input and textarea

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Restrict Bad Words from Input and textarea



I am trying to validate input value and textarea text against Bad words.
Below code is working fine for single Badword and sentence which contains a badword.


badWords - contains the list of bad words
fieldValue contains input/Text area text

var badText = fieldValue.split(" ");
for (i = 0; i < badWords.length; i++)

if(badWords[i] != "")

if(badText.indexOf(badWords[i].trim()) > -1)
return true;



return false;



Now I am facing issue with below format. Please suggest me how to restrict the below bad word Badword: B a d w o r d



EDIT: The question is not how to filter but how to find the cases where the word is spelled with spaces in between.





You could remove all spaces from words and compare non-spaced words.
– Get Off My Lawn
Aug 6 at 16:26





I'm voting to close due to this being very broad. While the question is straight forward, the implementation, I feel, is too broad to be done in a single question. For instance, the word "assistance' could fail a "bad word" test unless you wrote an exclusion into it. But it is a valid word. Hopefully this gives you an idea of why I'm saying it's too broad.
– Taplar
Aug 6 at 16:28






Your entire function could also be written in its current format as textArr.some((x) => badWords.includes(x))
– Akrion
Aug 6 at 16:29


textArr.some((x) => badWords.includes(x))




3 Answers
3



I would remove all spaces from the words and compare each using includes.



So, for each item you would use replace with the regexp / /g to remove all whitespace, then convert all letters to lowercase so the search is case insensitive using toLowerCase() on both strings (the input string and the bad word string).


/ /g


toLowerCase()




function getBadWords(str)
let badwords = ['spider', 'monkey', 'pig']
let words =
badwords.forEach(word =>
str.replace(/ /g, '').toLowerCase()
.includes(word.replace(/ /g, '').toLowerCase()) ? words.push(word) : null)
return words


const tests = [
'I am a spider',
'This looks like a p i g',
'Look at that Mon Key',
'That spider looks like a monkey',
'Silly words',
'Fancy S p i d e r'
]

tests.forEach(sentence =>
let foundWords = getBadWords(sentence)
console.log(foundWords)
console.log('Is bad: ' + (foundWords.length > 0))
)





:) would it find s p i d e r ... since that is the actual question really ...
– Akrion
Aug 6 at 16:33


s p i d e r





Yes it would find s p i d e r and any other variation with spaces.
– Get Off My Lawn
Aug 6 at 16:40


s p i d e r





Yep I just tested it. Awesome. You get my vote :)
– Akrion
Aug 6 at 16:40



You can store all the words you don't like in an array and then check if the value of the textarea includes any one of them.




let badwords = ["badword", "flip", "hobgoblin", "turtleneck"], ele = document.querySelector.bind(document);
btn = ele("button"), ta = ele("textarea");

btn.addEventListener("click", () =>
for(let badword of badwords)
if(ta.value.replace(/s/gm, "").includes(badword)) return alert("found " + badword + "!");

alert("all clean!");
);


<textarea></textarea><button>detect!</button>





@PDSSandeep Changed it. Keep in mind this isn't a total solution because it's going to check everything after removing white space. It means that if whatever word is valid as part of another word it's also going to catch it. The solution to that is far more complex though and probably best for another question.
– zfrisch
Aug 6 at 16:40



If you split the fieldValue by spaces, you won't be easily able to search for bad words. The easiest solution would be to replace split with trim replace(/ /g,'') to replace all spaces, like:


split


trim


replace(/ /g,'')


var badText = fieldValue.replace(/ /g,'');

for (i = 0; i < badWords.length; i++)

if(badWords[i] != "")

if(badText.indexOf(badWords[i]) > -1)
return true;







Not his issue. Please read the post again.
– Akrion
Aug 6 at 16:32





@Akrion why? if the text includes "B a d w o r d" it will still be found, or did I miss something?
– SapuSeven
Aug 6 at 16:34





Test your code with input bad and then with input b a d
– Akrion
Aug 6 at 16:38


bad


b a d





Oops, I confused the trim method. I updated my answer
– SapuSeven
Aug 6 at 16:39






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS