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

Restrict user to copy/paste Issue

P: 19
Hi all,
I have a form with fields email and confirm email. I want to restrict the user not to copy the value from email field and paste it into the other one.

I think this is possible through javascript, thats why i am posting my problem here.

Anyone who can give some idea on how it can be achieved, please post it here as it is really urgent.

Thanks
Jun 10 '08 #1
Share this Question
Share on Google+
8 Replies


gits
Expert Mod 5K+
P: 5,390
you could make it harder but you cannot fully avoid it ... for 'ctrl-V' you have to handle the keypress-event, for the contextmenu you could handle the 'oncontextmenu'-event but for the browsers menubar you are lost ...

kind regards
Jun 10 '08 #2

100+
P: 428
Its never a good idea to fight with your users,
but you can use some indirection here, if you insist.

Save each key's value from the second email field as it is pressed,
in some accessible place- maybe the field's title.

When the form is validated, or the field blurs or changes,
test if the title of the second field matches the value of the first.

If your two email fields are named 'email1' and 'email2',
(I assume that email1 has its own validation for good email syntax,
and there is no title set on email2):
Expand|Select|Wrap|Line Numbers
  1. document.getElementsByName('email2')[0].onkeypress= function(e){
  2.     e= window.event || e;
  3.     var who= e.target || e.srcElement;
  4.     var c= e.charCode || e.keyCode;
  5.      if(who.value=='') who.title='';
  6.     who.title+=String.fromCharCode(c);
  7.  
  8. }
  9. document.getElementsByName('email2')[0].onchange= function(e){
  10.     var in1= document.getElementsByName('email1')[0];
  11.     var in2= document.getElementsByName('email2')[0];
  12.     if(in1.value && in1.value!= in2.title){        
  13.         in2.title='';
  14.         in2.value='';
  15.         alert('Please type your email again');
  16.         in2.focus();
  17.         return false;
  18.     }
  19.     else alert(in2.title);
  20.     return true;
  21. }
  22.  
Jun 10 '08 #3

P: 19
Hi,
Thanks for reply.
I am able to achieve the required functionality using the following code.
Expand|Select|Wrap|Line Numbers
  1. var el; 
  2. onload=function()
  3.     el = document.forms[0].elements; 
  4.     for(var i=0;i<el.length;i++)
  5.     { 
  6.         if(el[i].name=='confirmEmail')
  7.         {
  8.             el[i].onpaste=function(){return false; } 
  9.         } 
  10.     } 
  11. }
But the problem is that it is working only for IE and not for firefox.

Anybody has any idea, what we have to do to make it comapatible to both the browsers.

Thanks
Gaurav


Its never a good idea to fight with your users,
but you can use some indirection here, if you insist.

Save each key's value from the second email field as it is pressed,
in some accessible place- maybe the field's title.

When the form is validated, or the field blurs or changes,
test if the title of the second field matches the value of the first.

If your two email fields are named 'email1' and 'email2',
(I assume that email1 has its own validation for good email syntax,
and there is no title set on email2):
Expand|Select|Wrap|Line Numbers
  1. document.getElementsByName('email2')[0].onkeypress= function(e){
  2.     e= window.event || e;
  3.     var who= e.target || e.srcElement;
  4.     var c= e.charCode || e.keyCode;
  5.      if(who.value=='') who.title='';
  6.     who.title+=String.fromCharCode(c);
  7.  
  8. }
  9. document.getElementsByName('email2')[0].onchange= function(e){
  10.     var in1= document.getElementsByName('email1')[0];
  11.     var in2= document.getElementsByName('email2')[0];
  12.     if(in1.value && in1.value!= in2.title){        
  13.         in2.title='';
  14.         in2.value='';
  15.         alert('Please type your email again');
  16.         in2.focus();
  17.         return false;
  18.     }
  19.     else alert(in2.title);
  20.     return true;
  21. }
  22.  
Jun 11 '08 #4

acoder
Expert Mod 15k+
P: 16,027
onpaste doesn't work in most browsers, probably only IE.

Have you tried mrhoo's suggested code?
Jun 11 '08 #5

P: 19
Hi,

Thanks for reply.
Actually I could not understand the flow of code, i.e.how it will work, posted by mrhoo. And I am not using the plain html code ( I am using a tool SIM 7.1), thats why I have to look into how will it work in my case and that is possible only if I can understand the code.

If you,mrhoo or anyone else can elaborate it, then it will be really helpful to me.

Thanks
Gaurav

onpaste doesn't work in most browsers, probably only IE.

Have you tried mrhoo's suggested code?
Jun 11 '08 #6

100+
P: 428
If you have a handler on the second text box that adds the letter from each of its keydowns to a value (the box's empty title is easily read and written to), it doesn't matter what they do to fill the box.

The built up title will only match if each character was pressed in turn.

If you paste the address, the most you would get is a 'v', if pasted with the key board, and nothing at all if pasted using the mouse menu.
Jun 11 '08 #7

P: 2
To prevent pasting to text field, my idea was to limit content length using 'maxlength'. Increase 'maxlength' by 1 only if a key is pressed.
So:

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 #8

P: 88
I think this is possible through javascript, thats why i am posting my problem here.Thanks
What if they turn off javascript, how would you restrict then?
Nov 15 '08 #9

Post your reply

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