10 Replies - 6156 Views - Last Post: 17 March 2012 - 04:11 AM

#1 sandysooso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-May 09

copy from clipboard is not working for chrome jscript + VB.net

Posted 16 March 2012 - 05:34 AM

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

Is This A Good Question/Topic? 0
  • +

Replies To: copy from clipboard is not working for chrome jscript + VB.net

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3525
  • View blog
  • Posts: 10,171
  • Joined: 08-June 10

Re: copy from clipboard is not working for chrome jscript + VB.net

Posted 16 March 2012 - 05:37 AM

JScript works only in IE. all other browsers (including Chrome) use Javascript.

that means that you have to completely rewrite your count() function for the other browsers.

note: esp. (to be) unstandardized things like clipboard access can vary between browsers.

This post has been edited by Dormilich: 16 March 2012 - 05:38 AM

Was This Post Helpful? 0
  • +
  • -

#3 sandysooso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-May 09

Re: copy from clipboard is not working for chrome jscript + VB.net

Posted 16 March 2012 - 06:08 AM

View PostDormilich, on 16 March 2012 - 05:37 AM, said:

JScript works only in IE. all other browsers (including Chrome) use Javascript.

that means that you have to completely rewrite your count() function for the other browsers.

note: esp. (to be) unstandardized things like clipboard access can vary between browsers.



yes , I tried it by Javascript , the same ; in chrome it understands the part of the maximum length but on the onkeyup event and onkeypress
but the clipboard is only for IE, so my problem is how to copy a substring from the clipboard in the chrome ?,

so as you said "unstandardized things like clipboard access can vary between browsers." , is there any other way to limit the textbox multiline mode in copy and paste?

but i don't want to substring from the textbox itself as " text.value=text.value.substring(0,maxlength); "
this will display the the whole length and then cut it, i want to substring without showing it to the user.

thanks
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3525
  • View blog
  • Posts: 10,171
  • Joined: 08-June 10

Re: copy from clipboard is not working for chrome jscript + VB.net

Posted 16 March 2012 - 06:16 AM

I don't know any out of my mind and clipboard stuff seems to be really different among browsers (according to a quick google search). webkit mentions clipboard events, maybe that's something to make use of?
Was This Post Helpful? 0
  • +
  • -

#5 sandysooso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-May 09

Re: copy from clipboard is not working for chrome jscript + VB.net

Posted 16 March 2012 - 06:25 AM

View PostDormilich, on 16 March 2012 - 06:16 AM, said:

I don't know any out of my mind and clipboard stuff seems to be really different among browsers (according to a quick google search). webkit mentions clipboard events, maybe that's something to make use of?



Regardless the clipboard stuff , is there any other way to limit the length in multiline textbox without showing that to the user?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3525
  • View blog
  • Posts: 10,171
  • Joined: 08-June 10

Re: copy from clipboard is not working for chrome jscript + VB.net

Posted 16 March 2012 - 09:21 AM

sure, check the content length while the user is typing (you can use the input / onpropertychange events for that)

something alike
// untested
textbox.addEventListener("input", function(evt) {
    if (this.value.length > 100) { // or whatever value you like
        evt.preventDefault();
    }
}, false);

Was This Post Helpful? 0
  • +
  • -

#7 sandysooso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-May 09

Re: copy from clipboard is not working for chrome jscript + VB.net

Posted 16 March 2012 - 10:11 AM

View PostDormilich, on 16 March 2012 - 09:21 AM, said:

// untested
textbox.addEventListener("input", function(evt) {
    if (this.value.length > 100) { // or whatever value you like
        evt.preventDefault();
    }
}, false);


but i didn't test the above, but i think the above code will prevent the user from typing more than 100 length but If i copied a text from anywhere (length >100) , i think this part is not handled. or am i wrong?

Thanks :)
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3525
  • View blog
  • Posts: 10,171
  • Joined: 08-June 10

Re: copy from clipboard is not working for chrome jscript + VB.net

Posted 16 March 2012 - 01:21 PM

try it out.
Was This Post Helpful? 0
  • +
  • -

#9 sandysooso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-May 09

Re: copy from clipboard is not working for chrome jscript + VB.net

Posted 17 March 2012 - 01:03 AM

I tried to do it with chrome but It doesn't work with me , but it IE doesn't have the property "addeventlistener"

what i wrote


        var textbox = document.getElementById("textbox1");
        textbox.addeventlistener("input", function(evt) {

        if (textbox.value.length >= 10) { 
               
                evt.preventDefault();
                
            }
         
        }, false);



Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3525
  • View blog
  • Posts: 10,171
  • Joined: 08-June 10

Re: copy from clipboard is not working for chrome jscript + VB.net

Posted 17 March 2012 - 03:15 AM

a) Javascript is case-sensitive
b) IE uses onpropertychange / attachEvent()
Was This Post Helpful? 0
  • +
  • -

#11 sandysooso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-May 09

Re: copy from clipboard is not working for chrome jscript + VB.net

Posted 17 March 2012 - 04:11 AM

Thank you v. much :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1