Validate input decimals with comma

0

I would like to validate decimals with a comma, or how to add a comma to the allowed characters with this validation, I could validate integers but not decimals (with a comma)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="enteros" type=text />
<input class="decimales" type=text />

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


$(function () {
                $('.decimales').on('input', function () {
                    this.value = this.value.replace(/[^0-9]/g, '');
                });
            });
    
asked by Andres 17.01.2018 в 16:27
source

1 answer

0

Simply add the , to the characters in the regular expression:

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

  $('.decimales').on('input', function () {
        this.value = this.value.replace(/[^0-9,]/g, '');
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="enteros" type=text />
<input class="decimales" type=text />
    
answered by 17.01.2018 / 16:31
source