Jquery - Limit text in text input, not textarea

后端 未结 1 1793
[愿得一人]
[愿得一人] 2021-01-28 03:01

Using jquery how can I limit the number of characters that can be typed in a text field? Not referring to a textarea, just a normal text input field. I have a form and viewing v

相关标签:
1条回答
  • 2021-01-28 03:33

    You need to bind to the keydown event for the field and prevent it from entering a character if there are already 10 in that field: http://jsfiddle.net/j6tJ3/

        $('input').keydown(function(e){
            if($(this).val().length > 9 && e.which !== 8 && e.which !== 46){ //if it's too long and the key pressed isn't backspace or delete
                return false;
            }
        });
    
    0 讨论(0)
提交回复
热议问题