How to prevent keypress two digits after a decimal number?

后端 未结 5 1977
花落未央
花落未央 2020-12-31 10:06

I have got a task to prevent keypress two digits after a decimal number. My jquery file is

$(function(){ 
    $(\'#name\').bind(\'paste\', function(){
             


        
5条回答
  •  离开以前
    2020-12-31 10:33

    I did it this way: Provided a class allow-only-numbers, for your input then:

      var numberOfDecimals = 2;
      $(document).on("input", ".allow-only-numbers", function () {
        var regExp = new RegExp('(\\.[\\d]{' + numberOfDecimals + '}).', 'g')    
        this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1').replace(regExp, '$1');
    });
    

提交回复
热议问题