如果表单输入中存在任何错误/过滤的Word,则显示警报
我试图摆脱我通过我的网站收到的垃圾邮件联系邮件。
我有一个禁止的单词列表,每行格式一个。 现在我想实现一些可以显示警告的内容,如果表单输入中存在任何被禁止的单词并阻止表单提交。 我怎样才能做到这一点?
这是我尝试过的代码,但失败了。 我不知道如何将每行单词或短语与输入数据匹配。
HTML
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 */ }); });
请帮忙
她是解决方案:
HTML:
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")
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; }
是否有一个版本可以检查整个字符串中是否存在坏词? 例如:var badwords =“\ nkill”; 输入用法:杀手找到前三个字符然后禁止使用?
如果你有这样亲切问候的例子,会认真帮助我