Disable Texbox Pasting/ctrl-V

liunx

Guest
Anyone know how to disable the user from pasting into a textbox. A solution in VB is what I am ultimately looking for, thanks in advance. This is how I have the textbox declared if that helps...

<asp:TextBox ID="txtMemo" TextMode="MultiLine" Runat="server" Width="400px" Height="200px"></asp:TextBox>This is technically not that easy

Your web page is being displayed on a browser which is outside your control. Once a textbox has been sent in the html page to the browser then the browser will decide how to work with it. Since text can be typed into a textbox the browser will allow any means of typing text into a textbox.

The only even remotely concievable way of achieving what your want to do is as follows:

1) On page load run a javascript function that will call itself once every half second. You can acieve this by using the settimeout function with a setting of 500 milliseconds.
2) In this function on the first run you would count the number of characters that are present in the text box and store it in a hidden form field.
3) On each subsequent run you would count the number of characters in the text box and compare the value to that you stored in the hidden form field on the previous run. If the difference is greater than 3 then you know that someone has pasted text into the box.
4) If more than 3 characters then only allow the first three characters and remove the rest.

The logic is based on the asumption that a user cannot type more than 3 characters per half second. if your users can type more then you probably want to adjust the value.

Alternatively you could adjust the code to check for more than one character per 100 milliseconds as long as your clients browsers are on decent enough machines this should probably work better.

The concept here is that when a user does CTRL V or other means of paste all the characters are added simultaneously in a single instant. On the other hand when a user types into a textbox then the characters are added one by one with a timespan between each character.

Good Luck

Tikiri

Web application development (<!-- m --><a class="postlink" href="http://www.zapstrategy.com">http://www.zapstrategy.com</a><!-- m -->)Since this is an operating system function the web page is two layers removed from the functionality and any decent security at either of those two layers will block your ability to access it. Not all operating systems use the CTRL-V for paste so blocking that will not work everywhere.Thanks for all the ideas, I actually ended up adding this code to the properties of each textbox:

onfocus="javascript:window.clipboardData.clearData()"

This way their clipboard is emptied before they have he chance to paste. Might not be the nicest way of doing it but it certainly gets the job done. Thanks againonfocus="javascript:window.clipboardData.clearData()"

This way their clipboard is emptied before they have he chance to paste.


just curious.... if I highlight whatever text I want to copy with my mouse, then put the focus back to the text box, and hit the middle button of my mouse using my linux box, will the clipboard thing would be any affect to it?not sure about the difference with linux as I have no experience with it. However, if it is moving anything to the sytem clipboard it will be erased.
 
Back
Top