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

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

    Use JavaScript function isNaN,

    if (isNaN($('#inputid').val()))
    

    if (isNaN(document.getElementById('inputid').val()))

    if (isNaN(document.getElementById('inputid').value))
    

    Update: And here a nice article talking about it but using jQuery: Restricting Input in HTML Textboxes to Numeric Values

    0 讨论(0)
  • 2020-11-21 05:58

    Simpler one for me is

    jQuery('.plan_eff').keyup(function () {     
      this.value = this.value.replace(/[^1-9\.]/g,'');
    });
    
    0 讨论(0)
  • 2020-11-21 05:59

    Short and sweet - even if this will never find much attention after 30+ answers ;)

      $('#number_only').bind('keyup paste', function(){
            this.value = this.value.replace(/[^0-9]/g, '');
      });
    
    0 讨论(0)
  • 2020-11-21 05:59
    $(document).ready(function() {
        $("#txtboxToFilter").keydown(function(event) {
            // Allow only backspace and delete
            if ( event.keyCode == 46 || event.keyCode == 8 ) {
                // 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.preventDefault(); 
                }   
            }
        });
    });
    

    Source: http://snipt.net/GerryEng/jquery-making-textfield-only-accept-numeric-values

    0 讨论(0)
  • 2020-11-21 05:59

    You can try the HTML5 number input:

    <input type="number" value="0" min="0"> 
    

    For non-compliant browsers there are Modernizr and Webforms2 fallbacks.

    0 讨论(0)
  • 2020-11-21 05:59

    I wrote mine based off of @user261922's post above, slightly modified so you can select all, tab and can handle multiple "number only" fields on the same page.

    var prevKey = -1, prevControl = '';
    $(document).ready(function () {
        $(".OnlyNumbers").keydown(function (event) {
            if (!(event.keyCode == 8                                // backspace
                || event.keyCode == 9                               // tab
                || event.keyCode == 17                              // ctrl
                || 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.keyCode == 65 && prevKey == 17 && prevControl == event.currentTarget.id))          // ctrl + a, on same control
            ) {
                event.preventDefault();     // Prevent character input
            }
            else {
                prevKey = event.keyCode;
                prevControl = event.currentTarget.id;
            }
        });
    });
    
    0 讨论(0)
提交回复
热议问题