Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 24-Nov-10 23:03pm
Edited 24-Nov-10 23:07pm
JF201553.1K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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>
 
  Permalink  
v2
Comments
MathewPV at 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 at 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 Termote at 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/ at 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 at 25-Nov-10 5:35am
   
e.ctrlKey is giving me a runtime java script error e is undefined
MathewPV at 25-Nov-10 5:37am
   
Ankur, the thing is want to use paste function after the maxlength is achieved then it should not paste the rest of the content
Ankur\m/ at 25-Nov-10 5:47am
   
Replace 'e' by 'event'.
Ankur\m/ at 25-Nov-10 5:49am
   
Okay, In that case, instead of returning false in onpaste event, write the same check you did in the above function inside it.
Dalek Dave at 26-Nov-10 3:51am
   
Good Answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Ok, Check with this code.
 
<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):
 
<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>
  Permalink  
Comments
MathewPV at 25-Nov-10 6:32am
   
Sebastian Termote, the code is giving me an error isCtrl is undefined
MathewPV at 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 Termote at 26-Nov-10 3:02am
   
It is strange and I do not see why. Sorry.
Dalek Dave at 26-Nov-10 3:51am
   
Good answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Add onpaste="return false;" in TextBox,
<asp:textbox runat="server" id="txtRef" maxlength="40" onpaste="return false;">
  Permalink  
v2
Comments
Toni Jack at 25-Nov-10 5:37am
   
If its working fine accept the answer.
MathewPV at 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 at 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)

  Print Answers RSS
0 OriginalGriff 370
1 Prakriti Goyal 177
2 Sergey Alexandrovich Kryukov 155
3 jlopez788 134
4 _Amy 110
0 OriginalGriff 6,762
1 Sergey Alexandrovich Kryukov 5,539
2 Maciej Los 3,479
3 Peter Leow 3,323
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 26 Nov 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100