I’m working on importing a word filter into a text input and I want to check if there is a match between a entire bad word that is in a array of words and a word that is inside a string, I will give an example of this…
List of bad words:
JavaScript
x
2
1
var bad_words = ['bad1', 'bad2', 'bad3'];
2
String with the value of text input:
JavaScript
1
2
1
var check_text = document.getElementById("text").value;
2
Lets say I have this word in my string (In this example I expect a false answer):
JavaScript
1
2
1
Here is bad2222
2
Lets say I have this word in my string (In this example I expect a true answer):
JavaScript
1
2
1
Here is bad2
2
Advertisement
Answer
After a slight correction to the code,
This is the code that solved this problem for me..Thx to ritaj
JavaScript
1
26
26
1
<script type="text/javascript">
2
3
function check_val() {
4
var bad_words = ['bad1', 'bad2', 'bad3'];
5
var check_text = document.getElementById("text").value;
6
var error = 0;
7
8
if (check_text.split(' ').some(part => bad_words.includes(part))) {
9
error = error + 1;
10
}
11
12
if (error > 0) {
13
document.getElementById("bad_notice").innerHTML = "WARNING: Some Bad Words In Your Text";
14
}
15
else {
16
document.getElementById("bad_notice").innerHTML = "";
17
}
18
}
19
20
</script>
21
22
<div id="wrapper">
23
<textarea id="text" onKeyUp="check_val()" placeholder="Write Some Text Having Words 'bad1', 'bad2', 'bad3'"></textarea>
24
<p id="bad_notice"></p>
25
</div>
26