Next Question
RSS
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?
Permalink | Report
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?
Permalink | Report
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
Permalink | Report
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)
- In Web Development |
- |
- Report |
-
Share
RSS
Best Answer Chosen by Asker
| January 14, 2009 09:33 AM |
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: |
Permalink | Report
Other Answers (3)
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?
Permalink | Report
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
Permalink | Report
Answer this Question
Related Questions
Ask a Question
Buy Mahalo Dollars with Credit Card or PayPal
Top Members
Most Popular Tags
Categories
- Anonymous
- Arts & Design
- Beauty & Style
- Books & Authors
- Business
- Cars & Transportation
- Consumer Electronics
- Coupons Deals
- Education
- Entertainment
- Environment
- Fitness
- Food & Drink
- From Email
- From Iphone
- From Twitter
- Health
- History
- Hobbies
- Home & Garden
- How Tos
- Humor
- Jobs
- Legal
- Local
- Love & Relationships
- Mahalo Answers Community
- Money
- Music
- News
- NSFW
- Parenting
- Pets
- Science & Mathematics
- Services
- Shopping
- Social Science
- Society & Culture
- Sports
- Technology & Internet
- Travel
- Video Games
Welcome New Members
- tammiet06, November 29, 2009 08:11 AM
- judycritchley, November 29, 2009 08:02 AM
- leebutler, November 29, 2009 07:11 AM
- maritaalvaran, November 29, 2009 07:10 AM
- sumaanand, November 29, 2009 06:48 AM
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
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();
}