user4867030
user4867030

Reputation:

how to test text against a bad word list using jquery validation?

I am trying to validate textarea/input against list of words. I created

var badwordlist = new Array("blue", "Blue", "BLUE",
"ASS", "drugs", "aciphex", "nude");
        jQuery.validator.addMethod("badWord", function(value) {
            return !new RegExp(badwordlist.join('|')).test(value);
        }, "Please remove bad words.");

this is working fine. I am having issue if i type greenblue it will still give me error. While i only want to check blue not greenblue. same thing with ass as class or assessment.

Upvotes: 1

Views: 1868

Answers (2)

user4867030
user4867030

Reputation:

This piece of code worked for me,

var blacklist = /\b(word|word|word|word|word|word|word|word|word|word|word|word)\b/; /* many more banned words... */

jQuery.validator.addMethod("badwordcheck", function(value) {
    return !blacklist.test(value.toLowerCase());
}, "Please remove inappropriate language before submitting.");

Upvotes: 2

IMTheNachoMan
IMTheNachoMan

Reputation: 5839

Why not try checking the other way around?

var badwordlist = new Array("blue", "ass", "drugs", "aciphex", "nude");

alert((new RegExp("blue", "i")).test("[" + badwordlist.join("][") + "]"));
alert((new RegExp("greenblue", "i")).test("[" + badwordlist.join("][") + "]"));
alert((new RegExp("ass", "i")).test("[" + badwordlist.join("][") + "]"));
alert((new RegExp("class", "i")).test("[" + badwordlist.join("][") + "]"));

Upvotes: 0

Related Questions