maskMoney without having to select input

Asked

Viewed 5,856 times

2

Guys I’m using maskMoney but it only format value if I click the input, have you change it? That is to format it as soon as the page is loaded?

$(function() {

  $("input").maskMoney({
    allowNegative: true,
    thousands: '.',
    decimal: ',',
    affixesStay: false
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-maskmoney/3.0.2/jquery.maskMoney.min.js"></script>
<input type="text" value="-90.00"/>

1 answer

3


you can force the formatting when firing the event mask.maskMoney.

$(function() {
  var maxLength = '-0.000.000,00'.length;
  
  $("input").maskMoney({
    allowNegative: true,
    thousands: '.',
    decimal: ',',
    affixesStay: false
  }).attr('maxlength', maxLength).trigger('mask.maskMoney');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-maskmoney/3.0.2/jquery.maskMoney.min.js"></script>
<input type="text" value="-90.00"/>

  • very good, it worked 100%. It takes away just one more doubt, as I limit the maximum number to 0.000.000,000?

  • 1

    @Hugoborges, I don’t know if it’s possible through API of maskMoney, but try to set a maxlength to the input

  • 1

    I edited the answer, it’s not 100%, but it’s a start.

  • it was very good there :)

Browser other questions tagged

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