Html 如何防止用户在文本框中粘贴文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15320069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:18:00  来源:igfitidea点击:

How to prevent user pasting text in a textbox?

javascripthtmlvalidation

提问by PeterS

I have this JS function that prevents user from typing characters

我有这个 JS 函数可以防止用户输入字符

<script type="text/javascript">
function validate(evt) {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode(key);
  var regex = /[0-9]|\./;
  if(!regex.test(key)) {
    theEvent.returnValue = false;
    if(theEvent.preventDefault) theEvent.preventDefault();
  }
}
</script>

<span>Radio Receive:</span>
<input name="ReceiveNo" type="text" class="txtbox" onkeypress='validate(event)' maxlength="11" value="${cpCon.receiveNo}" required tabindex="34" />

But I noticed that when the user tried to paste a word from this textbox, the text can be entered. How can I prevent this without disabling the paste?

但是我注意到当用户试图从这个文本框中粘贴一个单词时,可以输入文本。如何在不禁用粘贴的情况下防止这种情况发生?

回答by arefin2k

Very Easy Solution:

非常简单的解决方案:

<input name="ReceiveNo" type="text" class="txtbox" onkeypress='validate(event)' maxlength="11" value="${cpCon.receiveNo}" required tabindex="34" onCopy="return false" onDrag="return false" onDrop="return false" onPaste="return false" autocomplete=off />

This worked for me very well. No one can paste now into your textbox using right button paste option of mouse or pressing ctrl+v from the keyboard.

这对我非常有效。现在没有人可以使用鼠标右键粘贴选项或从键盘按 ctrl+v 粘贴到您的文本框中。

回答by Mallikarjuna Reddy

// To Disable the paste functionality

// 禁用粘贴功能

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

// Below One might be helpful for your functionality.

// 下面一个可能对您的功能有帮助。

$(document).ready(function(){
  $('#txtInput').bind("paste",function(e) {
    validate(e);
  });
});

or

或者

OnTabOut()or onblur()you can validate the entered / pasted text instead of handling the paste functionality.

OnTabOut()或者onblur()您可以验证输入/粘贴的文本而不是处理粘贴功能。

回答by Abhishek

I tried this in my Angular project and it worked fine without jQuery.

我在我的 Angular 项目中尝试过这个,它在没有 jQuery 的情况下运行良好。

<input type='text' ng-paste='preventPaste($event)'>

And in script part:

在脚本部分:

$scope.preventPaste = function(e){
   e.preventDefault();
   return false;
};

In non angular project, use 'onPaste' instead of 'ng-paste' and 'event' instead of '$event'.

在非角度项目中,使用 'onPaste' 代替 'ng-paste' 和 'event' 代替 '$event'。

回答by Chtiwi Malek

If you simply need non editable kendoComboBox(), you could use kendoDropDownList() instead.

如果您只需要不可编辑的 kendoComboBox(),则可以改用 kendoDropDownList()。

In my case i needed it to be enabled/disabled depending on user privileges, so here's the solution i came up with (this prevents key inputs and pasting values) :

在我的情况下,我需要根据用户权限启用/禁用它,所以这是我想出的解决方案(这可以防止键输入和粘贴值):

if (user.hasRight()) {

  var myinput = $("#myinput").data("kendoComboBox");
  myinput.input.on("keydown", function (e) { e.preventDefault(); });
  myinput.input.bind("paste", function (e) { e.preventDefault(); });

}

You can test it here

你可以在这里测试

回答by Amrendra

jQuery:

jQuery:

    $(this).click(function () {
    $(".txtbox").attr("disabled", "disabled"); 

  });

or css:

或CSS:

.txtbox{
 display: none;
 visibility: hidden;
 }

or html attribute:

或 html 属性:

 set disabled="disabled";