简体   繁体   English

jQuery-如何允许数字键盘和数字键行输入; 以及防止输入特殊字符?

[英]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 09的keyCode是4857因此请使用此条件

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.

相关问题 如何限制在输入 texbox 中仅输入空格和特殊字符。 当使用字母输入时,我们需要允许两者 - how to restrict ONLY space and special characters to be entered in the input texbox. We need to allow both when entered with alphabets 如何防止将无效字符输入表单? - How do I prevent invalid characters from being entered into a form? 如何修改此验证逻辑以允许输入字符和一些特殊字符? - How can I modify this validation logic to be able to allow characters and some special characters too to be entered? 如何将 javascript 或 html 的输入限制为仅限字母,但也允许输入空格? - How can i limit the input with javascript or html to letters only, but allow a space to be entered as well? 如何使用Javascript防止在输入上输入除数字以外的其他字符 - How can I prevent other characters from being inputted on an input except numbers using Javascript 防止在文本字段中输入某些字符 - prevent certain characters from being entered in a textfield 如何允许输入中除数字外的所有字符 - How to allow all characters but number from input 如何在我的输入中只允许数字和特殊字符? - How do I allow only numbers and special characters in my input? 如何防止在输入字段中写入和粘贴特殊字符? - how can I prevent write and paste special characters inside input fields? 正则表达式只允许 az 中的任何字符,包括小写和大写、任意数字和特殊字符。 -_ 只要 - Regex to allow only any character from a-z both lower and upper, any number and special characters . -_ only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM