Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable input field from pasting

Status
Not open for further replies.

LouisC4

Programmer
Jul 26, 2003
54
US
How do I go about making an input field or textbox unable to accept pasting (i.e. CTRL-V or right-click paste). I do want the user to be able to type in this input field but I don't want them to be able to paste.

Thanks,

Louis
 
You can clear out the clipboard on the onfocus event handler in the textbox. That would be done like so:
Code:
<input type="text" onfocus="clearClipboard()" />

Your javascript would be:
Code:
function clearClipboard() {
   window.clipboardData.clearData('Text');
}

That might make some people mad, but it'll do what you want.
(note: This will NOT work if you are using Firefox at all)




<.

 
Good luck.
This is a very difficult thing to do.

In order to stop a paste you have to take into account three different methods of pasting. 1. Ctrl-V, 2. Clicking Edit then Paste, 3. Right mouse clicking and selecting Paste.

To make things worse, to prevent a ctrl-v paste you have to trap the event for the ctrl-v keypress so you know someone tried pasting from the keyboard.

What you would have to do is keep track of what characters you already have in the input field and trigger your script when someone presses a key in that field. YOu would then have to analyze what the current content is, make sure it is only one character higher or one character lower than the last time the event ran and also test that the remaining content of the field match what was there before.

I have played with this in the past and gotten as far as blocking Ctrl-V events but not the menu paste methods.


At my age I still learn something new every day, but I forget two others.
 
This will work fine for IE:
Code:
<textarea onpaste="event.returnValue=false;"></textarea>

But will be steadfastly ignored by other browsers.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Enable Apps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top