Limit Characters and Words Entered | JavaScriptSource

Limit Characters and Words Entered

Will Bontrager Nov 17, 2006

General

This script will allow you to limit the number of words and/or characters entered into a form. Easily customizable.


This is an example textarea form field that limits input to either 80 characters or 16 words, whichever comes first.


characters typed
characters left
words typed
words left

Notes

The form tag must have a name= attribute. In addition to what's in this example, your form tag will probably have method= and action= attributes, and possibly others.

This example uses a TEXTAREA form field as the text field to be monitored. An INPUT bype="text" field may be used instead.

The next four fields are where the number of characters typed, the number of characters left, the number of words types, and the number of words left are displayed. They all have a readonly attribute so the user can't change the number.

Source Code

Paste this source code into the designated areas.

External file

Paste this code into an external JavaScript file named: charLeft.js


Head

Paste this code into the HEAD section of your HTML document.


Body

Paste this code into the BODY section of your HTML document


Leave a Response

(8 comments)

If the word limit is reached before the character limit the scritp still allows you to finish the character limit if you use spaces

Gavin Nov 30, 2006

Consigliere,[p]Thanks! I missed that. It should be fine now.

Lee Nov 28, 2006

great code! thank you for help!

Iron_Storm Nov 25, 2006

In the copy and paste code that is used as an external.js file, there is one mistake. At the end of the initial text block at the top, there is no closing */ This would be placed right after:[br /]"in the code may be removed if desired."

Consigliere Nov 24, 2006

The "which ever comes first" for this script is not true for reaching the word limit first. After the word limit is reached you can still add the remaining characters with a space. Otherwise a great script though.[p]Good Job

CTK Nov 21, 2006