Compare array of words to a textarea input with javascript

后端 未结 5 1263
自闭症患者
自闭症患者 2021-01-16 00:24

I have a textarea inside a form.

Before the form is submitted, the textarea is validated and checked so it is not empty, not over 2000 characters, not contain forbid

5条回答
  •  被撕碎了的回忆
    2021-01-16 01:25

    If you wanted to check for the presence of "expletive1" and "expletive2" you'd do the following:

    my_textarea = document.getElementById('textarea_id');
    
    if (/\b(?=\w)(expletive1|expletive2)\b(?!\w)/i.test(my_textarea.value)) {
        // we found bad words!  do something
    } else {
        // no bad words found, carry on, nothing to see here
    }
    

    And you'd just add more words to the list in the same manner (expletive1|expletive2|expletive3|expletive4)

    Keep in mind that to keep the words out of your app entirely you'll also need to do server-side filtering.

提交回复
热议问题