Javascript清除脏话的注释

Javascript清除脏话的注释,javascript,comments,Javascript,Comments,我正在创建一个评论板,并试图通过Javascript实现一段脚本,如果段落中有不需要的单词,该脚本将阻止用户提交该段落。我在网上查过,很难找到任何例子。这是我到目前为止所拥有的,但不确定是否应该使用index.of index.php <div class="askComment"> <h2>Submit new comment</h2> <!--comment form --> <form id="form" met

我正在创建一个评论板,并试图通过Javascript实现一段脚本,如果段落中有不需要的单词,该脚本将阻止用户提交该段落。我在网上查过,很难找到任何例子。这是我到目前为止所拥有的,但不确定是否应该使用index.of

index.php

<div class="askComment">
    <h2>Submit new comment</h2>
    <!--comment form -->
    <form id="form" method="post">
        <!-- need to supply post id with hidden fild -->
        <input type="hidden" name="postid" value="<?php echo $post; ?>">
        <input type="hidden" name="type" value="A">
            <p>Hello <strong><?php echo $fname; ?></strong> what do you have to say</p>
            <input type="hidden" name="fname" id="comment-name" value="<?php echo $fname; ?>" >
            <input type="hidden" name="userid" id="comment-mail" value="<?php echo $UserId; ?>" >
            <p>Your comment *</p>
            <textarea name="comment" id="comment" cols="30" rows="10" placeholder="Type your comment here...." ></textarea>
            <div id="error"></div>
        <input type="submit" id="submit-comment" name="submit" value="Submit Comment">
    </form>
    </div>

提交新评论
看看这个:(在数组中搜索数组)

这可能不是最理想的选择,但它将是代码的开始

编辑:一个更好的选择(可能不是最好的)可能是将注释字符串按单词分隔成一个数组,并在两个数组之间进行交叉,这里有一个问题解释如何在js中进行数组之间的交叉,一个可能的解决方案(类似于您的,相同的逻辑,只是一些更改/添加)

$("#submit-comment").attr('disabled', true);

var swear = new Array();
swear[0] = "jelly";
swear[1] = "trumpet";
swear[2] = "chocolate";

$("#comment").on("keyup", function () {

  var comment = $("#comment").val();

  word = comment.split(' ');

  for (i = 0; i < word.length; i++) {

    worda = word[i].trim();

    worda = worda.replace(/\.|,|!|:| |;|\?|\r?\n/g, ''); // bad word + one of chars = bad word

    console.log(worda);

    if ($.inArray(worda, swear) != -1) {
      $("#error").html('<font color="red">Please rewrite <strong>bad</strong> word found.</font>');
      $("#submit-comment").attr('disabled', true);
      break;
    } else {
      $("#error").html('');
      $("#submit-comment").attr('disabled', false);
    }
  }

});
$(“#提交评论”).attr('disabled',true);
var=newarray();
发誓[0]=“果冻”;
发誓[1]=“小号”;
发誓[2]=“巧克力”;
$(“注释”)。关于(“键控”,函数(){
var comment=$(“#comment”).val();
word=注释。拆分(“”);
for(i=0;i找到的单词错误');
$(“#提交评论”).attr('disabled',true);
打破
}否则{
$(“#错误”).html(“”);
$(“#提交评论”).attr('disabled',false);
}
}
});

我宁愿使用“keyup”事件,这样用户在键入时会收到错误消息。但是-如前所述,这很容易被覆盖,服务器端检查是必须的。

您可以将
comment.indexOf(“??”)====-1
替换为
(new RegExp(sware.join(“|”)))。test(comment)
。您应该在服务器上执行此操作,因为用户可以覆盖客户端检查。非常感谢您的帮助,非常感谢
$("#submit-comment").attr('disabled', true);

var swear = new Array();
swear[0] = "jelly";
swear[1] = "trumpet";
swear[2] = "chocolate";

$("#comment").on("keyup", function () {

  var comment = $("#comment").val();

  word = comment.split(' ');

  for (i = 0; i < word.length; i++) {

    worda = word[i].trim();

    worda = worda.replace(/\.|,|!|:| |;|\?|\r?\n/g, ''); // bad word + one of chars = bad word

    console.log(worda);

    if ($.inArray(worda, swear) != -1) {
      $("#error").html('<font color="red">Please rewrite <strong>bad</strong> word found.</font>');
      $("#submit-comment").attr('disabled', true);
      break;
    } else {
      $("#error").html('');
      $("#submit-comment").attr('disabled', false);
    }
  }

});