Demonstrates the UnwantedWordsValidator
The UnwantedWordsValidator compares the contents of a textbox to a list of strings.
If it matches one of the strings, the condition fails.
This control can retrieve a list of strings from a data source such as a DataSet,
DataTable or a System.Collections.IEnumerable collection, in the same way you populate
a ListBox or DropList.
It has rules to match “whole words”, case insensitive, and “broken words”. A broken
word is a word that is separated by non-letter characters in an attempt to avoid
detection. For example, if the word “flower” is unwanted, an error will be reported
on “f*l*o*w*e*r”.
Specify the control to evaluate with the ControlIDToEvaluate property.
The Items property holds each unwanted word in a
PeterBlum.DES.Web.WebControls.UnwantedWordsItem
object. Assign its Value property to the unwanted word.
Alternatively, the Items property can be populated from a datasource. The
data comes from three properties that are familiar if you have used databinding
on ListBoxes or DataGrids: DataSource, DataMember, and DataTextField.
However, you don’t need to call the DataBind() method unless you use databinding
notation in the ASP.NET markup.
The evaluation rules are in these properties:
- WholeWord – When true, all unwanted words must match to whole word. That
means their first letter is not preceded by another letter and their last letter
is not followed by another letter. When false, the unwanted work can be found inside
another word. It defaults to false.
- BrokenWords – Look for a word whose characters are broken up by non-letter
characters. For example, the unwanted word "darn" can be written "d&a!r-n" and it
will be detected. Spaces are valid separators. So the word "darn" will be found
in "radar noise" unless you set WholeWord to true. As a result, WholeWord
mode is strongly recommended. When true, the feature is enabled. When false, it
is disabled. It defaults to false.
- CaseInsensitive – When true, the regular expression parser will perform
a case insensitive match. When false, it will perform a case sensitive match. It
defaults to true.
The ErrorMessage and SummaryErrorMessage properties support the token
“{UNWANTED}” to state the offending word.
Controls
Basic setup
The following words will report errors: snake, ant, bird, mole.
Don't enter .
Various properties
Shows BrokenWords=true, WholeWord=true, and databinding to get the words.
The following words will report errors: snake, ant, bird, mole.
Don't enter .
Source Code (C#)
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
UnwantedWordsValidator2.DataSource = CreateDataSource();
UnwantedWordsValidator2.DataBind();
}
}
protected ICollection CreateDataSource()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Item", typeof(string)));
string[] vData = { "snake", "ant", "bird", "mole" };
for (int i = 0; i < vData.Length; i++)
{
DataRow dr = dt.NewRow();
dr[0] = vData[i];
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (PeterBlum.DES.Globals.WebFormDirector.IsValid)
{
}
}
</script>
<h2>Basic setup</h2>
The following words will report errors: snake, ant, bird, mole.<br/><br/>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<des:UnwantedWordsValidator ID="UnwantedWordsValidator1" runat="server"
ErrorMessage="Don't enter {UNWANTED}."
ControlIDToEvaluate="TextBox1" >
<Items>
<des:UnwantedWordsItem Value="snake" />
<des:UnwantedWordsItem Value="ant" />
<des:UnwantedWordsItem Value="bird" />
<des:UnwantedWordsItem Value="mole" />
</Items>
</des:UnwantedWordsValidator>
<br/>
<br/>
<h2>Various properties</h2>
Shows BrokenWords=true, WholeWord=true, and databinding to get the words.<br/>
The following words will report errors: snake, ant, bird, mole.<br/><br/>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<des:UnwantedWordsValidator ID="UnwantedWordsValidator2" runat="server"
ErrorMessage="Don't enter {UNWANTED}."
ControlIDToEvaluate="TextBox2" BrokenWords="true" WholeWord="true"
DataTextField="Item" >
</des:UnwantedWordsValidator>
<br/>
<br/>
<des:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"></des:Button>