I am working on some client side validation for the sort contact form, the website is currently not connected, so the server side is not related.
I am trying to create a โword filterโ to catch any abuse of obscene language before the form is โsubmittedโ.
Here is the code, without obscenity ...
function filterInput(str) { var inputFilter = ['word1', 'word2', 'word3']; var arrayLength = inputFilter.length; if (inputFilter.indexOf(str) > - 1) {
If the user was to enter "word1", he would catch the word. If the user enters the word "word1word2" or "John is word3", he will not catch it.
I originally had a for loop that worked better, but still wouldn't work without spaces between words ("word1word2").
Any input is welcome, I searched, but didnโt meet my needs.
EDIT: So, I came up with a solution too, but, seeing the various ways that this can be achieved, I am curious how this works and also why is the best way better?
Here is what I came up with ...
function filterInput(str) { var inputFilter = ['word1', 'word2', 'word3']; var arrayLength = inputFilter.length; for (var i = 0; i < arrayLength; i++) { if (str.includes(inputFilter[i])) { window.alert('Message...'); return; } } }