Use % in input using jQuery Mask

Asked

Viewed 94 times

-2

How do I allow the % symbol inside the input using jQuery Mask?

I ask this, because in the same field will have case that it is used as currency formatting 1.000.000,00 or just percentage 2.50%.

$(document).ready(function() {

  $('.moeda').mask("#.##0,00", {reverse: true});

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script>


Taxa: 
<input id="formaTaxas" name="formaTaxas" type="text" class="form-control moeda" value="0,00" required/>

  • 2

    But the value "2.50%" is not a monetary format, so why use maskMoney?

  • @Andersoncarloswoss I missed the name, it’s not maskMoney, it’s jQuery Mask. Do you know how I can use the symbol of %?

  • If you really want to use JS (which I wouldn’t), try using the percentage code: UNICODE. U+00025. HEX CODE. &#X25; HTML CODE. &#37; HTML ENTITY. &percnt; CSS CODE. 0025. <span>&#37;</span> content: " 0025";

  • @Edwardramos what suggestion would you give? It doesn’t have to be exactly JS, I just need it to work.

  • Just leave it outside the input or would mention in the title Ex: "Estimated Profit (%)". But if you want to use the % within the input, try using any of the above comment codes.

1 answer

0

For those who have the same question follows answer:

https://igorescobar.github.io/jQuery-Mask-Plugin/docs.html#Translation

$(document).ready(function() {
    $(".moeda").mask("#.##0,00%", {
        reverse: true,
        translation: {
            "%": {
                pattern: /\%/,
                optional: true
            }
        }
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script>


Tax: 
<input id="formaTaxas" name="formaTaxas" type="text" class="form-control moeda" value="" required/>

Browser other questions tagged

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