Catch paste input

后端 未结 17 1910
名媛妹妹
名媛妹妹 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:28

    For cross platform compatibility, it should handle oninput and onpropertychange events:

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

    I sort of fixed it by using the following code:

    $("#editor").live('input paste',function(e){
        if(e.target.id == 'editor') {
            $('<textarea></textarea>').attr('id', 'paste').appendTo('#editMode');
            $("#paste").focus();
            setTimeout($(this).paste, 250);
        }
    });
    

    Now I just need to store the caret location and append to that position then I'm all set... I think :)

    0 讨论(0)
  • 2020-11-22 08:31

    This proved to be quite illusive. The value of the input is not updated prior to the execution of the code inside the paste event function. I tried calling other events from within the paste event function but the input value is still not updated with the pasted text inside the function of any events. That is all events apart from keyup. If you call keyup from within the paste event function you can sanitize the pasted text from within the keyup event function. like so...

    $(':input').live
    (
        'input paste',
        function(e)
        {
            $(this).keyup();
        }
    );
    
    $(':input').live
    (
        'keyup',
        function(e)
        {
            // sanitize pasted text here
        }
    );
    

    There is one caveat here. In Firefox, if you reset the input text on every keyup, if the text is longer than the viewable area allowed by the input width, then resetting the value on every keyup breaks the browser functionality that auto scrolls the text to the caret position at the end of the text. Instead the text scrolls back to the beginning leaving the caret out of view.

    0 讨论(0)
  • 2020-11-22 08:32

    You can actually grab the value straight from the event. Its a bit obtuse how to get to it though.

    Return false if you don't want it to go through.

    $(this).on('paste', function(e) {
    
      var pasteData = e.originalEvent.clipboardData.getData('text')
    
    });
    
    0 讨论(0)
  • 2020-11-22 08:32

    This code is working for me either paste from right click or direct copy paste

       $('.textbox').on('paste input propertychange', function (e) {
            $(this).val( $(this).val().replace(/[^0-9.]/g, '') );
        })
    

    When i paste Section 1: Labour Cost it becomes 1 in text box.

    To allow only float value i use this code

     //only decimal
        $('.textbox').keypress(function(e) {
            if(e.which == 46 && $(this).val().indexOf('.') != -1) {
                e.preventDefault();
            } 
           if (e.which == 8 || e.which == 46) {
                return true;
           } else if ( e.which < 48 || e.which > 57) {
                e.preventDefault();
          }
        });
    
    0 讨论(0)
  • 2020-11-22 08:32
    document.addEventListener('paste', function(e){
        if(e.clipboardData.types.indexOf('text/html') > -1){
            processDataFromClipboard(e.clipboardData.getData('text/html'));
            e.preventDefault();
    
            ...
        }
    });
    

    Further:

    • Clipboard API and events
    0 讨论(0)
提交回复
热议问题