question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong masked values without decimal

See original GitHub issue

Hey guys… When I’m editing some fields and I have my input rendered as follow:

<input type="text" class="mask-money"  value="10000" data-thousands="." data-decimal="," data-prefix="R$ ">
<input type="text" class="mask-money" value="4500" data-thousands="." data-decimal="," data-prefix="R$ ">

<input type="text" class="mask-money"  value="1" data-thousands="." data-decimal="," data-prefix="R$ ">
<input type="text" class="mask-money"  value="1.1" data-thousands="." data-decimal="," data-prefix="R$ ">

<input type="text" class="mask-money"  value="0.8" data-thousands="." data-decimal="," data-prefix="R$ ">
<input type="text" class="mask-money"  value="0.88" data-thousands="." data-decimal="," data-prefix="R$ ">

First, feels weird to have to do this in order to mask the fields:

$('.mask-money').maskMoney(); // shouldn't this be enough?
$('.mask-money').maskMoney('mask'); // is this really necessary?

But the problem is that I get this values:

  • 100,00 (instead of 10.000,00)
  • 45,00 (instead of 4.500,00)
  • 0,01 (instead of 1,00)
  • 0,11 (instead of 1,10)
  • 0,08 (instead of 0,80)
  • 0,88 (yeah! fot his one right)

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
ghostcommented, Jan 14, 2015

We ran into this issue as well. As a work around I am using javascripts Number() to convert the value to a number during the mask. Below is an example:

http://jsfiddle.net/5pgdq8od/1/

see pull request with possible fix: https://github.com/plentz/jquery-maskmoney/commit/2de7b9a5f09d44731fcd0ef9c0c751eeb24e48b1

jsfiddle with some tests for fix: http://jsfiddle.net/6vyL2v7f/

3reactions
MauricioMoraescommented, Sep 13, 2016

I’ve read everything but I had to solve my problem RIGHT NOW. So here goes an UGLY patch. My values that should show R$ 500,00 were showing as R$ 50,00.

It makes the value of the field get two decimals when it has only one, so 500.0 becomes 500.00, 100.1 becomes 100.10. I do it right before setting the maskMoney on the field, and it fixed it for now (for me)

Be very careful with this patch. I will remove it as soon as the rails gem I’m using for this lib is correct.

function setupMoneyFields(){
   var moneyFields = $("input.money");

   // Iterating so I can have multiple money fields on the same page.
   $.each(moneyFields, function(){
      replaceSingleDecimalByDouble($(this));
   });

   moneyFields.maskMoney({
     thousands: '.',
     decimal: ',',
     prefix: "R$ ",
     allowZero: true
   });
   moneyFields.maskMoney('mask');
}

// Doing this because of Jquery MaskMoney's issue, that is turning 500.0 into R$ 50,00
function replaceSingleDecimalByDouble(jquerySelector){
  var currentValue = jquerySelector.val();
  jquerySelector.val(currentValue.replace(/\.(\d)\b/, ".$10"));
}

I hope that helps someone somehow, and here goes my +1 for fixing this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Input Masks Library] Incorrect value when decimal has no ...
Hello. When using the currency mask on an edit screen, if the value got from the fetched data is for instance 123.45, it...
Read more >
Input mask for numeric and decimal - javascript - Stack Overflow
but when he put 555555 it's error, so I need to specify the mask where the mantissa is optional 0 to 999 and...
Read more >
Custom Input Masks (Numeric/Integer) - Informa Software
Numeric input masks are specifically designed for entering numeric values ... Decimal. The mask for entering integer values of a fixed and flexible...
Read more >
ngx-mask - npm
You can choose if mask will allow the use of negative numbers. The default value is false . Usage. <input type="text" ...
Read more >
Control data entry formats with input masks - Microsoft Support
If someone enters a phone number without the area code, Access won't write the ... For example, this is an input mask for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found