Catch paste input

后端 未结 17 1896
名媛妹妹
名媛妹妹 2020-11-22 08:09

I\'m looking for a way to sanitize input that I paste into the browser, is this possible to do with jQuery?

I\'ve managed to come up with this so far:



        
相关标签:
17条回答
  • 2020-11-22 08:23

    How about comparing the original value of the field and the changed value of the field and deducting the difference as the pasted value? This catches the pasted text correctly even if there is existing text in the field.

    http://jsfiddle.net/6b7sK/

    function text_diff(first, second) {
        var start = 0;
        while (start < first.length && first[start] == second[start]) {
            ++start;
        }
        var end = 0;
        while (first.length - end > start && first[first.length - end - 1] == second[second.length - end - 1]) {
            ++end;
        }
        end = second.length - end;
        return second.substr(start, end - start);
    }
    $('textarea').bind('paste', function () {
        var self = $(this);
        var orig = self.val();
        setTimeout(function () {
            var pasted = text_diff(orig, $(self).val());
            console.log(pasted);
        });
    });
    
    0 讨论(0)
  • 2020-11-22 08:23

    This method uses jqueries contents().unwrap().

    1. First, detect the paste event
    2. Add a unique class to the tags that are already in the element into which we are pasting.
    3. After a given timeout scan through all the contents unwrapping tags that don't have the class that you set earlier. Note: This method does not remove self closing tags like
      See an example below.

      //find all children .find('*') and add the class .within .addClass("within") to all tags
      $('#answer_text').find('*').each(function () {
      $(this).addClass("within");
      });
      setTimeout(function() {
      $('#answer_text').find('*').each(function () {
          //if the current child does not have the specified class unwrap its contents
          $(this).not(".within").contents().unwrap();
      });
      }, 0);
      
    0 讨论(0)
  • 2020-11-22 08:24

    Listen for the paste event and set a keyup event listener. On keyup, capture the value and remove the keyup event listener.

    $('.inputTextArea').bind('paste', function (e){
        $(e.target).keyup(getInput);
    });
    function getInput(e){
        var inputText = $(e.target).val();
        $(e.target).unbind('keyup');
    }
    
    0 讨论(0)
  • 2020-11-22 08:24

    See this example: http://www.p2e.dk/diverse/detectPaste.htm

    It essentialy tracks every change with oninput event and then checks if it’s a paste by string comparison. Oh, and in IE there’s an onpaste event. So:

    $ (something).bind ("input paste", function (e) {
        // check for paste as in example above and
        // do something
    })
    
    0 讨论(0)
  • 2020-11-22 08:26

    OK, just bumped into the same issue.. I went around the long way

    $('input').on('paste', function () {
      var element = this;
      setTimeout(function () {
        var text = $(element).val();
        // do something with text
      }, 100);
    });
    

    Just a small timeout till .val() func can get populated.

    E.

    0 讨论(0)
  • 2020-11-22 08:26
     $('').bind('input propertychange', function() {....});                      
    

    This will work for mouse paste event.

    0 讨论(0)
提交回复
热议问题