-1

Possible Duplicate:
Detecting a context-menu paste in the browser with jquery

I have a multi-line textbox, which is maximum take 100 character. I have already wrote a JavaScript for restrict up to 100 character, but when I am doing Ctrl+V on that textbox it is not removing the extra character. Can any one help me to resolve this issue?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
San20130121
  • 11
  • 2
  • 6

2 Answers2

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;
        }
    }
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
0

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

TechDo
  • 18,398
  • 3
  • 51
  • 64