Text Entry Field: Type-in YES, Paste-in NO

Is there a way to create a text entry field on an HTML page that will let a user type into it, but not let them cut and paste text from elsewhere into it?

The reason I ask this is that I am trying to set up a game
site.  The game rewards speed and I don't want people to
type their answers ahead of time into Wordpad and then just
cut and paste them into the text field.

This might also be useful on chat sites where some users (losers?) have nothing better to do than to cut-and-paste pages of text into the chat window and lock it up until the pasting is completed.

Who is Participating?

I suppose you could trap the ctrl-v combination in NS4/IE4. However I don't know how.
The simple answer is no because the cut and paste feature is controlled by Windows and not the application (ie the browser). In theory it is possible to prevent cut and paste operating when you develop a Windows application but as you have no control over the browser software in use your can't use this method.

The only way I can think that you can actually do this is by running a Java Applet that contains your fields but I suspect that this is what you want. Even then I don't know the java.awt package well enough to be certain that this is possible.


Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

I've managed to catch Ctrl-V in Netscape 4. I'm now working with IE4. Have patience :-)

Here it is:

<FORM><INPUT onFocus="selectedField=this"></FORM>
if (window.captureEvents)
protectedField = document.forms[0].elements[0];
protectedField.onkeyup = keyUp;
protectedField.lastValue = protectedField.value;
function keyUp(e) {
  if (e) {
    // Netscape
    if (e.which == 22) // Ctrl-V in Windows
      e.target.value = e.target.lastValue;
    e.target.lastValue = e.target.value;
  else {
    // Internet Explorer
    if (event.keyCode == 86 && selectedField.lastKey == 17) { // Ctrl-V in Windows
      selectedField.value = selectedField.lastValue;
    selectedField.lastValue = selectedField.value;
    selectedField.lastKey = event.keyCode;

Works great in NS4, but since I am no IE-pro it...well....It doesn't work the first time after you load the page and if you press the "V" button multiple times without releasing "Ctrl", it doesn't work.

Note that I don't even try to detect if the user selects Edit | Paste. I think that's impossible.

I think the code for Ctrl-V (or whatever is the keys for Paste) varies on different systems, so if you really want to catch the Paste command, you'll have to check the code on Mac and Unix (they also differs from NS to IE...). And you'll probably have to get a better MSIE routine...

dslavinAuthor Commented:
Seems like there are some possible answers (or at least workarounds.)
dslavinAuthor Commented:

Please answer the question so I can award you the points.  Thanks for all your efforts!
dslavinAuthor Commented:
Thanks for the thoroughness of your answer.  Even if it is not a complete solution for every browser (and modified browser where users have remapped the PASTE key) it is a great starting point.
I just came to think of this solution (it could be causing troubles for speedy typers, though):
You could use a timer with an interval of something like 10 milliseconds. Every time it goes off, you check the length of the field's value. If it differs with more than 1 (or some other low value, like 2 or 3) from the length of the saved value, insert the old value. Save value and reset timer.

Maybe you want me to write a script like that?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.