How to block +,-,e in input type Number?

后端 未结 13 1778
我在风中等你
我在风中等你 2020-12-29 20:52

When I\'m giving input type number the letter e and special charecters are also displaying in input field. I want to display only digits. How to block them?

相关标签:
13条回答
  • 2020-12-29 21:32

    Instead on trying to block values, you can try to replace values that are non numeric.

    If you choose to handle keycodes, you will have to handle numKeys, numPad, shift +, crtl + etc and trying to refresh while focus is inside textbox will also fail. Prevent Default will stop lot more than incorrect values.

    $("#input").on("input", function() {
      var nonNumReg = /[^0-9]/g
      $(this).val($(this).val().replace(nonNumReg, ''));
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="tel" id="input" />
    <div class="alert"></div>

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