0

I'm trying to format values as money in one project, and those values can be negative.

I'm stuck in how to create the correct mask. I did one jsfiddle:

<input type="text" class="negative-money" />

$(document).ready(function() {    
  var mask = $('.negative-money');
  var money = -1011987.65;

  mask.val(money);
  mask.mask('-{000,}000.00');
  // ---------^^^^^^ the problem is here
});

I looked some examples and read some articles, but I couldn't figure out how to do it.

I want the output for the previous example to be: -1,011,987.65

Washington Guedes
  • 4,254
  • 3
  • 30
  • 56

1 Answers1

0

Refer this for negative numbers

jQuery Mask Plugin: mask for negative integers

I have modified the script based on the above link:

$('input.negative-money').mask('#,##0.00', {
  reverse: true,
  translation: {
    '#': {
      pattern: /-|\d/,
      recursive: true
    }
  }
});

var value = $('input.negative-money').val();
$('input.negative-money').val(value.replace(/(?!^)-/g, '').replace(/^,/, '').replace(/^-,/, '-'));
Community
  • 1
  • 1
Vijai
  • 2,369
  • 3
  • 25
  • 32