允许JQuery代码中使用十进制

允许JQuery代码中使用十进制,jquery,Jquery,我有以下脚本: <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script&g

我有以下脚本:

<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

 $(function () {
    $("#<%= txtAmount.ClientID %>").keydown(function (e) {
        if (e.shiftKey || e.ctrlKey || e.altKey) {
            e.preventDefault();
        } else {
            var key = e.keyCode;
            if (!((key == 8) || (key == 46) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105))) {
                e.preventDefault();
            }
        }
    });

$(函数(){
$(“#”).keydown(函数(e){
if(e.shiftKey | | e.ctrlKey | | e.altKey){
e、 预防默认值();
}否则{
var key=e.keyCode;
如果(!((key==8)| |(key==46)| |(key>=35&&key=48&&key=96&&key这里有一个块:

$(document).ready(function () {
  //called when key is pressed in textbox
  $("#quantity").keypress(function (e) {
     //if the letter is not digit then display error and don't type anything
     if (e.which != 8 && e.which != 0 && e.which.indexOf('.') != -1 && (e.which < 48 || e.which > 57)) {
        //display error message
        $("#errmsg").html("Digits Only").show().fadeOut("slow");
               return false;
    }
   });


});
$(文档).ready(函数(){
//在文本框中按下键时调用
$(“#数量”)。按键(功能(e){
//如果字母不是数字,则显示错误,不键入任何内容
如果(e.which!=8&&e.which!=0&&e.which.indexOf('.')!=-1&&e.which<48 | e.which>57)){
//显示错误消息
$(“#errmsg”).html(“仅限数字”).show().fadeOut(“慢”);
返回false;
}
});
});

对于您的代码:

$(function () {
    $("#<%= txtAmount.ClientID %>").keydown(function (e) {
         //if the letter is not digit then display error and don't type anything
         if (e.which != 8 && e.which != 0  && e.which.indexOf('.') != -1 && (e.which < 48 || e.which > 57)) {
            //display error message
            $("#errmsg").html("Digits Only").show().fadeOut("slow");
                   return false;
        }
    });

   });
$(函数(){
$(“#”).keydown(函数(e){
//如果字母不是数字,则显示错误,不键入任何内容
如果(e.which!=8&&e.which!=0&&e.which.indexOf('.')!=-1&&e.which<48 | e.which>57)){
//显示错误消息
$(“#errmsg”).html(“仅限数字”).show().fadeOut(“慢”);
返回false;
}
});
});

似乎应该有一种更简单的方法来做到这一点

$("#<%= txtAmount.ClientID %>").keypress(function (e) {
    if (!String.fromCharCode(e.which).match(/[\d.]/)) e.preventDefault();
});
$(“#”)按键(功能(e){
如果(!String.fromCharCode(e.which).match(/[\d.]/)e.preventDefault();
});

它到底在哪里允许190?我对这句话的理解是“如果。。。。(key>=96&&key这不起作用。我在原始帖子中添加了脚本声明,以防我的目标版本不正确。在Fiddle中工作,但在我的网页上不起作用?我尝试将其更改为1.11.0版,但也不起作用。@Computer-如果它在Fiddle中工作,但在你的网页上不起作用,那么你这方面显然出了问题,因为它只是一个简单的正则表达式,不会有太多的错误?这很奇怪,键盘布局限制了主布局的小数点,但右边的数字布局允许小数??顶部代码允许底部代码几乎可以工作的所有字符,但不允许小数。我在我的原始帖子中发布了声明我的代码只允许数字,这不是你问的问题吗?不,我的要求是“只允许数字和十进制字符”,意思是你希望
被允许?是的,这就是我在12.34之后所做的,但我不希望允许任何字符(即a-Z,a-Z)或任何特殊字符(!“£$%%等)