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

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

    I wanted to help a little, and I made my version, the onlyNumbers function...

    function onlyNumbers(e){
        var keynum;
        var keychar;
    
        if(window.event){  //IE
            keynum = e.keyCode;
        }
        if(e.which){ //Netscape/Firefox/Opera
            keynum = e.which;
        }
        if((keynum == 8 || keynum == 9 || keynum == 46 || (keynum >= 35 && keynum <= 40) ||
           (event.keyCode >= 96 && event.keyCode <= 105)))return true;
    
        if(keynum == 110 || keynum == 190){
            var checkdot=document.getElementById('price').value;
            var i=0;
            for(i=0;i

    Just add in input tag "...input ... id="price" onkeydown="return onlyNumbers(event)"..." and you are done ;)

提交回复
热议问题