Solved

disable copy and paste

Posted on 2004-04-22
5
3,652 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
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 30 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
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).

863 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

28 Experts available now in Live!

Get 1:1 Help Now