Implement a Bad Word checker on ASP.NET form for all text fields upon form submit.
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$4 Answers
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$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$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$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$
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();
}
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 :-)