Some sites have a limit on the number of characters entered in a field (for example, leaving a comment), after typing the maximum number of characters, nothing is introduced any further, how to make this restriction?

  • I figured it out myself, share it with others. How to limit the number of characters entered in <textarea> Cyrillic - 70 Latin - 160 - Taypfoon

2 answers 2

If the field input type=text , then set maxlength ; if textarea , then javascript is needed). And then all this can be bypassed) At PCP, you can check the length of the entered data through strlen and if they are larger than х , return an error)
So an example of what you need? html, javascript, php ??

  • Yes, I already figured it out. - nick777
  • one
    I would like to clarify - if utf-8 is used, it is not strlen but mb_strlen <br> In general, I absolutely agree, and here I also did not understand php ... - Zowie
  • but I didn’t know that there was a difference)) although yes, I only recently translated the project into the utf) - DemoriaN

Here for all browsers with paste support,

 $('.limited').bind('keyup keydown paste',function(e) { var $t=$(this); setTimeout(function(){ var max=$t.attr('maxlength')*1; if ($t.val().length>max) { $t.val($t.val().substr(0,max)); } $t.next().children('span').text(max-$t.val().length); },0); }); 

setTimeout required in order for the string to fall into the field, and then you can work with it.

demo