Skip to content

Max characters in textarea with jquery

I have the following code, and I’m kind of stuck on what to do next. The idea is when you enter text into a text area a counter tells you how many characters you have left. Once you get to the max characters I want to stop allowing characters to be entered, or delete all the characters that were entered so there are only 10 characters in the text area. I know I have to put the code where it says alert("LONG"); but I’m not quite sure what.

var maxLen = 10;
            var Length = $("#send-txt").val().length;
            var AmountLeft = maxLen - Length;
            if(Length >= maxLen){




Here it goes. Anything beyond character limit will be removed.

$('textarea').keypress(function(e) {
    var tval = $('textarea').val(),
        tlength = tval.length,
        set = 10,
        remain = parseInt(set - tlength);
    if (remain <= 0 && e.which !== 0 && e.charCode !== 0) {
        $('textarea').val((tval).substring(0, tlength - 1));
        return false;

Check working example at

User contributions licensed under: CC BY-SA
3 People found this is helpful