tags:

views:

70

answers:

3

Im using Html.TextArea in my application and when i set its maxlength property to 255. It doesnt work and write more than 255 characters. how can this problem be solved

+1  A: 

The HTML <textarea> tag does not support the "maxlength" property. A variety of Javascript solutions exist, some for frameworks and others stand-alone. Basically it's necessary to catch keypress events (with Javascript) and track the content length. Usually some messaging to users is considered a Really Good Idea, like the Stackoverflow "Comment" boxes have.

Pointy
A: 

You can use a JavaScript validator. For example,

    function limitText(limitField, limitCount, limitNum) {
        if (limitField.value.length > limitNum) {
            limitField.value = limitField.value.substring(0, limitNum);
        } else {
            limitCount.value = limitNum - limitField.value.length;
        }
    }
<textarea name="limitedtextarea" onKeyDown="limitText(this.form.limitedtextarea,this.form.countdown,255);" 
SageNS