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 1915 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:
Introduction to Web Applications Development
This 376 page eBook "Introduction to Web Applications Development", starts with an introduction to the internet, including a brief history of the TCT/IP protocol and World Wide Web.
Washington Technology
Washington Technology's coverage of government, business and technology issues helps contractors manage the business of delivering technology and services to the government.
100% Free Subscription until Stock lost.View complete list.
Sponsored by