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.

Thanks.
LVL 2
dslavinAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
martinagConnect With a Mentor Commented:
Answered.

Martin
0
 
rafisternCommented:
I suppose you could trap the ctrl-v combination in NS4/IE4. However I don't know how.
0
 
mouattsCommented:
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.

HTH

Steve
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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

Martin
0
 
martinagCommented:
Here it is:

<FORM><INPUT onFocus="selectedField=this"></FORM>
<SCRIPT>
if (window.captureEvents)
  captureEvents(Event.KEYDOWN);
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;
  }
}
</SCRIPT>

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...

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

Please answer the question so I can award you the points.  Thanks for all your efforts!
0
 
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.
0
 
martinagCommented:
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?

Martin
0
All Courses

From novice to tech pro — start learning today.