Submit FAQs Awards Usage Terms Contact
Categories
Partners
DaniWeb is an exciting community for web developers, Internet marketers, and technology enthusiasts.
Other Sections
Sweet Ads
Compatibility
Bookmark online:

NS6+ IE5+ Opera 7+

Form field Limiter

Author: Dynamic Drive

Note: Updated Dec 7th, 04'. Script now supports input either by form element's name or ID.

Description: Want to limit the amount of text a user can enter in particular INPUT or TEXTAREA elements? Well, this script enables not only that, but displays in real time the number of characters remaining. An extremely practical and useful script that functions in both IE4+ and NS6+!

Demo: Try typing in the below form.




Directions: Developer's View

Step 1: Add the below script to the <HEAD> section of your page:

Select All

Step 2: Insert the below sample form onto your page, which also demonstrates how to associate the script with your own form:

Select All

As shown, enabling the script on a particular form element is a simple process:

<input type="text" name="george"><br>
<script>
displaylimit("document.sampleform.george","",8)
</script>

Simply add the code in blue directly beneath the form element in question. Function displaylimit() within accepts the following three parameter:

displaylimit("document.formname.elementname", "elementID", "characterslimit")

  1. For the first parameter, enter the path to the form element being limited, whereby "formname" is the name of the form, and "elementname", the name of the element. Enter a blank string ("") instead if 2) below is entered. *

  2. For the second parameter, enter the ID of the form element in question. This provides an alternate way for you to reference your form element instead of using its name in 1). Enter a blank string ("") instead if 1) above is entered. *

  3. For the third parameter, enter the number of characters the form element should be limited to.

* Useful information

The 1) and 2) parameters of displaylimit() above allow you to reference your form element via two different ways- either using its name or ID. This is extremely useful if your form elements do not contain unique names, in which using their IDs is the only way to reference each one. For example, in many PHP forms, the names of form elements need to be arrays, such as:

<input type="text" name="survey[]">
<input type="text" name="survey[]">
<input type="text" name="survey[]">
Since "survey[]" is a shared and non unique name, passing it in as the first parameter of the function will not work. The solution is simply to assign each textbox with a unique ID attribute, and pass that value in instead for each textbox.