Solved

disable copy and paste

Posted on 2004-04-22
5
3,650 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
5 Comments
 
LVL 2

Expert Comment

by:itwerx
Comment Utility
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
Comment Utility
10x.but i want to diable "ctrl" key also
0
 
LVL 2

Accepted Solution

by:
Splat earned 30 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Uploading files to the web server has become common part of almost any kind of web application. People use different technologies to solve this, but regardless of the technology used, it is always useful to have some kind of progress indicator shown…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This video teaches users how to migrate an existing Wordpress website to a new domain.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now