简体   繁体   中英

How to fix this currency mask for text input?

Hey guys I'd like to mask some numbers like this:

001 -> 0,01 || 10000 -> 100,00 || 10012300 -> 100.123,00 and so on...

But my code just put the ','

$(document).ready(function () {

            var mask = {
                money: function () {
                    var el = this
                        , exec = function (v) {
                            v = v.replace(/\D/g, "");
                            v = new String(Number(v));
                            var len = v.length;
                            if (len > 2)
                                v = v.replace(/(\d{2})$/, ',$1');
                            else if (2 == len)
                                v = v.replace(/(\d)/, "0,$1");
                            else if (1 == len) {
                                v = v.replace(/(\d)/, "0,0$1");
                            }
                            return v;
                        };

                    setTimeout(function () {
                        el.value = exec(el.value);
                    }, 1);
                }

            }

            $(function () {
                $('#money').bind('keypress', mask.money);
                $('#money').bind('keyup', mask.money);
            });

        });

Could you help with that please? If possible I'd like to put the "R$", like this:

"R$ 1.123.254,00".

Thank you very much.

there you go.

html

<label for="currencyMask"> R$</label>
<form action="#">
    <input id="currencyMask" name="currencyMask" value="" required/>
</form>

javascript

$('input[name=currencyMask]').mask('#.##0,00', {reverse: true, maxlength: false});

Note. You need to include jquery.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM