我正在寻找一种方法来消毒输入,我粘贴到浏览器,这是可能的用jQuery做吗?
到目前为止,我已经想出了这个:
$(this).live(pasteEventName, function(e) {
// this is where i would like to sanitize my input
return false;
}
不幸的是,我的发展因为这个“小”问题而戛然而止。 如果有人能给我指出正确的方向,我真的会让我成为一个快乐的露营者。
我正在寻找一种方法来消毒输入,我粘贴到浏览器,这是可能的用jQuery做吗?
到目前为止,我已经想出了这个:
$(this).live(pasteEventName, function(e) {
// this is where i would like to sanitize my input
return false;
}
不幸的是,我的发展因为这个“小”问题而戛然而止。 如果有人能给我指出正确的方向,我真的会让我成为一个快乐的露营者。
当前回答
这段代码为我工作,要么从右击粘贴或直接复制粘贴
$('.textbox').on('paste input propertychange', function (e) {
$(this).val( $(this).val().replace(/[^0-9.]/g, '') );
})
当我粘贴第1节:劳动力成本时,它在文本框中变成1。
为了只允许浮动值,我使用这段代码
//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();
}
});
其他回答
我通过使用以下代码修复了它:
$("#editor").live('input paste',function(e){
if(e.target.id == 'editor') {
$('<textarea></textarea>').attr('id', 'paste').appendTo('#editMode');
$("#paste").focus();
setTimeout($(this).paste, 250);
}
});
现在我只需要存储插入符号的位置并附加到该位置,然后我就全部设置好了……我认为:)
$('').bind('input propertychange', function() {....});
这将为鼠标粘贴事件工作。
使用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");
}
}
这里有一个警告。在Firefox中,如果您重置每个键上的输入文本,如果文本长于输入宽度允许的可视区域,则重置每个键上的值将破坏浏览器自动将文本滚动到文本末尾的插入号位置的功能。相反,文本将滚动回开始,而插入符号则不在视图中。
function scroll(elementToBeScrolled)
{
//this will reset the scroll to the bottom of the viewable area.
elementToBeScrolled.topscroll = elementToBeScrolled.scrollheight;
}
嗯…我认为你可以使用e.clipboardData来捕捉被粘贴的数据。如果不成功,看看这里。
$(this).live("paste", function(e) {
alert(e.clipboardData); // [object Clipboard]
});