allowing input only for float number

后端 未结 7 542
别那么骄傲
别那么骄傲 2020-12-09 21:58

I have pain-time when making input that only allows float number with jquery library. my code can\'t prevent chacacter \".\" when it\'s becoming first input, can anyone guid

7条回答
  •  感动是毒
    2020-12-09 22:41

    I use this - works for keyboard input or copy and paste

    $('input.float').on('input', function() {
      this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');
    });
    
    

    Explanation:

    • First regex replaces anything that's not a number or a decimal.
    • Second regex removes any instance of a second decimal.

提交回复
热议问题