-1

可能重复:
使用 jquery 检测浏览器中的上下文菜单粘贴

我有一个多行文本框,最多 100 个字符。我已经编写了一个 JavaScript 来限制最多 100 个字符,但是当我在那个文本框上执行Ctrl+V时,它并没有删除多余的字符。谁能帮我解决这个问题?

4

2 回答 2

0

Use onkeypress, onkeyup as well as onkeydown on input text.

Use the below Javascript finction:

<asp:TextBox ID="txtvalueAr" runat="server" TextMode="MultiLine" 
onkeypress="return CheckLength(this,160)" onkeyup="return CheckLength(this,160)"
onkeydown="return CheckLength(this,160)" onPaste="return CheckLength(this,160)">
</asp:TextBox>

Javascript

    function CheckLength(txt, maxLen) {
        try {
            if (txt != null) {
                var iLength = txt.value.length
                if (iLength <= maxLen) //Check the Limit.
                {
                    //Display the remaining characters
                    document.getElementById('character').innerHTML = maxLen - iLength + " are remaining characters.";
                }
                else {
                    txt.value = txt.value.substring(0, maxLen);
                    return false;
                }
            }
        }
        catch (e) {
            return false;
        }
    }
于 2013-01-04T11:09:54.813 回答
0

Call the javascript function in onKeyUp ,onblur and onmouseout events.

于 2013-01-04T11:11:19.623 回答