Forum Moderators: coopster

Message Too Old, No Replies

Form bad words filtering with value check, alert and return false

A basic bad words filter that asks for user help to keep things clean

         

JS_Harris

11:42 pm on Mar 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm working on a form that has a TITLE and DESCRIPTION field combined with a SUBMIT button. Instead of attempting to replace or modify "bad words" I'd like to try the approach of displaying an alert and disabling the submit button if either TITLE or DESCRIPTION is empty or if DESCRIPTION contains a bad word.

I realize this will not stop hackers or determined spammers but I'd like to implement it as part of my efforts to keep a site clean. Sometimes asking people to self moderate actually works. The benefit is that if they self moderate when asked to do so the database remains clean and resources are saved.

This is my best working effort thus far...


badwords = new Array("bad", "words", "here", "etc");
function Verify(aaa)
{
tvalue = aaa.title.value; // entered words
// check if title field is empty
if (tvalue == "")
{
alert("Please include an accurate title");
aaa.title.focus();
return false;
}
fvalue = aaa.description.value; // entered words
// check if description field is empty
if (fvalue == "")
{
alert("Please include an accurate description of your upload");
aaa.description.focus();
return false;
}

// title and description are not empty so check for badwords in description
else
{
wordsArray = fvalue.split(" ");
for (i = 0; i < badwords.length; i++)
{
for ( j=0; j < wordsArray.length; j++)
{
if (wordsArray[j] == badwords[i])
{
alert("Please keep descriptions clean, self moderation means no forced moderation!");
aaa.description.focus();
return false;
}
}
}
} // end check for badwords
}

The form uses onsubmit="return Verify(this)" to run the function.

It's unfortunately case sensitive and I'd like to ask your help in making it case insensitive (or to make it more efficient). Any suggestions welcome.

d40sithui

2:19 pm on Mar 11, 2009 (gmt 0)

10+ Year Member



Hey. You might get a better response if you put this in the JS forum. However, I think the syntax is almost the same as PHP.
To make it case insensitive, you can convert all the values to lower case or upper case - depending on how you generate your "bad words" array and then comparing them. So you can do something like
if (wordsArray[j].toLowerCase() == badwords[i].toLowerCase()){}