Limit number of characters in input field

前端 未结 9 877
Happy的楠姐
Happy的楠姐 2020-11-30 12:28

I want to use jquery to limit the number of characters in an editable div (or form input).

9条回答
  •  误落风尘
    2020-11-30 12:45

    This should work for you I think.

    HTML

    
    

    jQuery

    $('#myText').keyup(validateMaxLength);
    
    function validateMaxLength()
    {
            var text = $(this).val();
            var maxlength = $(this).data('maxlength');
    
            if(maxlength > 0)  
            {
                    $(this).val(text.substr(0, maxlength)); 
            }
    }
    

提交回复
热议问题