使用jQuery禁用文本字段中字母的复制粘贴 [英] Disable copy paste of alphabets in text field using jquery
问题描述
在我的项目中,我的文本字段仅取数字值,但是当我使用ctl + c复制字母并使用ctl + v粘贴字母时,它将允许文本字段中的字母,因此我禁用了复制和粘贴使用以下代码.
In my project i have text field that only take numeric value.but when I copy an alphabets using ctl+c and paste using ctl+v it will allow the alphabets in the text field.So I disable the the copy and paste using the following code.
$('input').bind('copy paste', function(e) {
e.preventDefault();
});
但是我只想防止使用字母,这意味着我要复制粘贴的数值而不是字母.
But I want to prevent only alphabets.That means I want copy paste numeric value not alphabets.
推荐答案
尝试使用此功能.这可能不是您要查找的内容,但是您可以执行一些操作.我之前已经做过并将其发布在我的博客上.
Try this function. This may not be what you are looking for but you can do something out of this. I have done this earlier and posted on my blog.
JS:
$(function(){
$(".numericOnly").bind('keypress',function(e){
if(e.keyCode == '9' || e.keyCode == '16'){
return;
}
var code;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
if(e.which == 46)
return false;
if (code == 8 || code == 46)
return true;
if (code < 48 || code > 57)
return false;
}
);
$(".numericOnly").bind("paste",function(e) {
e.preventDefault();
});
$(".numericOnly").bind('mouseenter',function(e){
var val = $(this).val();
if (val!='0'){
val=val.replace(/[^0-9]+/g, "")
$(this).val(val);
}
});
});
HTML:
<body>
<input type="text" id="textBox" class="numericOnly" />
</body>
这将允许您仅输入数字值.您甚至无法复制粘贴并拖放.
This will allow you to enter only numeric values only. You can't even copy paste and drag drop.
DEMO
Code Link
这篇关于使用jQuery禁用文本字段中字母的复制粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!