Jquery 检查值的长度,如果是数字

Jquery 检查值的长度,如果是数字,jquery,Jquery,我使用以下代码检查文本框中的值是否为数字: (function ($) { $.fn.numeric = function (options) { return this.each(function () { var $this = $(this); $this.keypress(options, function (e) { // allow backspace and delete

我使用以下代码检查文本框中的值是否为数字:

(function ($) {
    $.fn.numeric = function (options) {
        return this.each(function () {
            var $this = $(this);

            $this.keypress(options, function (e) {
                // allow backspace and delete
                if (e.which == 8 || e.which == 0)
                    return true;

                //if the letter is not digit
                if (e.which < 48 || e.which > 57)
                    return false;

                // check max range
                var dest = e.which - 48;
                var result = this.value + dest.toString();
                if (result > e.data.max) {
                    return false;
                }
            });
        });
    };
})(jQuery);
问题是它正在处理输入并检查它 值应为整数,而不是字符串

但我需要更改代码,使其也能双重验证

所以如果我插入0.22就可以了,但是如果我插入0.22 它不会填充数字


请帮助检查此答案和评论(尤其是已接受的答案):


否则,是否真的有必要检查每个按键的值?

我尝试调试代码。您应该只需要更新“.”。“.”(点)的ASCII值为46。如果您做以下更改,它将工作

  • 更新
    (e.which==8 | e.which==0)
    (e.which==8 | e.which==0 | e.which==46)
  • 您需要使用引号包装输入-
    $('input')。数值({max:999})
  • 通过使用代码
    alert(parseInt($('input').val())警告值进行测试

  • 我看到上面的更改解决了这个问题:)请,如果它不起作用,请告诉我:)

    这段代码应该按照您的预期工作:

    (function($) {
        $.fn.numeric = function(options) {
            return this.each(function() {
                var $this = $(this);
    
                $this.keypress(options, function(e) {
                    // allow backspace and delete
                    if (e.which == 8 || e.which == 0) {
                        return true;
                    }
    
                    // allow float
                    if (e.which == 46 && this.value.length >=1 && this.value.indexOf('.') == -1) {
                        return true;
                    }
    
                    //if the letter is not digit
                    if (e.which < 48 || e.which > 57) return false;
    
                    // check max range
                    var dest = e.which - 48;
                    var result = this.value + dest.toString();
                    if (result > e.data.max) {
                        return false;
                    }
                });
            });
        };
    })(jQuery);
    
    (函数($){
    $.fn.numeric=函数(选项){
    返回此值。每个(函数(){
    var$this=$(this);
    $this.keypress(选项,功能(e){
    //允许退格和删除
    如果(e.which==8 | | e.which==0){
    返回true;
    }
    //允许浮动
    如果(e.which==46&&this.value.length>=1&&this.value.indexOf('.')==1){
    返回true;
    }
    //如果字母不是数字
    如果(e.which<48 | | e.which>57)返回false;
    //检查最大范围
    var dest=e,其中-48;
    var result=this.value+dest.toString();
    如果(结果>e.data.max){
    返回false;
    }
    });
    });
    };
    })(jQuery);
    

    演示:

    在每个
    keypress
    事件上进行验证可能会很麻烦,并且仅从按键返回false不会让用户知道为什么不允许输入该键。您可能会在标签中将字段标记为数字或类似的内容,但是您可以向用户提供任何额外的帮助,即使用户只是您自己,也可以节省很多麻烦。像这样的怎么样

    (function($) {
        $.fn.numeric = function(options) {
            return this.each(function () {
                var $this = $(this);
    
                $this.blur(function(e) {
                    if (isNaN(+this.value)) {
                        $('<span class="error">Must be numeric</span>').insertAfter($this);
                    } else {
                        $this.next('span.error').remove();
                    }
                });
            });
        };
    }(jQuery));
    
    (函数($){
    $.fn.numeric=函数(选项){
    返回此。每个(函数(){
    var$this=$(this);
    $this.blur(函数(e){
    if(isNaN(+此值)){
    $(“必须是数字”).insertAfter($this);
    }否则{
    $this.next('span.error').remove();
    }
    });
    });
    };
    }(jQuery));
    
    有一个

    此外,您还需要从选项中重新添加最小/最大检查。为了这个例子,我删除了它


    最后,如果您要进行大量验证,您可能希望使用类似的验证库,而不是使用自己的验证库。

    我遵循@Guillaume建议的解决方案。但我增加了附加条件

     if (this.value.length > e.data.maxLength)
                return false;
    
    下面是所有代码

    (function ($) {
    
        $.fn.numeric = function (options) {
    
            return this.each(function () {
                var $this = $(this);
    
                $this.keypress(options, function (e) {
    
                    // allow backspace and delete
                    if (e.which == 8 || e.which == 0)
                        return true;
    
                    if (e.which == 46 && this.value.length >= 1 && this.value.indexOf('.') == -1) {
                        return true;
                    }
                    //if the letter is not digit
                    if (e.which < 48 || e.which > 57)
                        return false;
    
                    // check max range
                    var dest = e.which - 48;
                    var result = this.value + dest.toString();
                    if (result > e.data.max) {
                        return false;
                    }
    
                    if (this.value.length > e.data.maxLength)
                        return false;
                });
            });
        };
    })(jQuery);
    

    maxLength-属性表示最大输入长度

    更好。但是脚本有几个问题,主要的问题是value.22允许但不应该
    (function ($) {
    
        $.fn.numeric = function (options) {
    
            return this.each(function () {
                var $this = $(this);
    
                $this.keypress(options, function (e) {
    
                    // allow backspace and delete
                    if (e.which == 8 || e.which == 0)
                        return true;
    
                    if (e.which == 46 && this.value.length >= 1 && this.value.indexOf('.') == -1) {
                        return true;
                    }
                    //if the letter is not digit
                    if (e.which < 48 || e.which > 57)
                        return false;
    
                    // check max range
                    var dest = e.which - 48;
                    var result = this.value + dest.toString();
                    if (result > e.data.max) {
                        return false;
                    }
    
                    if (this.value.length > e.data.maxLength)
                        return false;
                });
            });
        };
    })(jQuery);
    
     $(input).numeric({ max: 999,maxLength:4});