Restrict user to copy/paste Issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gjain12
    New Member
    • Jun 2008
    • 19

    Restrict user to copy/paste Issue

    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
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

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

    Comment

    • mrhoo
      Contributor
      • Jun 2006
      • 428

      #3
      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):
      Code:
      document.getElementsByName('email2')[0].onkeypress= function(e){
      	e= window.event || e;
      	var who= e.target || e.srcElement;
      	var c= e.charCode || e.keyCode;
       	if(who.value=='') who.title='';
      	who.title+=String.fromCharCode(c);
       
      }
      document.getElementsByName('email2')[0].onchange= function(e){
      	var in1= document.getElementsByName('email1')[0];
      	var in2= document.getElementsByName('email2')[0];
      	if(in1.value && in1.value!= in2.title){		
      		in2.title='';
      		in2.value='';
      		alert('Please type your email again');
      		in2.focus();
      		return false;
      	}
      	else alert(in2.title);
      	return true;
      }

      Comment

      • gjain12
        New Member
        • Jun 2008
        • 19

        #4
        Hi,
        Thanks for reply.
        I am able to achieve the required functionality using the following code.
        Code:
        var el; 
        onload=function()
        { 
        	el = document.forms[0].elements; 
        	for(var i=0;i<el.length;i++)
        	{ 
        		if(el[i].name=='confirmEmail')
        		{
        			el[i].onpaste=function(){return false; } 
        		} 
        	} 
        }
        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


        Originally posted by mrhoo
        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):
        Code:
        document.getElementsByName('email2')[0].onkeypress= function(e){
        	e= window.event || e;
        	var who= e.target || e.srcElement;
        	var c= e.charCode || e.keyCode;
         	if(who.value=='') who.title='';
        	who.title+=String.fromCharCode(c);
         
        }
        document.getElementsByName('email2')[0].onchange= function(e){
        	var in1= document.getElementsByName('email1')[0];
        	var in2= document.getElementsByName('email2')[0];
        	if(in1.value && in1.value!= in2.title){		
        		in2.title='';
        		in2.value='';
        		alert('Please type your email again');
        		in2.focus();
        		return false;
        	}
        	else alert(in2.title);
        	return true;
        }

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

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

          Have you tried mrhoo's suggested code?

          Comment

          • gjain12
            New Member
            • Jun 2008
            • 19

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

            Originally posted by acoder
            onpaste doesn't work in most browsers, probably only IE.

            Have you tried mrhoo's suggested code?

            Comment

            • mrhoo
              Contributor
              • Jun 2006
              • 428

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

              Comment

              • eshka
                New Member
                • Nov 2008
                • 2

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

                Code:
                <input type="text" onkeydown="checkItNoPaste(event);" />
                Code:
                function checkItNoPaste(evt){
                   evt = (evt) ? evt : window.event;
                   input= evt.target || evt.srcElement;
                   input.setAttribute('maxlength', input.value.length + 1); 
                }
                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.

                Comment

                • serdar
                  New Member
                  • Nov 2008
                  • 88

                  #9
                  Originally posted by gjain12
                  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?

                  Comment

                  Working...