Click here to Skip to main content
15,937,779 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have a textbox with multilines. I am restricting the max length using java script. My problem is, when I enter the values using keyboard it's validating my max length but when I copy a text and paste it the same is accepting the values more than the given max length. How can I restrict the max length when I copy and paste the content to the textbox?
Posted
Updated 24-Nov-10 23:07pm
v2

You can check the paste on the keyup. If the keycode = 86, check the length.

<HTML><head></head><body>
<form name="myForm">
Text: <input type="text" name="myText"
onKeyUp = "fncKeyLength(this);">
</form>
<script>
function fncKeyLength(text){
if (window.event.ctrlKey){
if (window.event.keyCode == 86) {
   alert("<br />The string is this long: " + text.length);

}
}}
</script></body></HTML>
 
Share this answer
 
v2
Comments
MathewPV 25-Nov-10 5:15am    
sorry to say u i am new with web application i dont have much idea about java script i have inserted the following code but still its accepting paste in text box..
var key;
if(window.event)
{
key = window.event.keyCode; //IE
}
else
{
key = e.which; //firefox
}
if(key==86)

{
event.returnValue = false ;
}
Sandeep Mewara 25-Nov-10 5:18am    
Well, 2 things:
1. 86 is a keycode of 'V' so, this would be executed every time you press key capital V
2. for small 'v', this will not get executed.

Though, your suggestion could be made for a normal scenario with handling both 'V' & 'v'
Sebastien T. 25-Nov-10 5:21am    
Sandeep, it's true, thanks and you can test if "CTRL" is pressed. The correct test is:
if (e.ctrlKey && (e.keyCode == 118 || e.keyCode == 86))

MathewPV, because when keyup it is call, the textbox is normally already fill. If you want you can clear the textbox, (document.myform.Textbox1.value= "") or cut the text (document.myform.Textbox1.value= document.myform.Textbox1.value.substr(0, MAXLENGTH)
Ankur\m/ 25-Nov-10 5:33am    
There is still a problem with this code. A user can right click on the textbox and select paste.
I guess adding onpaste="return false;" would solve the issue.
MathewPV 25-Nov-10 5:35am    
e.ctrlKey is giving me a runtime java script error e is undefined
Ok, Check with this code.

XML
<html>
  <head>
  </head>
  <body>
        <form id="myForm">
          <asp:textbox runat="server" id="TextBox1" maxlength="40" onpaste="fncCheckLength()">

          function fncCheckLength()
          {
              var textBox = document.myForm.TextBox1;
              if(textBox.value.length > MaxLength)
              {
                 alert("Text too long");
                 textBox.value = "";
              }
          }
  </body>
</html>



For the keyboard check, the correct code is (but the Ankur remark is true for right click):

XML
<html>
  <head>
  </head>
  <body>
        <form>
          Text: <input type="text" name="myText"
          onKeyUp = "fncKeyLength();">
          </form>
          <script>
          function fncKeyLength()
          {
            var key;
            var IsCtrl = false;
            if (window.event)
            {
              key = window.event.keyCode; //IE
              IsCtrl = window.event.ctrlKey;
            }
            else
            {
              key = e.which; //firefox
              IsCtrl =  e.ctrlKey;
            }
            if( IsCtrl && (key == 118 || key == 86 ) )
            {
               fncCheckLength();
            }
          }
          function fncCheckLength()
          {
              var textBox = document.myForm.TextBox1;
              if(textBox.value.length > MaxLength)
              {
                 alert("Text too long");
                 textBox.value = "";
              }
          }
  </body>
</html>
 
Share this answer
 
Comments
MathewPV 25-Nov-10 6:32am    
Sebastian Termote, the code is giving me an error isCtrl is undefined
MathewPV 25-Nov-10 23:19pm    
Sebastian Termote, thanks and regards the code is working there is one simple validation that is if my text box length is 10 and i have 9 characters entered then i am able to paste the large content that i have copied.
Sebastien T. 26-Nov-10 3:02am    
It is strange and I do not see why. Sorry.
Dalek Dave 26-Nov-10 3:51am    
Good answer.
Add onpaste="return false;" in TextBox,
<asp:textbox runat="server" id="txtRef" maxlength="40" onpaste="return false;">
 
Share this answer
 
v2
Comments
Toniyo Jackson 25-Nov-10 5:37am    
If its working fine accept the answer.
MathewPV 25-Nov-10 5:40am    
Toni Jack, What you have given is correct i want to paste the content till the max length is achieved moreover my textbox mode is multiline
Dalek Dave 26-Nov-10 3:51am    
Good Call

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900