I have a script which is supposed to limit the no. of characters entered in a text box or textarea. I know I can do this using maxlength attribute. But I want to use JS implementation.

Below is the fiddle:

<http://jsfiddle.net/xTrKD/>

CODE:

```
$(function() {
function limitText(limitField, limitNum) 
    { 
        if (limitField.value.length > limitNum)
            { 
                limitField.value = limitField.value.substring(0, limitNum);
                }
        }

})​

```

HTML:

```
<input type="text" id="sessionNo" name="sessionNum" onKeyUp="limitText(this,5);" />

```