limit number of characters entered in textarea

Posted by Abu Hamzah on Stack Overflow See other posts from Stack Overflow or by Abu Hamzah
Published on 2010-05-10T19:12:52Z Indexed on 2010/05/10 19:24 UTC
Read the original article Hit count: 243

Filed under:

here is the script does what i want but not exactly,

my question is, how can i stop user entering text once it reached the lmit of 255 characters?

var limit = 255;
            var txt = $('textarea[id$=txtPurpose]');
            $(txt).keyup(function() {
                var len = $(this).val().length;
                if (len > limit) {
                    //this.value = this.value.substring(0, 50);
                    $(this).addClass('goRed');
                    $('#spn').text(len - limit + " characters exceeded");
                    return false;
                }
                else {
                    $(this).removeClass('goRed');
                    $('#spn').text(limit - len + " characters left");
                }
            });

if there is a better way please let me know.

© Stack Overflow or respective owner

Related posts about jQuery