被禁止的单词,但如果有更多的单词则不会 [英] Banned words but not if with more words
问题描述
我找到了解决方案,禁止在输入字段中输入某些单词. 问题是我仅在有人将其用作单个单词时才需要禁止它们,而在输入更多单词时则不需要.
I find solution, to ban some words if they are entered in input field. The problem is that i need ban them only when somebody use them as single word, not when are entered with more words.
示例:
如果输入了%bannedword%,则必须显示ERROR,但是如果有人写
If %bannedword% was entered then ERROR must show up, but if somebody write
%bannedword%wordnotbanned"
" %bannedword% wordnotbanned "
所有标记都必须标记为正确的输入填充,没有任何错误.
all must be marked as correct input fill, without any ERROR.
一切正常后我必须做什么?
What i must do to everything works fine?
这是jQuery
$('#submit').click(function() {
var bannedWords = ["black", "white"],
regex = new RegExp('\\b' + bannedWords.join("\\b|\\b") + '\\b', 'i');
var zalvalid = !regex.test($('#zalmie').val().toLowerCase());
if(!zalvalid) {
$('#zwynik').addClass("hover").html('<strong>ERROR:</strong>');
$('#zalmie').focus();
return false;
} else {
$('#zwynik').addClass("hover").html('<strong>NICE ONE!</strong>');
}
});
和这是小提琴:: http://jsfiddle.net/p16954wc/
推荐答案
我更改了bannedwords数组,该数组将用于regExp. 您还必须包括用于处理witespaces的方案.因此,我在bannedWords数组中添加了提示.
I have changed the bannedwords array, that will be used for regExp. You must also include the scenario to handle witespaces. So I have added thoes in bannedWords array.
$('#submit').click(function() {
var bannedWords = ["black ", "white ", "black", "white"],
regex = new RegExp('\\b' + bannedWords.join("\\b|\\b") + '\\b', 'i');
var zalvalid = !regex.test($('#zalmie').val().toLowerCase());
if(!zalvalid) {
$('#zwynik').addClass("hover").html('<strong>ERROR:</strong>');
$('#zalmie').focus();
return false;
} else {
$('#zwynik').addClass("hover").html('<strong>NICE ONE!</strong>');
}
});
这是到小提琴的链接!
以前,您无法处理以下情况: 例子:黑色的东西. 因此,您必须具有regExp,它必须能够验证"black"和"white".
Earlier, you were not able to handle the scenario where: Example : black something. So, You must have regExp which must be able to validate "black " and "white " .
如果禁止的单词列表很长,则只需在每个单词之后添加额外的空格.
If your list of banned word is long , then just add extra space after each word.
您还可以自动创建banWord数组.
You can also automate you banWord array creation.
这篇关于被禁止的单词,但如果有更多的单词则不会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!