【问题标题】:Set an input field to accept only numbers with jQuery使用 jQuery 设置输入字段以仅接受数字
【发布时间】:2016-10-27 18:20:28
【问题描述】:

我必须定义一个只接受整数的输入文本。

我尝试使用正则表达式,但值的小数部分仍然可见。

我用过这个功能:

$(document).on("input","input", function(e) {
    this.value = this.value.replace(/[^\d\\-]/g,'');
})

【问题讨论】:

标签: javascript jquery


【解决方案1】:

这个怎么样?

$('input').on('input blur paste', function(){
 $(this).val($(this).val().replace(/\D/g, ''))
})
<input>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

【讨论】:

    【解决方案2】:

    简单易用。试试看: 编辑:还调用onblur 事件以防止粘贴。

    function numOnly(selector){
      selector.value = selector.value.replace(/[^0-9]/g,'');
    }
    &lt;input type="text" onkeyup="numOnly(this)" onblur="numOnly(this)"&gt;

    【讨论】:

    • 右键点击粘贴不会触发keyup。
    • 谢谢@Archer,我之前没想到。现在它是固定的。立即检查。
    【解决方案3】:

    如果是数字,您可以在 keydown 上检测,如果是数字,您可以检查粘贴事件并删除所有非数字。这也删除了点。

    原文出处:keydown detectPaste remove

    以下代码已从原始源中修改。

    请尝试:

    $( "#input" ).on("paste", function() {  
        setTimeout(function(){
        if ($('#input').val() != $('#input').val().replace(/\D/g,"")) 
        { 
          $('#input').val($('#input').val().replace(/\D/g,""));
        }
        },10)
    });
    
    $('#firstrow').on('keydown', '#input', function(e){-1!==$.inArray(e.keyCode,[46,8,9,27,13])||/65|67|86|88/.test(e.keyCode)&&(!0===e.ctrlKey||!0===e.metaKey)||35<=e.keyCode&&40>=e.keyCode||(e.shiftKey||48>e.keyCode||57<e.keyCode)&&(96>e.keyCode||105<e.keyCode)&&e.preventDefault()});
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="firstrow">
      <input id="input" type="text">
    </div>

    【讨论】:

      【解决方案4】:
      Finally I solved the above issue with my requirement to type only number is
      
      $(document).on("input", ".skilltxt", function(e) {
                      this.value = this.value.replace(/[^\d]/g,'');
                   });
                   $(".skilltxt").on('paste',function(e) {
      
                          if(gBrowser=='IE') {
                              var clipboardData, pastedData;
                               clipboardData = e.clipboardData || window.clipboardData;
                                pastedData = clipboardData.getData('Text');
      
                          }
                          else if((gBrowser=='Firefox')|| (gBrowser=='Chrome')){ 
                                var pastedData = (e.originalEvent || e).clipboardData.getData('text/plain');
                                  window.document.execCommand('insertText', false, pastedData)
                          }
                      if(Math.floor(pastedData) == pastedData && $.isNumeric(pastedData)){
                         $(this).val($(this).val().replace(/[^\d]/g,''));
                        }
                        else{
                         return false;
                        }
                   });
      

      【讨论】:

        猜你喜欢
        • 2017-05-18
        • 1970-01-01
        • 2021-06-13
        • 2016-10-08
        • 2012-06-07
        • 2014-06-26
        • 2013-08-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多