Powered by Blogger.

Sunday, February 16, 2014

Set Maximum length for multiline textbox in asp.net



In this post we will discuss how we can set maximum length for multiline textbox in asp.net using regular expression validator.

Also you can check out my previous posts on:

- Show confirmation message in gridview for delete in Asp.net

- Data access block of Microsoft enterprise library 6.0 using stored procedure

- How to use WSDL.exe in Asp.Net?

To make a textbox as multiline textbox we need to add the property as  TextMode="MultiLine".

In case of Single line textbox, you can set the maximum length by setting a value for the property as MaxLength as MaxLength="10".

For multiline of textbox we can use a regular expression and the regular expression will be:

ValidationExpression="^[\s\S]{0,20}$"

.Aspx Code:
<div>
            Text:
            <asp:TextBox ID="txtMultiline" runat="server" TextMode="MultiLine" Height="69px" Width="302px"></asp:TextBox><br />
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ValidationExpression="^[\s\S]{0,20}$" ErrorMessage="Can not enter more than 20 characters" ForeColor="Red" ControlToValidate="txtMultiline"></asp:RegularExpressionValidator>
        </div>

Once you will set the regular expression, if you put more than 20 characters, it will appear like below:





0 comments

Post a Comment