Catch paste input

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

    Script to remove special characters from all fields with class portlet-form-input-field:

    // Remove special chars from input field on paste
    jQuery('.portlet-form-input-field').bind('paste', function(e) {
        var textInput = jQuery(this);
        setTimeout(function() {
            textInput.val(replaceSingleEndOfLineCharactersInString(textInput.val()));
        }, 200);
    });
    
    function replaceSingleEndOfLineCharactersInString(value) {
        <%
            // deal with end-of-line characters (\n or \r\n) that will affect string length calculation,
            // also remove all non-printable control characters that can cause XML validation errors
        %>
        if (value != "") {
            value = value.replace(/(\x00|\x01|\x02|\x03|\x04|\x05|\x06|\x07|\x08|\x0B|\x0C|\x0E|\x0F|\x10|\x11|\x12|\x13|\x14|\x15|\x16|\x17|\x18|\x19|\x1A|\x1B|\x1C|\x1D|\x1E|\x1F|\x7F)/gm,'');
            return value = value.replace(/(\r\n|\n|\r)/gm,'##').replace(/(\#\#)/gm,"\r\n");
        }
    }
    

提交回复
热议问题