Jquery Mask number with commas and decimal

a 夏天 提交于 2020-01-01 04:58:26

问题


I am using a Jquery mask plugin to format many things on my site and I am trying to figure out how to get it to format numbers in the way I need. I am using the following plugin.

https://igorescobar.github.io/jQuery-Mask-Plugin/

I am attempting to get the following format for my numbers.

999,999,999.99

The number is a currency field that needs a comma added every three digits and the value can be as low as 0.00


回答1:


Well, this worked. I just tweaked the example from the website changing dots with commas and it worked.

$('.money').mask("#,##0.00", {reverse: true});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.13.4/jquery.mask.min.js"></script>

<input class="money" type="text"/>    

As you can see there is no limitation of numbers. If you want to limit them, you can do $('.money').mask('000,000,000,000,000.00', {reverse: true});




回答2:


Try this mask from their website with inverting commas and dots:

$('.money').mask('000.000.000.000.000,00', {reverse: true});

to have

$('.money').mask('000,000,000,000,000.00', {reverse: true});



回答3:


I've created a mask which is without reverse but still works fine.

Idea is to change mask on the fly on every onKeyPress. Here is a solution: https://codepen.io/anon/pen/wNvvWw

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.10/jquery.mask.js"></script>

<input type="text" class="input-float" placeholder="00.00" value="" />

<script>
    var curCharLenght = 0;
    var floatOptions = {
      onKeyPress: function(cur, e, currentField, floatOptions) {
        var mask = createMask(cur);
        var field = currentField
          .parent()
          .find(".input-float[data-field=" + currentField.attr("data-field") + "]");
        if (cur.length - curCharLenght < 0 && cur.indexOf(".") == -1) {
          field.mask(mask + " 000", floatOptions);
          curCharLenght = cur.length;
        } else if (event.data == "," || event.data == ".") {
          curCharLenght = mask.length + 1;
          mask += ".0000";
          field.mask(mask, floatOptions);
        } else {
          if (cur.indexOf(".") == -1) {
            mask = mask + " 000.0000";
            field.mask(mask, floatOptions);
            if (isNaN(e.originalEvent.data) || e.originalEvent.data == " ") {
              field.val(field.val().slice(0, -1));
            }
          }
          curCharLenght = cur.length;
        }
      }
    };

    function createMask(val) {
      var mask = "";
      var num = val.split(".")[0];
      num = num.replace(/ /g, "");
      for (var i = 1; i <= num.length; i++) {
        mask += "0";
        if ((num.length - i) % 3 === 0 && i != num.length) {
          mask += " ";
        }
      }
      return mask;
    }

    $(".input-float").each(function(index, el) {
      var item = $(this);
      item.attr("data-field", "field-" + index);

      var mask = createMask(item.val());
      if (item.val().indexOf(".") !== -1) {
        var splitedVal = item.val().split(".");
        if (splitedVal.length > 1 && splitedVal[1].length > 2) {
          if (splitedVal[1].length == 3) {
            mask += ".000";
          } else {
            mask += ".0000";
          }
        } else {
          mask += ".00";
        }
      }

      item.mask(mask, floatOptions);
    });
</script>


来源:https://stackoverflow.com/questions/35413377/jquery-mask-number-with-commas-and-decimal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!