my code is to handle the maxlength in the multiline textbox (textarea) so it works just like maxlength in the singleline text box , so I handled it onkeypress , but if I copy and paste , i want to substring the length to the maxlength from the clipboard , just to behave like the singleline textbox, the problem that the clipboard works only for the internet explorer but it doesn't work in other browsers
so function in JScript.js

function count(Obj, long) {
  //IE only
    var board = new String();
    var maxlength = new Number(parseInt(long ));

    if (window.event.ctrlKey) {

        if (window.clipboardData != "") {
      
if (window.clipboardData.getData("Text").length >= maxlength) {
  
     board.value = window.clipboardData.getData("Text");
     Obj.value = board.value.substring(0, maxlength);
        }
   }
 }

 if (Obj.value.length >= maxlength) {
  return false;
     }   
     else
     return true;     
 
}

and wrote in vb.net :

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        If (Me.textbox1.MaxLength <> 0) Then

            Me.textbox1.Attributes.Add("onpaste", "return count (this,'" + Me.textbox1.MaxLength.ToString + "');")
            Me.textbox1.Attributes.Add("onkeypress", "return count (this,'" + Me.textbox1.MaxLength.ToString + "');")
         
        End If


    End Sub

and in aspx i wrote :

<head runat="server">
    <title>Untitled Page</title>
    <script src="JScript.js" language="jscript" type ="text/jscript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="textbox1" runat ="server" Textmode="MultiLine" MaxLength="10"></asp:TextBox>
    
    </div>
    </form>
</body>
</html>

so, It works very proper in Internet explorer , but in chrome ,it limits to the maximum length in writing but in copy and paste it doesn't work, as clipboard is not working in chrome, is there any way to make it work in multi-Browser?

p.s :the above is just a sample

Thanks and Best regards,
Sandy

Recommended Answers

All 3 Replies

Member Avatar for stbuchok

is there any way to make it work in multi-Browser?

no

so is there any way to limit multiline textbox (maxlength) in multibrowser through the copy and paste
but before pasting the content in the textbox ?...

Member Avatar for stbuchok

Not through copy and paste. You can only give the field a maxlength (nothing to do with copy and paste)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.