How to use money mask without jQuery

Asked

Viewed 250 times

-1

I would like to know if there is any way to use masks for money without having to jQuery. I know the toLocaleString to format, but would like to apply a mask in the field. I searched the internet but unsuccessfully.

I have a input and would like to apply the value of toLocaleString on it. At the moment I only catch it with.

let salario = document.querySelector('#contract_salary');
salario.addEventListener('keyup', () => {
  console.log(salario.value.toLocaleString("pt-BR", { minimumFractionDigits:2, style: 'currency', currency: 'BRL' }))
})
 <input type="text" class="form-control" id="contract_salary" name="contract_salary" required>

  • 2

    This one is more interesting https://answall.com/questions/181922/formatter-brasileira-em-javascript

  • There is no way to make mask with .toLocaleString by typing because it formats the value as a whole and not in pieces.

  • It has a lib called jquery Mask, very good.

  • Have you tried Simplemaskmoney https://www.npmjs.com/simple-mask-money

1 answer

2


document.getElementById("contract_salary").onkeyup = function() {myFunction()};

function myFunction() {
  var atual = document.getElementById("contract_salary").value;
console.log(new Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL' }).format(atual));
}
<input type="text" class="form-control" id="contract_salary" name="contract_salary" required>

I hope it helped you!

Browser other questions tagged

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