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

前端 未结 30 1770
一生所求
一生所求 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 06:06

    Here is a quick solution I created some time ago. you can read more about it in my article:

    http://ajax911.com/numbers-numeric-field-jquery/

    $("#textfield").bind("keyup paste", function(){
        setTimeout(jQuery.proxy(function() {
            this.val(this.val().replace(/[^0-9]/g, ''));
        }, $(this)), 0);
    });
    

提交回复
热议问题