jQuery - Filter Copy Paste

In this example if the user copy-paste a value in an input field where the class contains 'price', the input is filtered except for decimals, dot, command and minus sign.

function filterCopyPaste() {
    $('input').bind("paste input", function(e) {
        var element = this;
        let className = $(this).attr('class');
        if (className.includes('price')) {
                setTimeout(function () {
                        let val = $(e.currentTarget).val();
                        val = val.replace(/[^\d.,-]/g, '');
                        $(element).val(val);
                }, 0);
        }
        $(this).val(val);
    });
}

setTimeout is required to prevent duplicate content in Safari.

Sometimes you need to call filterCopyPaste after adding dynamic content or add the onpaste attribute:

onpaste="filterCopyPaste()"