Link to home
Start Free TrialLog in
Avatar of eladr
eladr

asked on

disable copy and paste

i want users will type something in text box and wont copy & past into this field.
how it can be done?

e.
Avatar of itwerx
itwerx

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...
Avatar of eladr

ASKER

10x.but i want to diable "ctrl" key also
ASKER CERTIFIED SOLUTION
Avatar of Splat
Splat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also add:

onCut="return false;"

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

Cheers,
 - Splat

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!