AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 How to Limit the Number of Characters in Asp.Net TextArea with JavaScript?
Posted by Moderator1 on  9/2/2009 9:17:43 AM 5552 Views
This tips will help you to limit the number of characters in the Asp.Net TextArea or Asp.Net Multiline Textbox. It uses a JavaScript function to limit the characters keyed in the TextArea.

Step 1: Add the below JavaScrpit function between the <script></script> in your aspx page.

function fnLimitTextAreaChars(textareaName, NoOfChars)
{
if (textareaName.value.length > NoOfChars)
{
textareaName.value = textareaName.value.substring(0, NoOfChars);
}
}

Step 2: In the code-behind at Page_Load event, add the two lines as follows.

TextBox1.Attributes.Add("onKeyDown", "fnLimitTextAreaChars(this, 150)");
TextBox1.Attributes.Add("onKeyUp", "fnLimitTextAreaChars(this, 150)");

It's done. Now when you type any letters in the TextArea, it will allow only first 150 characters. The rest will be truncated. Try yourself.

Post New Tips/Tricks View all Tips/Tricks

Featured Resources:
Simply JavaScript - Free 150 Page Preview!
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Windows 8: 50 Questions and Answers You Need to Make a Decision (Normally $9.95)
A guide to help you make an informed decision and determine the best way to use Windows 8 personally or professionally.
100% Free Subscription until Stock lost.View complete list.
Sponsored by