简体   繁体   English

禁用文本框的复制或粘贴操作?

[英]Disable Copy or Paste action for text box?

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阻止来自 email 文本框的复制操作,或
  2. Prevent paste action from the confirmEmail textbox.防止从 confirmEmail 文本框中进行粘贴操作。

Any idea about how to do it?关于如何做的任何想法?

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

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 .您可以在此处找到详细说明

Use

oncopy="return false" onpaste="return false" 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/ http://jsfiddle.net/S22ew/4/

EX:前任:

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

Use these attributes in the required textbox in HTML.在 HTML 中所需的文本框中使用这些属性。 Now the drag-and-drop and the paste functionality are disabled.现在拖放和粘贴功能被禁用。

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.希望能帮助到你。

Try This尝试这个

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

你可以试试这个:

   <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">

UPDATE : The accepted answer provides the solution, but .on() is the method that should be use from now on.更新:接受的答案提供了解决方案,但.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." “从 jQuery 3.0 开始,.bind() 已被弃用。自 jQuery 1.7 起,它被用于将事件处理程序附加到文档的 .on() 方法取代,因此已经不鼓励使用它。”

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

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()调用方法 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/ https://jsfiddle.net/0ac6pkbf/21/

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>

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" />

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;
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM