开发者

keydown doesn't catch text-changed by hold-up copy action (Ctrl-V)

I have an text input. I wanna limit number of its character downto 300. So I'm catching keydown event on it. But 开发者_JAVA百科in case that user copies some text into clipboard, then pastes it into the text input by press and HOLD UP Ctrl-V (please be noticed that Ctrl-V is hold-up, not released), text is changed but I can't apply the limitation, TILL Ctrl-V is released That's the fact though many ways have been tried : onkeydown, onkeypress, onkeyup, onchange. Does anyone come up with solution ? Thank you and have a good working day.


You can try this (change your selector appropriately):

//Restrict text while pasting
$("#myinput").bind('paste', function () {
    var input = $(this);
    var maxLength = input.attr("maxLength");
    setTimeout(function () {
        input.val(input.val().substring(0, maxLength));
    }, 100);

});

//Restrict text while typing
$("#myinput").live("keypress", function () {
    var maxLength = $(this).attr("maxLength");
    return $(this).val().length <= maxLength - 1;
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜