drewmace's Avatar
drewmace 4
1 Asked
6 Answered
1 Best
0
No one has voted on this question yet :(
3 years, 4 months ago

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?
Tip for best answer: M$0.00
Separate topics with commas, or by pressing return. Use the delete or backspace key to edit or remove existing topics.

You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.

M$

What is Your Answer?

0
0
0

4 Answers

0
pvera's Avatar
pvera | 3 years, 4 months ago
4
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?

You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.

M$
drewmace's Avatar
drewmace | 3 years, 4 months ago Report

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();
}

pvera's Avatar
pvera | 3 years, 4 months ago Report

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 Abuse

Post Reply Cancel
0
nickfitz's Avatar
nickfitz | 3 years, 4 months ago
4
Don't forget to include the English town name "Scunthorpe" 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

You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.

M$

Report Abuse

Post Reply Cancel
0
darth continent's Avatar
darth continent | 3 years, 4 months ago
4
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.

You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.

M$

Report Abuse

Post Reply Cancel
0
naspinski's Avatar
naspinski | 3 years, 4 months ago
4
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?

You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.

M$

Report Abuse

Post Reply Cancel

Learn something new with our FREE educational apps!

Private lessons in the comfort of your own home. Get back in shape or finally pick up a guitar with our great experts guiding you the whole way!
Learn Guitar
Learn Hip Hop
Learn Pilates