Ask questions via twitter! Message any question to @answers on twitter. We'll publish the question and send you a reply each time there's a new answer.
Next Question

Answered Question

 
January 14, 2009 03:13 AM

Implement a Bad Word checker on ASP.NET form for all text fields upon form submit.

I need to implement a bad word checker that will check each control of type TextBox on a form and compare its text to a bad word list using a RegEx. I have created a BadWord class that returns a bool on a match to the regex. I have currently implemented a CustomFieldValidator on the page that iterates over the controls on the page and calls a the BadWord method check on each text box. But this does not seem to be working. Is there a smarter route to take?
Interesting Question?  Yes (0)   No (0)   
RSS
 
 

Best Answer  Chosen by Asker

 
January 14, 2009 09:33 AM
Make sure that the validator is set to enable client script = false . For some reason the only way I can get server side validators to trigger is by making sure all of them have that property set to false.

Is the function itself tested? If you arbitrarily feed it text, does it do what you want? Is the problem that you can't get the page to stop processing because the validator got triggered? Do you have set control to validate?
Asker's Rating:


Helpful Answer?  (0)   (0)    Tip pvera for this answer
Permalink | Report
   Reply  
 
 
 
January 14, 2009 01:20 PM
The function is unit tested and works fine. The issue seems to be in the Custom Field Validator - I keep getting the error "the property ControlToValidate cannot be set to '' ". Here is the code for the CFV:
protected void cfv_Reg_BadWordChecker_ServerValidate(object source, ServerValidateEventArgs args)
{
BadWordFilter badWordFilter = new BadWordFilter();
Control[] allControls = FlattenHierachy(Page);
foreach (Control c in allControls)
{
if (c is TextBox)
{
TextBox txtBox = c as TextBox;
if (badWordFilter.CheckForIllegalWords(txtBox.Text))
{
txtBox.ForeColor = System.Drawing.Color.Red;
this.CustomValidationSummary.AddMessage("One or more fields has an illegal word. The field has been highlighted in red.");
args.IsValid = false;
//break;
}

}

}
}

public static Control[] FlattenHierachy(Control root)
{
List<Control> list = new List<Control>();
list.Add(root);
if (root.HasControls())
{
foreach (Control control in root.Controls)
{
list.AddRange(FlattenHierachy(control));
}
}
return list.ToArray();
}

Report
 
 
 
January 14, 2009 03:13 PM
OK, I know why you are getting that error. Custom validators must point to ONE control as the control to validate. It doesn't matter if the validator itself loops thru 500 other controls, at run time it has to point to something. Set it to the first text box or something else, and the error goes away :-)

Report
 
 

Other Answers (3)

Sort By
 
January 14, 2009 04:58 AM
Are you trying to reject anything with bad words, or change them to something like **** ?

If you want to replace, something like this will work:

protected string replaceBadWords(string testThis, string[] badWords)
{
foreach (string s in badWords)
{
string replaceWith = string.Empty;
for (int i = 0; i < s.Length; i++) replaceWith += "*";
testThis = testThis.Replace(s, replaceWith);
}
return testThis;
}

That takes in a string to test, and a string array of bad words and returns the 'cleansed' string.

This one will return a true if it contains a bad word:


protected bool containsBadWords(string testThis, string[] badWords)
{
bool output = false;
foreach (string s in badWords) if (testThis.Contains(s)) output = true;
return output;
}

but I don't recommend this method as it can be very ambiguous if you do not want to show the word. What if the user inputs assanine or something like that?

Helpful Answer?  (0)   (0)    Tip naspinski for this answer
Permalink | Report
   Reply  
 
 
 
January 14, 2009 02:21 PM
Have you set the ControlToValidate property so that it references the TextBox you're trying to validate?

As far as a smarter route, this seems like a good approach, this way you can validate without the extra overhead of a postback.

Helpful Answer?  (0)   (0)    Tip darth continent for this answer
Permalink | Report
   Reply  
 
 
 
January 14, 2009 07:08 PM
Don't forget to include the English town name "Sc--thorpe" in your tests.

I'd also recommend reading Jeff Atwood's article on the subject of obscenity filters, linked below.

EDIT: looks like Mahalo has the same problem with Scu nthorpe :D
Source(s):
http://www.codinghorror.com/blog/archives/001176.html


Helpful Answer?  (0)   (0)    Tip nickfitz for this answer
Permalink | Report
   Reply  
 
 

Answer this Question

How tips and payments work

This question has already been resolved. You may add an answer to it but you will not be eligible to win best answer or any associated tips.

Ask a Question


140 characters left
Top of Page
Buy Mahalo Dollars with Credit Card or PayPal

Top Members

This Week All Time
  • buddawiggi
    buddawiggi
    2nd Degree Black Belt
    27543 Points
    M$789.91 Earned
  • opher
    opher
    Purple Belt
    4443 Points
    M$196.22 Earned
  • annelisle
    annelisle
    Purple Belt
    2997 Points
    M$91.22 Earned
   See All
 

Most Popular Tags

mahalo(1618)
iphone(465)
music(461)
google(357)
food(321)
online(295)
beer(279)
money(262)
movies(258)
apple(251)
aotd(235)
health(220)
video(207)
free(205)
dog(205)
   See All
 

Categories

Welcome New Members


 
 
Mahalo Dollars are the currency of Mahalo Answers.

Each Mahalo Dollar costs $1.

Once you earn more than 40 Mahalo Dollars, you can request to be paid via PayPal. Each Mahalo Dollar is currently worth $0.75 when paid out via PayPal. Learn More

 
 

Please log in to use this function.