Powered by Blogger.

Saturday, March 1, 2014

Set maximum length for multiline textbox in Asp.Net:

In this post we will discuss how to set maximum length for multiline textbox in Asp.net using JavaScript function. You can do this also by using regular expression validator, check out this article.

Also you can check out some post on:

- Implement delete functionality in Gridview in Asp.Net

- Query to get records between two dates in sql server 2008

- Encrypt and Decrypt a file in C#.Net

To do this first we need to write the JavaScript function like this:

 <script type="text/javascript">      
        function CheckCharacters(textBox, maxLength) {          
            if (textBox.value.length > maxLength) {          
                alert("Maximum  " + maxLength + " characters allowed.");              
                textBox.value = textBox.value.substr(0, maxLength);              

Then we need to call the JavaScript function in onkeyup and onchange event like below:

<asp:textbox id="txtDescription" runat="server" textmode="MultiLine" onkeyup="javascript:CheckCharacters(this, 20);" onchange="javascript:CheckCharacters(this, 20);"></asp:textbox>

So when user try to enter characters, and when character count greater than 20 then it will show the alert message like below: