javascript - Multiple dynamic input character restriction -
i have form users can add inputs dynamically. sum of inputs character length shouldn't higher 500 , can not rid of in correct way.
current javascript code looks this:
$('.group[data-maxchar] .add-item-input').on('keyup paste', function () { var group = $(this).closest('.group[data-maxchar]'); var maxchar = parseint($(this).closest('.group[data-maxchar]').attr('data-maxchar')); var remainingchar = parseint(group.find('.counter').text()); var str = $(this).val(); var length = str.length; if (remainingchar < 1) { $(this).val(str.substr(0, maxchar)); } updatecounters(group); });
on first input added can i'm able insert 500 characters correctly , if try paste ot type more text stopped , not exceed it.
unfortunately if add more inputs see counter going negative , i'm able write text...
i'm pretty sure error in part of control:
if (remainingchar < 1) { $(this).val(str.substr(0, maxchar)); //here }
how can solve it?
the main problem on
event. check jquery api
proper usage $('.group[data-maxchar]').on('keypress paste', '.add-item-input', function () {
. function going triggered each element in group.
also use keypress
instead of keyup
because want handle printable characters.
Comments
Post a Comment