How to validate length of multiline textbox

I have to control the length of the message to be sent But for textbox of Multiline type did not allowing the MaxLength property So plz do favour for me regarding this one

Multiline TextBox is rendered as <TextArea>.</TextArea> which does not have MaxLength Attrubite, However you can use RegularExpressionValidator to validate length, Following sample code shows how you can use RegularExpressionValidato to validate length of MultiLine TextBox.

Run sample code

<%@ Page Language="C#"%>
<html>
<head runat="server">
    <title>Multiline Textbox max length</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <p>Text should be less then or equal to 25 chars</p>
        <asp:TextBox runat="server" ID="MLTextBox" 
            TextMode="MultiLine" >
        </asp:TextBox>
        <asp:RegularExpressionValidator 
                runat="server" Display="dynamic" 
                ControlToValidate="MLTextBox" 
                ValidationExpression="^([\S\s]{0,25})$" 
                ErrorMessage="<p>only 25 chars please</p>">
        </asp:RegularExpressionValidator>
        <p />
        <asp:Button runat="server" text="Submit"/>
    </div>
    </form>
</body>
</html>
Bookmark with:
Technorati   Digg   Delicious   StumbleUpon   Facebook
My name is Jigar Desai I share my ideas on this site and you can contact me by filling contact form.

Categories