How to allow only numeric (0-9) in HTML inputbox using jQuery?

前端 未结 30 1758
一生所求
一生所求 2020-11-21 05:38

I am creating a web page where I have an input text field in which I want to allow only numeric characters like (0,1,2,3,4,5...9) 0-9.

How can I do this using jQuery

30条回答
  •  执笔经年
    2020-11-21 05:49

    I also would like to answer :)

        $('.justNum').keydown(function(event){
            var kc, num, rt = false;
            kc = event.keyCode;
            if(kc == 8 || ((kc > 47 && kc < 58) || (kc > 95 && kc < 106))) rt = true;
            return rt;
        })
        .bind('blur', function(){
            num = parseInt($(this).val());
            num = isNaN(num) ? '' : num;
            if(num && num < 0) num = num*-1;
            $(this).val(num);
        });
    

    That's it...just numbers. :) Almost it can work just with the 'blur', but...

提交回复
热议问题