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

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

    function suppressNonNumericInput(event){

        if( !(event.keyCode == 8                                // backspace
            || event.keyCode == 46                              // delete
            || (event.keyCode >= 35 && event.keyCode <= 40)     // arrow keys/home/end
            || (event.keyCode >= 48 && event.keyCode <= 57)     // numbers on keyboard
            || (event.keyCode >= 96 && event.keyCode <= 105))   // number on keypad
            ) {
                event.preventDefault();     // Prevent character input
        }
    }
    
    0 讨论(0)
  • 2020-11-21 06:01

    You would want to allow tab:

    $("#txtboxToFilter").keydown(function(event) {
        // Allow only backspace and delete
        if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 ) {
            // let it happen, don't do anything
        }
        else {
            // Ensure that it is a number and stop the keypress
            if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
                event.preventDefault(); 
            }   
        }
    });
    
    0 讨论(0)
  • 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<checkdot.length;i++){
                if(checkdot[i]=='.')return false;
            }
            if(checkdot.length==0)document.getElementById('price').value='0';
            return true;
        }
        keychar = String.fromCharCode(keynum);
    
        return !isNaN(keychar);
    }
    

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

    0 讨论(0)
  • 2020-11-21 06:03

    This seems unbreakable.

    // Prevent NULL input and replace text.
    $(document).on('change', 'input[type="number"]', function (event) {
        this.value = this.value.replace(/[^0-9\.]+/g, '');
        if (this.value < 1) this.value = 0;
    });
    
    // Block non-numeric chars.
    $(document).on('keypress', 'input[type="number"]', function (event) {
        return (((event.which > 47) && (event.which < 58)) || (event.which == 13));
    });
    
    0 讨论(0)
  • 2020-11-21 06:04

    I think it will help everyone

      $('input.valid-number').bind('keypress', function(e) { 
    return ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
      })
    
    0 讨论(0)
  • 2020-11-21 06:06

    You can use this JavaScript function:

    function maskInput(e) {
        //check if we have "e" or "window.event" and use them as "event"
            //Firefox doesn't have window.event 
        var event = e || window.event 
    
        var key_code = event.keyCode;
        var oElement = e ? e.target : window.event.srcElement;
        if (!event.shiftKey && !event.ctrlKey && !event.altKey) {
            if ((key_code > 47 && key_code < 58) ||
                (key_code > 95 && key_code < 106)) {
    
                if (key_code > 95)
                     key_code -= (95-47);
                oElement.value = oElement.value;
            } else if(key_code == 8) {
                oElement.value = oElement.value;
            } else if(key_code != 9) {
                event.returnValue = false;
            }
        }
    }
    

    And you can bind it to your textbox like this:

    $(document).ready(function() {
        $('#myTextbox').keydown(maskInput);
    });
    

    I use the above in production, and it works perfectly, and it is cross-browser. Furthermore, it does not depend on jQuery, so you can bind it to your textbox with inline JavaScript:

    <input type="text" name="aNumberField" onkeydown="javascript:maskInput()"/>
    
    0 讨论(0)
提交回复
热议问题