Compare array of words to a textarea input with javascript

后端 未结 5 1257
自闭症患者
自闭症患者 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:20

    var bad_words = new Array('word1', 'word2');
    var user_words = document.getElementById('textarea').split(/\W+/);
    
    for( var i in bad_words)
    {
      if( user_words.indexOf( bad_words[i] ) != -1 )
      {
        alert( 'The textarea has bad word!');
        break;
      }
    }
    

提交回复
热议问题