Takuma
Takuma

Reputation: 43

Allow the use of the % symbol in the same field using jQuery Mask

How to allow inclusion of the % symbol in the field?

The same field is used as a fixed value and or a percentage value.

In the same field, it can be:

1,45 (one dollar and forty-five cents)

or

1,45% (one point forty-five percent)

-

1.000.000,00 or 2,50%

If there is another alternative, I will accept indication.

$(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>


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

Thank you.

Upvotes: 4

Views: 140

Answers (1)

NullDev
NullDev

Reputation: 7303

You could use the translation feature of the plugin to define a RegEx pattern:

$(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="0,00" required/>

Upvotes: 1

Related Questions