Introduction
Recently, I was revisiting the JavaScript used to limit
the number of characters in a TextBox control. This client-side script utilized
document.getElementById method and the control ID for HTML markup that
was generated by ASP.NET. The problem with this script was that it did not work
correctly with multiple TextBox controls on the web page and not cross-browser
compatible. So I decided to rewrite it to ease the mentioned problems. Shown
in Listing 1 is the new content of the JavaScript. There are
two functions resided in it namely validateLimit and get_object.
The former function accepts three parameters.
1. TextBox object
2. HTML Div id (to hold the text)
3. Maximum number of characters the TextBox control can hold
The purpose of the later function is to ensure that modern and older browsers are able to access the form elements.

History
- May 20, 2010: First release.
- May 20, 2010
- Added new function trimEnter to replace enter key with empty string.
Conclusion
If you find any bugs or disagree with the contents, please drop me a line and I'll work with you to correct it. I would suggest downloading the demo and explore it in order to grasp the full concept of it because I might left out some useful information.
Tested on IE 6.0/7.0/8.0, Google Chrome, Firefox, Safari
Resources
document.getElementById On All Browsers – Cross browser getElementById
Modifying Strings with Regular Expressions

baileyPosted May 31, 2010, 12:09 AM
Just my opinion -- in striving for elegant software design is to eliminate code like javascript that has a risk of cross-platform incompatibility, adds more script to be rendered in the browser, uses unnecessary bandwith as a result and creates a potential maintenance issue. Javascript has it's place but for limiting characters it is better to set the property for the core framework, and let ASP.Net take care of the rendering, that way it always works in all browsers and does not send and extra block of Javascript to the browser for each page.
baileyPosted May 30, 2010, 9:42 AM
Just curious, why this approach instead of just setting the Maxlength property?
Maulik PatelPosted May 20, 2010, 2:43 AM
thank you for your document.