'Optional decimal with jQuery-Mask-Plugin
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?
Solution 1:[1]
It works in my similary case
$('#test').mask('###,###,###,###P00', {
reverse: true, 'translation': {
P: {pattern: /\./, optional: true}
}
});
Solution 2:[2]
Try
$('#test').mask("#,##0.00", {reverse: false});
reverse = false will allow you to type from right to left and you can leave decimal
Solution 3:[3]
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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user2971916 |
Solution 2 | Ubiquitous Developers |
Solution 3 | Arash Bazrafshan |