1

Web サイトから受信するスパムの連絡先メールを取り除こうとしています。

1 行に 1 つずつ禁止語のリストがあります。ここで、禁止された単語のいずれかがフォーム入力に存在し、フォームの送信を妨げる場合にアラートを表示できるものを実装したいと考えています。どうやってやるの?

これが私が試したコードですが、失敗しました。行ごとに単語またはフレーズを入力データと一致させる方法がわかりません。

HTML

<form method="post" action="/sendmessage.php">
<input type="text" id="name" name="name" />
<input type="text" id="email" name="email" />
<input type="text" id="subject" name="subject" />
<input type="textarea" id="message" name="message" />
<input type="submit" id="submit" name="submit" value="Send Message" />
</form>

JavaScript

<script type="text/javascript">
$(function() {
    $("#submit").on("click",function() {
        var name = $("input:text[name='name']").val();
        var email = $("input:text[name='email']").val();
        var subject = $("input:text[name='subject']").val();
        var message = $("input#message").val();
            var badwords = (abc // first bad word, followed by a carriage return
                            bcd // second bad word, followed by a carriage return
                            cdf // third bad word, followed by a carriage return
                            ...
                            the bad phrase // bad phrase, followed by a carriage return
                            another bad phrase // another bad phrase, followed by a carriage return
                            ...
                            xyz
                            );

        /* I am not sure what to do from now on */ 

                if (name || email || subject || message ) {
                    alert("Your Message Contains Bad Words, Please Remove Them Before Proceeding");
                    return false;
                }
        /* I am not sure what to do */ 
    });
});
</script>

助けてください

4

3 に答える 3

4

彼女は解決策です:

HTML:

<form method="post" action="/sendmessage.php">
<input type="text" id="name" name="name" />
<input type="text" id="email" name="email" />
<input type="text" id="subject" name="subject" />
<input type="textarea" id="message" name="message" />
<input type="button" id="submit" name="submit" value="Send Message" />
</form>

JS:

$(function() {
$("#submit").on("click",function() {
    var name = $("input:text[name='name']").val();
    var email = $("input:text[name='email']").val();
    var subject = $("input:text[name='subject']").val();
    var message = $("input#message").val();
        var badwords = ["abc", "bca", "hai hello"];

    /* do this */ 

        if($.inArray(name, badwords) !==-1 || $.inArray(email, badwords) !==-1 || $.inArray(subject, badwords) !==-1 || $.inArray(message, badwords) !==-1)
            {
                alert("Your Message Contains Bad Words, Please Remove Them Before Proceeding");
                return false;
            }

});
});

JSFiddle:

http://jsfiddle.net/bittu4u4ever/8gF8k/

改行を含む単語を配列に自動的に変換するには:

var words = "abc\nbca\nhai hello";
var badwordsarray = words.split("\n")
于 2013-01-22T06:15:34.110 に答える
0

悪い単語が文字列全体に存在するかどうかをチェックするバージョンはありますか? 例: var badwords = "\nkill"; 入力の使用法: キラーが最初の 3 文字を見つけて、使用法を禁止していますか?

そのような敬具の例があれば、私を真剣に助けてくれるでしょう。

于 2015-01-28T21:21:27.660 に答える
0
  if($.inArray(name, badwords) !==-1 || $.inArray(email, badwords) !==-1 || $.inArray(subject, badwords) !==-1 || $.inArray(message, badwords) !==-1) {
        alert("Your inputs contain Bad Words, Please Remove Them Before Proceeding");
                    return false;
      } 
于 2013-01-22T06:15:19.633 に答える