Javascript 只允许数字和 ctrl+a 、 ctrl+v 、 ctrl+c 到文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33251052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Allow only numbers and ctrl+a , ctrl+v , ctrl+c to a textbox
提问by Suresh Pattu
I am trying to allow users enter only numbers and copy and paste control to the textbox. I am able to restrict user to enter only numbers but copy,paste is not working for me help me to fix this.
Here is my script:
我试图让用户只输入数字并将控制复制和粘贴到文本框。我可以限制用户只输入数字,但复制、粘贴对我不起作用帮助我解决这个问题。
这是我的脚本:
$(".allow_only_numbers").keydown(function (e) {
var ctrlDown = false;
var ctrlKey = 17, vKey = 86, cKey = 67;
if (e.keyCode === ctrlKey){
ctrlDown = true;
}
// Allow: backspace, delete, tab, escape, enter and .
if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110]) !== -1 ||
// Allow: Ctrl
(e.keyCode === ctrlKey) ||
// Allow: Ctrl+A
(e.keyCode === 65 && e.ctrlKey === true) ||
// Allow: Ctrl+v
(e.keyCode === vKey && ctrlDown) ||
// Allow: Ctrl+c
(e.keyCode === cKey && ctrlDown) ||
// Allow: home, end, left, right, down, up
(e.keyCode >= 35 && e.keyCode <= 40)) {
// let it happen, don't do anything
return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
});
Here is the jsfiddle link:
https://jsfiddle.net/sureshpattu/stwzhceL/1/
这是 jsfiddle 链接:https://jsfiddle.net/sureshpattu/stwzhceL/1/
回答by John R
Try with event.keyCode
and event.metaKey
like this.
尝试event.keyCode
并event.metaKey
喜欢这个。
$(document).ready(function() {
$(".allow_only_numbers").keydown(function(e) {
// Allow: backspace, delete, tab, escape, enter and .
if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
// Allow: Ctrl+A,Ctrl+C,Ctrl+V, Command+A
((e.keyCode == 65 || e.keyCode == 86 || e.keyCode == 67) && (e.ctrlKey === true || e.metaKey === true)) ||
// Allow: home, end, left, right, down, up
(e.keyCode >= 35 && e.keyCode <= 40)) {
// let it happen, don't do anything
return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<input type="number" class="allow_only_numbers" />
EDIT:
编辑:
Remove the following code snippets from your code.
从您的代码中删除以下代码片段。
var ctrlDown = false;
var ctrlKey = 17, vKey = 86, cKey = 67;
if (e.keyCode === ctrlKey) {
ctrlDown = true;
}
Because ctrlDown
will be false
while pressing C
and V
for copy and paste. And hence your ctr+c
and ctrl+v
is not working.
由于ctrlDown
将false
同时按下C
并V
用于复制和粘贴。因此,您的ctr+c
和ctrl+v
不起作用。
回答by Shanimal
$(".allow_only_numbers").on("input",function (e) {
e.target.value = e.target.value.replace(/[^0-9]/g,'')
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="allow_only_numbers" />
回答by suish
$(".allow_only_numbers").keydown(function (e) {
var isModifierkeyPressed = (e.metaKey || e.ctrlKey || e.shiftKey);
var isCursorMoveOrDeleteAction = ([46,8,37,38,39,40].indexOf(e.keyCode) != -1);
var isNumKeyPressed = (e.keyCode >= 48 && e.keyCode <= 58) || (e.keyCode >=96 && e.keyCode <= 105);
var vKey = 86, cKey = 67,aKey = 65;
switch(true){
case isCursorMoveOrDeleteAction:
case isModifierkeyPressed == false && isNumKeyPressed:
case (e.metaKey || e.ctrlKey) && ([vKey,cKey,aKey].indexOf(e.keyCode) != -1):
break;
default:
e.preventDefault();
}
});
here is working example
这是工作示例
https://jsfiddle.net/stwzhceL/8/
https://jsfiddle.net/stwzhceL/8/
this allow you ctrl+a
,ctrl+c
and ctrl+v
as well as arrow keys,delete key and backspace key.
这允许您ctrl+a
,ctrl+c
并且ctrl+v
还有方向键,删除键和退格键。
plus this code covers mac as well.(which is cmd+a/cmd+c/cmd+v
)
加上这段代码也涵盖了mac。(这是cmd+a/cmd+c/cmd+v
)
回答by Gacci
That should give you an idea. Sorry I am on a Mac and as far as I know the e.ctrlKey and the like don't work on Mac.
那应该给你一个想法。对不起,我在 Mac 上,据我所知 e.ctrlKey 等在 Mac 上不起作用。
var mod;
var ouput = jQuery('#output');
var numeric = jQuery('#numeric').keydown(function(e){
mod = e.which;
if(!(e.which >= 48 && e.which <= 57) && (e.which != 8)
&& (mod && (e.which == 67 || e.which != 86))){
e.preventDefault();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' id='numeric'/>
<label id='output'></label>
回答by johan indra Permana
From @Shanimal answer. this is my angular js directive. to use only number to your input.
来自@Sanimal 的回答。这是我的 angular js 指令。只使用数字输入。
.directive('onlyNumber', function() {
return {
restrict: 'A',
link: function (scope, elm, attrs, ctrl) {
elm.on("propertychange input",function (event) {
event.target.value = event.target.value.replace(/[^0-9]/g,'');
});
}
};
})
just use 'only-number' to your element. example :
只需对您的元素使用“only-number”即可。例子 :
<input type="text" data-ng-model="number" placeholder="number"id="cifNumber" only-number/>
回答by Moishe Lipsker
If you change your keydown
event to a keyup
event, you can use e.ctrlKey
, which will be true
when ctrl
key is pressed.
如果您将keydown
事件更改为keyup
事件,则可以使用e.ctrlKey
,这将true
在ctrl
按下键时发生。
回答by kfreeman04208
function numbersonly(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}