Jquery help to enforce maxlength on textarea?

跟風遠走 提交于 2019-11-30 12:46:22
X9DESIGN

This is the best solution! Put this into your script tag in HTML code

$("textarea[maxlength]").on("propertychange input", function() {
    if (this.value.length > this.maxlength) {
        this.value = this.value.substring(0, this.maxlength);
    }  
});

and now use <textarea maxlength="{your limit}"></textarea> for {your limit} chars limit.

Chaka

The solution is here:

http://web.enavu.com/daily-tip/maxlength-for-textarea-with-jquery/

$(document).ready(function() {

    $('textarea[maxlength]').keyup(function(){
        //get the limit from maxlength attribute
        var limit = parseInt($(this).attr('maxlength'));
        //get the current text inside the textarea
        var text = $(this).val();
        //count the number of characters in the text
        var chars = text.length;

        //check if there are more characters then allowed
        if(chars > limit){
            //and if there are use substr to get the text before the limit
            var new_text = text.substr(0, limit);

            //and change the current text with the new text
            $(this).val(new_text);
        }
    });

});

Cant you use this :

    class="validate[required,maxSize[50]] " 

This solution is working as native HTML text-area , MAX-Length property

Var MaxLength=100; 
$('#Selector').keydown(function (e) {
            var text = $(this).val();
            var chars = text.length;
            if (chars > MaxLength) {
                if (e.keyCode == 46 || e.keyCode == 8 || e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 39 || e.keyCode == 40) {
                    return true;
                }
                return false;
            }
            return true;
        });

Don't set the Maxlength property in the server side control (asp:TextBox) as server won't convert maxLength property to HTML markup when returning to your browser.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!