weixin_33736649 2014-08-07 04:06 采纳率: 0%
浏览 35

遍历过滤器中的每个单词

I feel like i'm coming up against a brick wall.

I've made a profanity filter, however this currently works when one bad word is entered, when more words (bad or otherwise) are entered it no longer filters and submits fine.

I'd like it to not submit if there are bad words present inside the input.

Here's what i've written so far:

var filterArray;
var inputArray;

$("button").on("click",function() {
    var input = $("input:text").val().toLowerCase();
    var inputArray = input.split(' ');

    console.log(inputArray);

    //for each item in inputArray
    $.each(inputArray, function() {
        if($.inArray(input, filterArray) !==-1)
        {
            console.log('Sorry you should not say ' + input + '!');
            return false;
        } else {
            console.log('passes');
        }
    });
});


$.ajax({
    async: false,
    url : "js/vendor/badwords.txt",
    dataType: "text",
    success : function (data) {

        filterArray = data.split(',');

    }
});

Any help would be appreciated, thank you!

  • 写回答

2条回答 默认 最新

  • weixin_33724059 2014-08-07 04:09
    关注

    Replace if($.inArray(input, filterArray) !==-1) with if($.inArray($(this), filterArray) !==-1). This should check every word in the array. Currently, it only looks at the first work multiple times.

    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格