How can I determine if the text has been truncated when I copy the text to the input field with the maxlength attribute

The HTML input element has a maxlength attribute that prevents text from entering longer than maxlength. When inserting text into the input field, if the text is longer than the length maxlength, it will be truncated to the exact length maxlength. How can I tell if a text has been truncated or not?

Thanks.

+4
source share
1 answer

Assuming you want to know if the length of the value of the input element matches the maximum length of this element, you can do something like this (using jQuery).

var $el = $("#myInputElement") var l = $el.val().length; var ms = $el.attr("maxlength"); var m = parseInt(ms, 10); // at this point m and l are maxlength and actual length 

This, however, is not the same as the truncated value, it may just be the exact length.

If you want to know if the user continues to record after reaching the maximum length, you can use the keyup event viewer and regardless of whether the input is focused. However, you may need Flash to compare the clipboard with your input.

In short, there is no good solution.

0
source

Source: https://habr.com/ru/post/1441367/


All Articles