Powered by Blogger.

Sunday, February 16, 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:

- Add Primary Key Constraint to existing table in sql server 2008

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

- Transforms in WPF and their types

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);              
            }    
        }      
</script>

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:





0 comments

Post a Comment