Adding a character limit to a text area using YUI
In the UI world, it is a good idea to let the users know about any constraints they have on any form fields. We all know this. There are a million ways to do this – through error messages, help text, and so on. For character limits on input fields, the best way to avoid a user error is to limit the number of characters the user can physically type in the field.
This is super easy for input boxes. Simply add the maxlength attribute to the input HTML filed and voila!
So… I decided to write one. I sort of ran away with this example, mostly because there are so many things to consider from a user perspective. If someone is entering in a longer piece of text – for example, an e-mail to a customer service department or a review on a product – is it really better to just cut them off from entering more text before they have finished their thought? True, the user will not be able to submit the form until their giant essay has been tamed to fit within the character constraints. From a programmatic perspective, it is better to simply cut the user off. This ensures a successful form submission.
On the other hand, there are some people (like me) who would much rather finish their thought, and then go back and edit the text down to size. In this case, it is much more useful to have an (obvious) indicator of how long the entered text is, so that the user knows how much needs to be cut.
Long story short (I know… too late), I decided to create a proof of concept for limiting character limits that contains options for both scenarios above.
Step 1: Create a web page.
First, I created a very simple page with 3 elements:
- A text area input field.
- A message letting the user know how many characters they have typed and how many characters they are allowed.
- A check box to determine whether or not the text box will allow the user to continue to enter text once they have reached the character limit.
Function to update the text area alert
This function is called as soon as the page loads and then each time a key is pressed (on the keyup event) to update the number of characters used in the character limit message to the user. Also, if the user has entered more characters than allowed by the limit, the truncate text area function is called.
Function to check the text area limit.
This function checks to see if the value of the text area field is longer than the set character limit. If so, the function to truncate the text area’s contents is called. This function is called each time a key is pressed on the key down event. This is to avoid issues with the text “jumping” – the text area contents are actually updated after the key down event is called – so we call this function on key down so that we have a chance to suppress the content update if the user is over the character limit.
Function to truncate the contents of the text area.
This function truncates the text in the text area field if the user is over the character limit. This function is called by the two functions listed above.