JavaScripTools Manual


Working with the SizeLimit

A classic problem in web applications is to limit the number of characters on a textarea. Many use something like onkeyup="if (this.value.length > 500) this.value = this.value.substring(0, 500)". That's ugly.

SizeLimit is used just like all other masks, and handles both onkeyup and onblur events (many people forget that the user can paste data with mouse). Aditionally, it can display a custom text on a given html element or input field, or allow the user to perform custom actions. Look at the samples to see what SizeLimit can do.

The contructor arguments are (only the first two are mandatory):

Here are some examples (assuming a textarea named "text" and a div named "text_output"):

//The simplest way
new SizeLimit("text", 500);

//Using an output
var limit = new SizeLimit("text", 500);
limit.output = "text_output";
limit.outputText = "${size}/${max}" //This would display sometihing like "127/500"

//Using a custom function to display the percent left
function onUpdate(control, size, max, left) {
    setValue("text_output", Math.round(size * 100 / max) + "%");
var limit = new SizeLimit("text", 500);
limit.updateFunction = onUpdate;

Working with the DateMask
Table of Contents Next:
Introduction to JavaScripTable