What is the best way to emulate an HTML input “maxlength” attribute on an HTML textarea?

后端 未结 8 1305
执笔经年
执笔经年 2020-11-28 11:54

An HTML text input has an attribute called \"maxlength\", implemented by browsers, which if set blocks user input after a certain number of characters.

An HTML texta

8条回答
  •  [愿得一人]
    2020-11-28 12:39

    Here's a jQuery plugin that I use called "Simply Countable" that allows you to add a counter to a textarea. One of the features is that it allows you to set a maximum number of characters that are allowed in a field and it enforces it so the user can't exceed that limit.

    Here's the GitHub page for Simply Countable:
    https://github.com/aaronrussell/jquery-simply-countable/

    You would use it like this...

    $('#my_textarea').simplyCountable({
        counter: '#counter',
        maxCount: 140,
        strictMax: true
    });
    

提交回复
热议问题