How to add limit value along with jquery Mask?

Asked

Viewed 747 times

0

Guys I’m using the mask plugin of this link: https://igorescobar.github.io/jQuery-Mask-Plugin/

I use the following jquery to make the mask:

$('.decimal').mask('000,0', {reverse: true});

However I need the maximum value to be 100.0. I’ve tried to put

$('.decimal').mask('100,0', {reverse: true});

But it doesn’t work because the 0 means any number so one can put up 199.9.

I wonder if someone could help me?

1 answer

0


You can the event onKeyPress, replace the comma with the point and check that it is greater than 100.0:

$('.decimal').mask('000,0', {
  reverse: true,
  onKeyPress: function(val, e, field, options) {
    if (val.replace(',', '.') > 100.0) {
      $('.decimal').val('')
    }
  }
});

See working:

$('.decimal').mask('000,0', {
  reverse: true,
  onKeyPress: function(val, e, field, options) {
    if (val.replace(',', '.') > 100.0) {
      console.clear();
      console.log('Valor maximo 100,0 !');
      $('.decimal').val('');
    }
  }
});
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.js"></script>
<input type="text" class="decimal">

Reference:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.