Optional decimal with jQuery-Mask-Plugin

Viewed 2093

I am trying to implement a mask using jQuery-Mask-Plugin https://igorescobar.github.io/jQuery-Mask-Plugin/docs.html#field-options

From the doc this will allow me a recursive number with two decimal points.

 $('#test').mask("#,##0.00", {reverse: true});

But as the user starts typing 100 is being masked to 1.00 vs. 100 I am trying to figure out if there is a way to do this recursiveness from left to right as in, only if the user types '.' the decimal will be considered

For example

100 -> 100
1000 -> 1,000
1000.00 -> 1,000.00

Is it even possible?

3 Answers

Try

 $('#test').mask("#,##0.00", {reverse: false});

reverse = false will allow you to type from right to left and you can leave decimal

var doptions = {
    reverse: true,
    translation: {
        'D': {
            pattern: /\./,
            optional: true,
            fallback: '.'
        }
    },
    onKeyPress: function (cep, e, field, options) {
        var masks = ['#,##0D', '#,##0D99'];
        var mask = (cep.indexOf('.') >= 1) ? masks[1] : masks[0];
        $('.money').mask(mask, options);
    }
};
$('.money').mask('#,##0D', doptions);
Related