GeekArticles
Database
SQL ServerCreating a TextBox Word / Character Counter Control
Author: aspnet.4guysfromrolla.com |
Published: 31st Oct 2007 |
Visited: 146 times |
Add CommentFiled in: SQL Server
When storing user-supplied text data into a database, it is essential that the length of the user's input does not exceed
the size of the corresponding database table field. For example, imagine that you have defined a table with a field named
Comments of type varchar(50). In a web page, the user is presented with a textbox into which
to enter their comments. After doing so and clicking the "Save" button on the page, a postback ensues and the database
is updated with the user's input. If the user entered more than 50 characters worth of comments, however, running the database
UPDATE statement will result in an exception with the message: String
or binary data would be truncated.
To prevent these types of exceptions, you need to ensure that the user's input does not exceed the size defined in the
database. If it does, you must cancel the update and somehow alert the user that their input is too verbose. One way to
proactively alert users that their input is too long is to interactively show them how many characters they've entered,
along with how many maximum characters are allowed. This can be accomplished with a bit of client-side JavaScript code
that runs whenever the user presses a key within the textboxes whose lengths you want to track and display.
In this article we'll look at a custom ASP.NET 2.0 server
control I built named TextBoxCounter that uses JavaScript to display the total number of words and/or characters
entered into a specified TextBox. The control's complete source code is available at the end of the article and you're
welcome to use this control anyway you like (free of charge, of course). Read on to learn more!
Read More >
Read Article Sponsored Links
Related Articles
• Quick! Word Association: XML Today I took some time to quickly scan through a backlog in my feed reader. There were a good number anti-XML articles cropping up. This got me thinking. What do you think of when I say "XML"? I personally associat ...