[英]jQuery - How can I allow both numpad and number key row input; as well as prevent special characters from being entered?
I want to allow both number pad numeric and oridinary number from keyboard. 我想同时允许键盘上的数字键盘和普通数字。 Currently, it only allows the number key row. 当前,它仅允许数字键行。 I also only want numbers to be entered, but it currently allows special characters. 我也只想输入数字,但目前允许特殊字符。
$(document).ready(function() { $('#price').keydown(function(event) { if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57) && event.which != 8) { event.preventDefault(); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type='text' id='price'>
Have a look at this plug-in (Fork of texotela's numeric jquery plugin). 看一下这个插件(texotela的数字jquery插件的分支)。 This (jStepper) is another one. 这(jStepper)是另一个。
This is a link if you want to build it yourself. 如果您想自己构建它,这是一个链接 。
$(document).ready(function() {
$("#txtboxToFilter").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, Command+A
(e.keyCode === 65 && (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();
}
});
});
NOTE: If your webpage uses HTML5, you can use the built-in <input type="number">
and use the min and max properties to control the minimum and maximum value. 注意:如果您的网页使用HTML5,则可以使用内置的<input type="number">
并使用min和max属性来控制最小值和最大值。
$(function() {
$('#staticParent').on('keydown', '#child', function(e){-1!==$.inArray(e.keyCode,[46,8,9,27,13,110,190])||(/65|67|86|88/.test(e.keyCode)&&(e.ctrlKey===true||e.metaKey===true))&&(!0===e.ctrlKey||!0===e.metaKey)||35<=e.keyCode&&40>=e.keyCode||(e.shiftKey||48>e.keyCode||57<e.keyCode)&&(96>e.keyCode||105<e.keyCode)&&e.preventDefault()});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="staticParent">
<input id="child" type="textarea" />
</div>
I coppied from it on here 我从这里开始
If its help for you.Please vote first writer . 如果它对你有帮助,请首先投票。
Updated your code 更新了您的代码
$(document).ready(function() {
$('#price').keydown(function(event) {
if((event.which >= 48 && event.which <= 57) && event.shiftKey) {
event.preventDefault();
}
if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57) && event.which != 8) {
event.preventDefault();
}
});
});
Use keypress for that instead of keydown 使用按键代替按键
then 然后
the keyCode of 0
to 9
are 48
to 57
so use this condition 0
到9
的keyCode是48
到57
因此请使用此条件
if (event.keyCode < 48 || event.keyCode > 57) {
event.preventDefault();
}
this condition returns true if you enter keys that are not 0, 1, 2, 3, 4, 5, 6, 7 ,8 and 9 or not numbers 如果您输入的键不是0、1、2、3、4、5、6、7、8和9或非数字,则此条件返回true
So your code would be like this 所以你的代码会像这样
$(document).ready(function() { $('#price').keypress(function(event) { if (event.keyCode < 48 || event.keyCode > 57) { event.preventDefault(); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type='text' id='price'>
Why we don't user input type number. 为什么我们不输入类型号。 I think just do it easy like this: 我想这样做很容易:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type='number' id='price'>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.