Solved

disable copy and paste

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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

695 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