 |
 |
Disabling paste into a form control?
|
 |
|
 |
|
Clinically Insane
Join Date: Nov 1999
Status:
Offline
|
|
I'm building a site with a set of forms. However, I have an interesting problem: I need to ensure that the user cannot paste into one of the fields. Copying out of the field isn't a problem, and the user still needs to be able to enter text, but I need to disable pasting if at all possible (the user needs to actually type out a short phrase, and we do not want a user to be able to just paste it in).
Any ideas? Can I use JavaScript to do this? Am I going to have to resort to Flash?
|
|
You are in Soviet Russia. It is dark. Grue is likely to be eaten by YOU!
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Aug 2002
Status:
Offline
|
|
The simplest solution would be to make that 'short phrase' a graphic so the user can't copy it in the first place.
--will
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: May 2001
Location: Nottingham, UK
Status:
Offline
|
|
I thought about having the short phrase in a disabled text field:
http://80.234.160.78/tests/disabled.asp
however, you can still copy the text in it with IE on the PC
Are you doing this to stop automated registrations?
I think Flash, or using the PHP gif generator would be required here
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Aug 2002
Status:
Offline
|
|
one thing that may work (i haven't tried this) would be to put a transparent gif above the the text (on a higher layer). Hopefully this would mean that the user would not be able to select the text because of the gif.
--will
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 2000
Status:
Offline
|
|
Originally posted by clam2000:
one thing that may work (i haven't tried this) would be to put a transparent gif above the the text (on a higher layer). Hopefully this would mean that the user would not be able to select the text because of the gif.
--will
I think that the position of the text field would be too hard to find. I supose you could fix the size and use position: relative and all that but I don't know how well it'd work. On a similar note, I did something like this where the header of a site simply loaded over a free counter 
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Dec 2002
Status:
Offline
|
|
What type of phrase is this going to be? letter and number combo? I think images would be your best bet.
|
|
Travis Sanderson
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Nov 2000
Location: Oslo, Norway
Status:
Offline
|
|
Ok, I see tho solutions.
a)
- Check the event (onKeyUp) to see if the ctrl key (WinDOS) or command Key (Mac) was pressed.
- Clear the textfield
b)
- Check the event (onKeyUp) to see if the length of the string in the textfield was increased with more than one.
- If true, clear the textfield
- If false do nothing
I'll be back...
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Nov 2000
Location: Oslo, Norway
Status:
Offline
|
|
Well.. This was a bit more complicated than I thought.
It seems that the browser (Safari) does not generate a key event when the "Apple key" is pressed.
This script might work on Windows. (I don't have any ten-foot-poles at hand, so I cannot test it).
Since those people use the ctrl key, as far as I know.
I will look into it further.
Code:
<html>
<head>
<script type="text/javascript" language="Javascript">
<!--
var lastLength = 0;
function handlePaste(textField) {
if((textField.value.length - lastLength) > 1) {
textField.value = ''; // Clear the textfield
lastLength = 0; // Reset character counter
alert("You tried to paste, you bastard");
}
lastLength = textField.value.length;
}
//-->
</script>
</head>
<body>
<form name="myform" method="post">
<input type="text" name="foo" onKeyUp="handlePaste(this);">
</form>
</body>
</html>
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Aug 2002
Location: Chicago
Status:
Offline
|
|
Try this site this script will disable a field in a form but still show the text.
JavaScript Section has great information on Javascript.
|
|

|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status:
Offline
|
|
< input name="test" value="test" readonly="readonly" />
|
|
Computer thez nohhh...
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Aug 2001
Location: Capitol City
Status:
Offline
|
|
Originally posted by Simon Mundy:
< input name="test" value="test" readonly="readonly" />
You'd still be able to copy from that, wouldn't you? I'm too lazy to try it out.
I like the checking for keystrokes idea.
You might rather count the keystrokes made in the text area, vs. how many are present when you either unfocus, or submit the form. It seemed like there could be a potential problem with a modifier/key combination with onKeyUp.
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Nov 1999
Status:
Offline
|
|
Originally posted by DeathMan:
You'd still be able to copy from that, wouldn't you? I'm too lazy to try it out.
Oh, I don't mind if people can copy out. I just need to be sure they can't paste in. The phrase is just something short and generic; "I Agree" would be a good example. I just want to make sure that it is actually typed out, rather than simply pasted in.
|
|
You are in Soviet Russia. It is dark. Grue is likely to be eaten by YOU!
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: May 2003
Location: Jämtland - Sweden
Status:
Offline
|
|
wouldn't it be possible to with a JavaScript controll if more than one character is added at one time?
Like if you one by one it is fine, but if there is 5 at once the field is cleared.
I don't know if it is possible, it was just an idea that went running through my head^^
|
|
Mighty3k @ #MacNN
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|