How to prevent user pasting text in a textbox?

后端 未结 5 765
我寻月下人不归
我寻月下人不归 2020-12-14 16:42

I have this JS function that prevents user from typing characters



        
相关标签:
5条回答
  • 2020-12-14 17:17

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

    0 讨论(0)
  • 2020-12-14 17:22

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

    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

    0 讨论(0)
  • 2020-12-14 17:28

    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.

    0 讨论(0)
  • 2020-12-14 17:31

    I tried this in my Angular project and it worked fine without 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'.

    0 讨论(0)
  • 2020-12-14 17:31

    jQuery:

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

    or css:

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

    or html attribute:

     set disabled="disabled";
    
    0 讨论(0)
提交回复
热议问题