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

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

    The pattern attribute in HTML5 specifies a regular expression that the element's value is checked against.

      <input  type="text" pattern="[0-9]{1,3}" value="" />
    

    Note: The pattern attribute works with the following input types: text, search, url, tel, email, and password.

    • [0-9] can be replaced with any regular expression condition.

    • {1,3} it represents minimum of 1 and maximum of 3 digit can be entered.

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

    Here is the function I use:

    // Numeric only control handler
    jQuery.fn.ForceNumericOnly =
    function()
    {
        return this.each(function()
        {
            $(this).keydown(function(e)
            {
                var key = e.charCode || e.keyCode || 0;
                // allow backspace, tab, delete, enter, arrows, numbers and keypad numbers ONLY
                // home, end, period, and numpad decimal
                return (
                    key == 8 || 
                    key == 9 ||
                    key == 13 ||
                    key == 46 ||
                    key == 110 ||
                    key == 190 ||
                    (key >= 35 && key <= 40) ||
                    (key >= 48 && key <= 57) ||
                    (key >= 96 && key <= 105));
            });
        });
    };
    

    You can then attach it to your control by doing:

    $("#yourTextBoxName").ForceNumericOnly();
    
    0 讨论(0)
  • 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...

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

    Why so complicated? You don't even need jQuery because there is a HTML5 pattern attribute:

    <input type="text" pattern="[0-9]*">
    

    The cool thing is that it brings up a numeric keyboard on mobile devices, which is way better than using jQuery.

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

    Need to make sure you have the numeric keypad and the tab key working too

     // 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)) {
    
                    }
                    else {
                        event.preventDefault();
                    }
                }
    
    0 讨论(0)
  • 2020-11-21 05:51

    Inline:

    <input name="number" onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')">

    Unobtrusive style (with jQuery):

    $('input[name="number"]').keyup(function(e)
                                    {
      if (/\D/g.test(this.value))
      {
        // Filter non-digits from input value.
        this.value = this.value.replace(/\D/g, '');
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input name="number">

    0 讨论(0)
提交回复
热议问题