Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Character Limit On Textbox

<input id="post" type="text" style="width:400px;"/>

You know when you go to a site, start typing in a textbox, and all of a sudden, your keyboard stops typing stuff because you've reached the character limit?

How can I make that happen for this textbox for 140 chars?

like image 733
slandau Avatar asked May 18 '11 03:05

slandau


1 Answers

function limitChars(textid, limit, infodiv) {
    var text = $('#'+textid).val(); 
    var textlength = text.length;
    if(textlength > limit) {
        $('#' + infodiv).html('You cannot write more then '+limit+' characters!');
        $('#'+textid).val(text.substr(0,limit));
        return false;
    }
    else {
        $('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
        return true;
    }
}

// Bind the function to ready event of document. 
$(function(){
    $('#comment').keyup(function(){
        limitChars('comment', 140, 'charlimitinfo');
    })
});
like image 191
albert Avatar answered Oct 05 '22 11:10

albert