By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,850 Members | 2,007 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,850 IT Pros & Developers. It's quick & easy.

Preventing user from copy/paste option

P: 79
Hi,

I have a text box and should allow user to type the text but not to allow them to copy/paste or right click.

Here is the code.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  4. <title>Untitled Document</title>
  5. <script language="javascript">
  6. function whichButton(event)
  7. {
  8. if (event.button==2)//RIGHT CLICK
  9. {
  10. alert("Not Allow Right Click!");
  11. }
  12.  
  13. }
  14. function noCTRL(e)
  15. {
  16. var code = (document.all) ? event.keyCode:e.which;
  17.  
  18. var msg = "Sorry, this functionality is disabled.";
  19. if (parseInt(code)==17) //CTRL
  20. {
  21. alert(msg);
  22. window.event.returnValue = false;
  23. }
  24. }
  25. </script>
  26. </head>
  27. <body>
  28. <form method="">
  29. <strong>Not Allow Paste </strong><BR>
  30. <input type="text" value="" onMouseDown="whichButton(event)" onKeyDown="return noCTRL(event)"/>
  31. </form>
  32. </body>
  33. </html>
  34.  
It works properly in IE and in mozilla also but in mozilla when right click option is used it will display the message saying that "Not allow right click option" but the menu will also displayed (i.e cut,copy,select all) and it allows to paste also.

How can this be avoided?

Any suggestions!!

with regards
Archana
Oct 13 '08 #1
Share this Question
Share on Google+
18 Replies


acoder
Expert Mod 15k+
P: 16,027
The first question to ask yourself is why you need this.

Rarely is it a good idea. In fact, some browsers allow users to prevent JavaScript code itself from preventing certain expected browser functionality.
Oct 13 '08 #2

P: 79
The first question to ask yourself is why you need this.

Rarely is it a good idea. In fact, some browsers allow users to prevent JavaScript code itself from preventing certain expected browser functionality.
Hi,

I need this because we are using email option so i want user to type there email id .They should not copy/paste there id's they should only type it. For that reason i should solve this problem!!

Any idea???

with regards
Archana
Oct 13 '08 #3

rnd me
Expert 100+
P: 427
human typing vastly increases the chance for error.
the form take longer to fill out.
these types of restrictions can make the page less accessible to disabled users.
doing so will also anger a large chuck of regular users.
it also does nothing to penalize someone who has javascript disabled, how fair is that?

what in your script prevents <shift>+<insert> or a menu key from pasting it in?

i beg you to ask yourself why you are going down this path in the first place.
clicking 'submit' can create a legally binding contract.

there is not a good enough reason to do so.
Oct 13 '08 #4

acoder
Expert Mod 15k+
P: 16,027
If it's for just a second confirmatory email field, this thread may help (if you insist).
Oct 13 '08 #5

P: 79
human typing vastly increases the chance for error.
the form take longer to fill out.
these types of restrictions can make the page less accessible to disabled users.
doing so will also anger a large chuck of regular users.
it also does nothing to penalize someone who has javascript disabled, how fair is that?

what in your script prevents <shift>+<insert> or a menu key from pasting it in?

i beg you to ask yourself why you are going down this path in the first place.
clicking 'submit' can create a legally binding contract.

there is not a good enough reason to do so.
Hi,

Thanks for your reply!!!

With regards
Archana
Oct 14 '08 #6

P: 79
If it's for just a second confirmatory email field, this thread may help (if you insist).
Hi,

Ya sure i will look into that!!!

Thanks for your reply!!!

With regards
Archana
Oct 14 '08 #7

P: 93
Both of my senior friends are correct but that is what the buisness concern.If someone wants to disable the right click not for the text field but for the pictures then the logic what my seniors has given is not correct.

This is about the technical concept what we are dealing .Right now for Archanak she wants to do it for the text field and she is getting some problem wth the mozila explorer so instead of talking about the business we should tell her about why the problem is.Isn't it my friends?

So, technically if you want to disable the right click of the mouse for the whole document then oncontextmenu is helpful.
But for the different tags you can't use this .So what you had done is ok with IE.

For mozila Add this piece of code where e is the event.As Mozila does not behave same as IE in context of script handling.
Expand|Select|Wrap|Line Numbers
  1. function ffdisable(e)
  2. {
  3. if (e.button==2)
  4.   { //firefox disable
  5.   e.preventDefault();
  6.   e.stopPropagation();
  7.   alert(message);
  8.   return false;
  9.   }
  10. }
Oct 14 '08 #8

acoder
Expert Mod 15k+
P: 16,027
Not quite. I understood what Achana asked. Pictures don't make a difference because the logic is inherently flawed. Trying to prevent the user from copy/pasting creates more problems than it solves unless you have some form of control over the user base, you use it sparingly and wisely and there are no accessibility problems. So explaining why it's not working in a particular browser is probably not required in most cases.
Oct 14 '08 #9

P: 93
Yes acoder that is fine but if that is requirement then we sholuld explain why the thing is malfunctioning.Thats what I had told there might be any other case also where people need not the copy/paste option but they want to disable the right click as per the code pasted by Archana.

Archana's code as she explained is running in IE but not in Mozila.So I had told to focus on this.Nothing else then that

What you suggested that is much fine.But my attention was to focus the problem as she mentioned.Please dont mind this otherwise.
Oct 14 '08 #10

P: 79
Both of my senior friends are correct but that is what the buisness concern.If someone wants to disable the right click not for the text field but for the pictures then the logic what my seniors has given is not correct.

This is about the technical concept what we are dealing .Right now for Archanak she wants to do it for the text field and she is getting some problem wth the mozila explorer so instead of talking about the business we should tell her about why the problem is.Isn't it my friends?

So, technically if you want to disable the right click of the mouse for the whole document then oncontextmenu is helpful.
But for the different tags you can't use this .So what you had done is ok with IE.

For mozila Add this piece of code where e is the event.As Mozila does not behave same as IE in context of script handling.
Expand|Select|Wrap|Line Numbers
  1. function ffdisable(e)
  2. {
  3. if (e.button==2)
  4.   { //firefox disable
  5.   e.preventDefault();
  6.   e.stopPropagation();
  7.   alert(message);
  8.   return false;
  9.   }
  10. }
Hi ,

Thanks for your reply!!!!
I tried but its giving that message("No right click is allowed) and along with that (cut,copy,paste,selectall) option menu is also displayed.

There is no solution for this problem?
with regards
Archana
Oct 14 '08 #11

acoder
Expert Mod 15k+
P: 16,027
There is, but you're going about it in the wrong way. Have you tried the code in the thread I linked to earlier?
Oct 14 '08 #12

P: 79
There is, but you're going about it in the wrong way. Have you tried the code in the thread I linked to earlier?
Hi,

I tried!!!

But i am getting context menu(cut,copy,paste)

I don't want context menu to come!!!

I have only one text box to take email id.

How should i call this function?

with regards
Archana
Oct 15 '08 #13

acoder
Expert Mod 15k+
P: 16,027
The code won't prevent the context menu from appearing because it doesn't need to. It detects a paste and lets the user know that the second email has to be typed in. Surely, that's what you wanted? It has the advantage of keeping all the browser functionality and still meeting your requirements.
Oct 15 '08 #14

P: 79
The code won't prevent the context menu from appearing because it doesn't need to. It detects a paste and lets the user know that the second email has to be typed in. Surely, that's what you wanted? It has the advantage of keeping all the browser functionality and still meeting your requirements.
Hi,

Is it possible to do such that context menu should not appear or even paste option should not be highlighted?

It works fine in IE i.e context menu will not appear in IE. but context menu appears in firefox.

How to avoid that??

With regards
Archana
Oct 16 '08 #15

acoder
Expert Mod 15k+
P: 16,027
OK, let's say you manage to disable the context menu in Firefox, but what about a user using the keyboard or using a scriptlet to automatically copy/paste the value? What if someone wants to use the context menu for another part of your page? What about the options from the file menu? What about other browsers? What about users who disable the ability of scripts interfering with functions like this? What about disabled users? Wat about someone who's disabled JavaScript? and so on...

By using the script suggested (in the link), the user can paste content as normal but it will be detected if it's not a single character key press. Anyone who works around that, well, you can always offer a change email page.
Oct 16 '08 #16

P: 2
It works perfectly!

Expand|Select|Wrap|Line Numbers
  1. <input type="text" onkeydown="checkItNoPaste(event);" />
Expand|Select|Wrap|Line Numbers
  1. function checkItNoPaste(evt){
  2.    evt = (evt) ? evt : window.event;
  3.    input= evt.target || evt.srcElement;
  4.    input.setAttribute('maxlength', input.value.length + 1); 
  5. }
So if user tries to paste using Ctrl+V or Ctrl+Insert, it pastes only 1 symbol.
And it's impossible to paste using context menu.
Nov 13 '08 #17

P: 2
Hello Everybody,

I am havig the same problem but not with textbox but with address bar.
Can anyone help me out

Thanks,
Sri Praveen
Apr 28 '09 #18

Markus
Expert 5K+
P: 6,050
@sripraveen
You have no control over the browser (from the webpage), nor should you be able to.

Why shouldn't the user be able to copy/paste in the address bar? You can't _force_ them to stay on your webpage. Is it really that bad? ;)

Mark.
Apr 28 '09 #19

Post your reply

Sign in to post your reply or Sign up for a free account.