mask accepting numbers and , with jQuery Mask Plugin

Asked

Viewed 8,101 times

1

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

To accept only number I’m doing so:

$('#valor').mask('#', {
    reverse: true
});

2 answers

4

I see that you want a money mask, in this case, masks based on regular expressions are not the best option, between using a specific 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>
<script src="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>

<input type="text" id="dinheiro" />

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

  • 1

    I don’t want to use a money mask, after all I want an input where I can add only numbers and commas. And I think with the jQuery Mask Plugin to do this, after all I already use it on my system.

  • @Hugoborges, you are trying to hunt like a cat, despite having a dog at your disposal.

0

To accept numbers with comma use

 $('.money2').mask("#.##0,00", {reverse: true});
  • did not work. I need to add numbers like 1,22,333,222,1,333,444,32

  • for whole numbers I did it $('#valor').mask('###0', {&#xA; reverse: true&#xA;});

  • but the comma doesn’t work. I need to add numbers and comma

  • But do you need a number with more than one comma? ?

  • then there is a specific amount, and I have to put several commas. You know how to solve this?

  • Friend, I don’t know if with jquery Mask you will be able to. In my environment to treat this I had to work with REGEX. Maybe to use Jquery Mask, you will have to tinker with the plugin core (which is not recommended). Maybe someone else has a better solution than Regex

  • good, so thank you so much for helping :) if I find the solution I will post here.

  • Blz, but try not to focus on MASK, you can make a script that allows entering only numbers and comma in your input

Show 3 more comments

Browser other questions tagged

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