Return to the menu   Select another DES Module

Demonstrates the TextLengthValidator

The TextLengthValidator evaluates the number of characters in a textbox against a minimum and/or maximum. It can optionally evaluate the total characters in two textboxes.

If you want to count the number of words, use the WordCountValidator. If you want an interactive view to the character count, use the TextCounter control.

Specify the control to evaluate with the ControlIDToEvaluate property; if you want to evaluate the combined length of two controls, specify the second in SecondControlIDToEvaluate.

The Minimum and Maximum properties determine the limits when they are not 0. When using the Minimum property, if you do not want errors reported for an empty textbox, set IgnoreBlankText to true.

The ErrorMessage and SummaryErrorMessage properties support these tokens.

  • “{COUNT}” - The number of characters entered.
  • "{COUNT:singular:plural}" – Shows either the word in the “singular” position or “plural” position based on the count. Singular is shown if the count is 1.
  • “{EXCEEDS}” - The number of characters exceeding the minimum or maximum.
  • “{EXCEEDS:singular:plural}” uses the singular word when {EXCEEDS} is 1; otherwise it uses the plural word.
  • “{MINIMUM}” - The value of the Minimum property.
  • “{MAXIMUM}” - The value of the Maximum property.


Controls

Maximum

Maximum=10



Minimum using various tokens

Minimum=5




Source Code (C#)

<script runat="server">
protected void Button1_Click(object sender, EventArgs e) { if (PeterBlum.DES.Globals.WebFormDirector.IsValid) { // save your data here } }
</script> <h2>Maximum</h2> Maximum=10<br/><br/> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <des:TextLengthValidator ID="TextLengthValidator1" runat="server" ControlIDToEvaluate="TextBox1" ErrorMessage="You have exceeded the limit of {MAXIMUM} characters" Maximum="10"> </des:TextLengthValidator> <br/> <br/> <h2>Minimum using various tokens</h2> Minimum=5<br/><br/> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <des:TextLengthValidator ID="TextLengthValidator2" runat="server" ControlIDToEvaluate="TextBox2" ErrorMessage="You have entered {COUNT} which is {EXCEEDS} {EXCEEDS:character:characters} less than {MINIMUM}." Minimum="5"> </des:TextLengthValidator> <br/> <br/> <des:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"></des:Button>

Return to the menu   Select another DES Module