Catch paste input

后端 未结 17 2126
名媛妹妹
名媛妹妹 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: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();
          }
        });
    

提交回复
热议问题