gpt4 book ai didi

javascript - 如果表单输入中存在任何错误/过滤的单词,则显示警报

转载 作者:行者123 更新时间:2023-11-30 10:35:07 26 4
gpt4 key购买 nike

我正试图摆脱通过我的网站收到的垃圾邮件联系邮件。

我有一个禁用词列表,每行一个。现在我想实现一些可以在表单输入中出现任何禁用词并阻止表单提交时显示警报的东西。我怎样才能做到这一点?

这是我试过的代码,但失败了。我不知道如何将每行一个单词或短语与输入数据匹配。

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>

请帮忙

最佳答案

她的解决方案是:

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")

关于javascript - 如果表单输入中存在任何错误/过滤的单词,则显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14452524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com