代码之家  ›  专栏  ›  技术社区  ›  Dirk J. Faber

jQuery测试00、0.0和0.0

  •  1
  • Dirk J. Faber  · 技术社区  · 7 年前

    我有一个输入框 currency 如果另一个字段上的值为 fee 为空或零。

    HTML

    Fee: <input id="fee"><br>
    Currency: <input id="currency"><br>
    

    javascript

    $("#fee").on('input', function() {
    $('#currency').prop('disabled', (this.value === '0' || this.value === ''));  
    })
    .trigger('input');
    

    JSFiddle

    我现在想达到的是 货币 如果有人进入,也会被禁用 00 0.0 0,0 目前情况并非如此。我要测试两个A . 和A , 在我的字段中,因为用户可以输入两个值作为分隔符的数字。

    我通过改变 this.value === '0' this.value < '0.000000001' || this.value < '0,000000001' 但是它仍然不能与 00 . 还有一些像 0,1 将被禁用(它不应该禁用),这是因为逗号 , 哪个不适用于数字?

    我的问题是否有一个简单(或困难)的解决方案?

    编辑 澄清:现场 货币 应在用户进入时禁用 0 , 00 (或任何数量的零), (或更多的零)和 0,0 (或更多的零)。如果用户输入类似 09 0,5 0.1 .

    2 回复  |  直到 7 年前
        1
  •  3
  •   Louys Patrice Bessette    7 年前

    isNaN()

    $("#fee").on('input', function() {
    
      // Replace all coma by dot and parse as a float number
      var value = parseFloat(this.value.replace(",","."));
    
      $('#currency').prop('disabled', ( value === 0 || isNaN(value) ));  
    })
    .trigger('input');
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    Fee: <input id="fee"><br>
    Currency: <input id="currency"><br>
        2
  •  1
  •   Serkan Taş    7 年前

     $("#fee").on('input', function() {
       $('#currency').prop('disabled', (this.value.replace(/[0\+\.,]+/,'') == 0));  
      }).trigger('input');