?
Solved

disable copy and paste

Posted on 2004-04-22
5
Medium Priority
?
3,666 Views
Last Modified: 2011-09-20
i want users will type something in text box and wont copy & past into this field.
how it can be done?

e.
0
Comment
Question by:eladr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 2

Expert Comment

by:itwerx
ID: 10888673
You can disable the right-click function on your web page using Javascript though this is far from fool-proof. Mac users will still be able to do what they like as their mouse has only one-button, and other users could disable Javascript rendering all your efforts null and void.

Step 1. The following JavaScript goes between your <head> tags:

<script language="JavaScript">
 <!-- Begin
function protect(e) {
alert("Right-click has been disabled.");
return false;
}

function trap() {
if(document.images)
for(i=0;i<document.images.length;i++)
document.images[i].onmousedown = protect;
}
// End -->

Step 2: Insert the onLoad event handler into your <body> tag as below:

<body OnLoad="trap()">

Hope this helps...
0
 
LVL 1

Author Comment

by:eladr
ID: 10888783
10x.but i want to diable "ctrl" key also
0
 
LVL 2

Accepted Solution

by:
Splat earned 120 total points
ID: 10890778
Hi eladr,

Again, not a fool proof answer, but there are a few JScript events that can detect things such as onCopy and onPaste that work in Internet Explorer (not sure about other browsers).
Check out : http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/events.asp

An example of code:
<input onPaste="return false;"
           onCopy="return false;"
           onDrag="return false;"
           onDrop="return false;"
           onContextMenu="return false;">

Cheers,
 - Splat

0
 
LVL 2

Expert Comment

by:Splat
ID: 10890807
Also add:

onCut="return false;"

There may be others you want to use also, check the link in my last post.

Cheers,
 - Splat

0
 
LVL 5

Expert Comment

by:bitter_chicken
ID: 10892144
Hey eladr!

The previous was fine, but you might as well be giving the finger to anyone who doesn't use the latest IE.

The following is a more versatile solution; but it has a drawback - it requires the computer to keep up with the speed of the typist:
----------------------

<html>
<head>
</head>

<body>
<script language="javascript" type="text/javascript">
<!--
//Code by Jack Langman - www.tamskyit.com - Tamsky IT Solutions
//Free to use/change. Please leave in the header!
function tallyit(form)
{
if (form.textfield.value.length - parseInt(form.hiddenfield.value) < 5 ) { form.hiddenfield.value = form.textfield.value.length; return; } //note digit here (originally 5)
if (form.textfield.value.length < parseInt(form.hiddenfield.value) ) { form.hiddenfield.value = form.textfield.value.length; }
}

function validate(form,str_length,hidden_val) {
      
      if (str_length > hidden_val)
      {
            form.textfield.value = "";
            form.hiddenfield.value = 0;
            alert("Please TYPE a value!!");
      }
      
}
//-->
</script>
<form method="post" action="">
  <input type="text" name="textfield" onKeyUp="tallyit(this.form)" onBlur="validate(this.form,this.value.length,parseInt(this.form.hiddenfield.value))" value="" onCopy="return false;" onContextMenu="return false;">
  <input type="hidden" name="hiddenfield" value="0">
</form>
</body>
</html>

---------------------------

this includes the events to thwart IE users, but also has functionality to stop users of other platforms.

if a user pastes 5 or more characters (i have commented a note next to the location of the digit) then when they try to do something else, they will get a warning, and the box will be cleared. The number can be changed at will; but the problem is that if a user types too fast, the values can get out of whack. So if a user types too quickly, then they can get a warning regardless of whether they pasted something. The higher the variable (currently 5), the faster the script can tolerate the typing. But a value of 5 means up to 4 characters can be pasted without a warning.

Five worked pretty well for me, I couldn't type fast enough to get an erroneous response. And pasting 4 characters is probably useless in most circumstances....

hope this helps!

bitter_chicken

p.s. i spent ages doing this stupid malfunctionous script so i'd better get some bleeding points!

0

Featured Post

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question