mascara accepting numbers and, with jQuery Mask Plugin

0

Galera wanted to know how to create a mask in the jQuery Mask Plugin so that the input accepts only numbers and commas.

To accept just number I'm doing this:

$('#valor').mask('#', {
    reverse: true
});
    
asked by anonymous 27.02.2016 / 13:43

2 answers

3

I see that you want a mascara for money, in this case masks based on regular expressions are not the best option, between using a specialized mask.

JQuery Mask Money

$(function() {
  $('#dinheiroComZero').maskMoney({ decimal: ',', thousands: '.', precision: 2 });
  $('#dinheiroSemZero').maskMoney({ decimal: ',', thousands: '.', precision: 0 });
  $('#dinheiroVirgula').maskMoney({ decimal: '.', thousands: ',', precision: 2 });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery-maskmoney/3.0.2/jquery.maskMoney.min.js"></script>

<div>
  <label>
    Com Centavos: 
    <input type="text" id="dinheiroComZero" />
  </label>
</div>
<div>
  <label>
    Sem Centavos:
    <input type="text" id="dinheiroSemZero" />
  </label>
</div>
<div>
  <label>
    Virgula como seperador: 
    <input type="text" id="dinheiroVirgula" />
  </label>
</div>

Vanilla Masker

var dinheiro = document.getElementById("dinheiro");
VMasker(dinheiro).maskMoney({
  precision: 2,
  separator: ',',
  delimiter: '.',
  unit: 'R$',  
  zeroCents: true
});
<script src="https://rawgit.com/BankFacil/vanilla-masker/master/lib/vanilla-masker.js"></script><inputtype="text" id="dinheiro" />

In the case of Vanilla Masker , I prefer to use a modified version, which makes use of the input event instead of keyup .

    
29.02.2016 / 13:12
0

To accept numbers with commas, use

 $('.money2').mask("#.##0,00", {reverse: true});
    
29.02.2016 / 12:33