34

I have two textboxes, and I want to prevent user from copying the value from the first (email) textbox and pasting it in the second (confirmEmail) textbox.

Email: <input type="textbox" id="email"><br/>
Confirm Email:    <input type="textbox" id="confirmEmail">

I have two solution in my mind:

  1. Prevent copy action from the email textbox, or
  2. Prevent paste action from the confirmEmail textbox.

Any idea about how to do it?

http://jsfiddle.net/S22ew/

Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
Mr.Cocococo
  • 1,401
  • 2
  • 11
  • 13
  • 13
    Please don't do this... I find having to type my e-mail address twice an extremely bad user experience, and it makes me want to leave a site ASAP. And nothing stops me from making the same mistake twice. – beetstra Feb 24 '15 at 09:49
  • It will help you to disable cut, copy , paste operations - http://www.kvcodes.com/2014/03/disabling-textbox-cut-copy-and-paste-operations/ – Kvvaradha Oct 09 '16 at 09:19
  • 2
    I too loath "confirm email" fields - a bad user experience :( – Zeek2 Feb 22 '17 at 10:02
  • Worse are when I have to enter long account numbers like bank information. You can safely copy your account number from your bank and paste it into a field without fear of fat-fingering anything, but if you force somebody to type it in themselves, you throw in a potential for error. Do not do this. It's stupid. I hate sites that do this. – senfo Jun 12 '20 at 20:33

12 Answers12

72

Check this fiddle.

 $('#email').bind("cut copy paste",function(e) {
     e.preventDefault();
 });

You need to bind what should be done on cut, copy and paste. You prevent default behavior of the action.

You can find a detailed explanation here.

Dan
  • 1,130
  • 2
  • 20
  • 38
Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
  • Nice. If users encounter a problem with loading then wrapping it a "document ready handler" may help. i.e. jQuery(function ($) { }); Reference: http://stackoverflow.com/questions/19373455/jquery-keyup-function-doesnt-work – Zeek2 Feb 22 '17 at 10:12
  • 1
    Now it should use .on() – Ravi Singh Jan 02 '20 at 12:51
38

Use

oncopy="return false" onpaste="return false"

Email: <input type="textbox" id="email" oncopy="return false" onpaste="return false" ><br/>
Confirm Email:    <input type="textbox" id="confirmEmail" oncopy="return false" onpaste="return false">

http://jsfiddle.net/S22ew/4/

Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
Mr7-itsurdeveloper
  • 1,631
  • 2
  • 17
  • 24
9

EX:

<input type="textbox" ondrop="return false;" onpaste="return false;">

Use these attributes in the required textbox in HTML. Now the drag-and-drop and the paste functionality are disabled.

Saran
  • 101
  • 1
  • 4
5

Here is the updated fiddle.

$(document).ready(function(){
    $('#confirmEmail').bind("cut copy paste",function(e) {
        e.preventDefault();
    });
});

This will prevent cut copy paste on Confirm Email text box.

Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
5

Try This

 $( "#email,#confirmEmail " ).on( "copy cut paste drop", function() {
                return false;
        });
Tushar Dave
  • 220
  • 1
  • 5
  • @mr-cocococo let me know if you need a fiddle for this – Tushar Dave Jun 26 '14 at 07:08
  • This answer accounts for "bind" deprecated as of jQuery 3.0: http://api.jquery.com/category/deprecated/deprecated-3.0/ jsFiddle: http://jsfiddle.net/JoePC/S22ew/702/ – JoePC May 29 '18 at 22:34
  • 1
    @TusharDave this helped me instead `e.preventDefault();`. Thank You :) – Divya Dec 20 '18 at 11:24
3

you can try this:

   <input type="textbox" id="confirmEmail" onselectstart="return false" onpaste="return false;" oncopy="return false" oncut="return false" ondrag="return false" ondrop="return false" autocomplete="off">
Abhi
  • 318
  • 1
  • 10
2

UPDATE : The accepted answer provides the solution, but .on() is the method that should be use from now on.

"As of jQuery 3.0, .bind() has been deprecated. It was superseded by the .on() method for attaching event handlers to a document since jQuery 1.7, so its use was already discouraged."

http://api.jquery.com/bind/

0

Best way to do this is to add a data attribute to the field (textbox) where you want to avoid the cut,copy and paste.

Just create a method for the same which is as follows :-

function ignorePaste() {

$("[data-ignorepaste]").bind("cut copy paste", function (e) {
            e.preventDefault(); //prevent the default behaviour 
        });

};

Then once when you add the above code simply add the data attribute to the field where you want to ignore cut copy paste. in our case your add a data attribute to confirm email text box as below :-

Confirm Email: <input type="textbox" id= "confirmEmail" data-ignorepaste=""/>

Call the method ignorePaste()

So in this way you will be able to use this throughout the application, all you need to do is just add the data attribute where you want to ignore cut copy paste

https://jsfiddle.net/0ac6pkbf/21/

Rahul
  • 23
  • 1
  • 6
0

You might also need to provide your user with an alert showing that those functions are disabled for the text input fields. This will work

    function showError(){
     alert('you are not allowed to cut,copy or paste here');
    }
    
    $('.form-control').bind("cut copy paste",function(e) {
     e.preventDefault();
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea class="form-control"  oncopy="showError()" onpaste="showError()"></textarea>
Benson Kiprono
  • 129
  • 1
  • 1
  • 12
0

You can try below code

$(document).ready(function() {
    $('#email, #confirmEmail').on("cut copy paste drop", function(e) {
         e.preventDefault();
         return false;
    });
});

add below code in the text boxes as attributes

onpaste="return false" oncut="return false" oncopy="return false" ondrag="return false" ondrop="return false" onselectstart="return false"

example:

<input type="textbox" id="email" onpaste="return false" oncut="return false" oncopy="return false" ondrag="return false" ondrop="return false" onselectstart="return false" />
rsmdh
  • 128
  • 1
  • 2
  • 12
0
<input type="text" oncut="return false" 
oncopy="return false" onpaste="return false" />
toyota Supra
  • 3,181
  • 4
  • 15
  • 19
  • 2
    Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Aug 10 '23 at 21:29
-1

See this example. You need to bind the event key propagation

$(document).ready(function () {
    $('#confirmEmail').keydown(function (e) {
        if (e.ctrlKey && (e.keyCode == 88 || e.keyCode == 67 || e.keyCode == 86)) {
            return false;
        }
    });
});
Abbas
  • 14,186
  • 6
  • 41
  • 72
steeve
  • 699
  • 3
  • 10
  • 19
  • 2
    It's just disables,Control +C,Control + V from keyboard,you can right click and copy and past the content using your mouse – Hbirjand Nov 03 '14 at 05:43