Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
61 lines (50 sloc) 1.93 KB
author comments date layout slug title desc wordpress_id categories tags
Joe Kampschmidt
true
2011-11-10 21:46:46 +0000
post
create-customvalidator-to-enforce-maxlength-on-a-multiline-textbox
Create CustomValidator to enforce MaxLength on a MultiLine TextBox
Example ASP.NET code for creating a custom validator to enforce a maximum length on a multiline textbox
23
Code
.NET
C#
UI
Validation

ASP .NET TextBoxes with mode = MultiLine do not support the MaxLength property. MultiLine TextBoxes are rendered as html textareas and this property is not supported in many browsers. (I believe HTML5 will be supporting it)

Solution

Create a custom validator and a provide your own ServerValidaton method. Set the MaxLength property of your textbox, the ControlToValidate and set the custom validator property to OnServerValidate="ValidateMaxLength"

HTML

<asp:TextBox ID="uxTextBox" runat="server"
                            TextMode="MultiLine"
                            MaxLength="2" />

<asp:CustomValidator ID="uxValidator"
                        runat="server"
                        ControlToValidate="uxTextBox"
                        OnServerValidate="ValidateMaxLength"
                        ErrorMessage="Too many characters!" />

<asp:Button ID="uxButton" runat="server" Text="Count" />

C#

protected void ValidateMaxLength(object sender, ServerValidateEventArgs e)
{
    e.IsValid = true;

    var validator = (sender as CustomValidator);
    if (validator != null)
    {
        // validator and textbox need to be in the same
        // NamingContainer for FindControl to work
        var textbox = validator.FindControl(validator.ControlToValidate);
        if (textbox != null && textbox is TextBox)
        {
            e.IsValid = (textbox as TextBox).MaxLength >= e.Value.Length;
        }
    }
}