How do I format the sum in an input?

  • 0
    How to make the number entered in input (for example 1000) automatically
    was displayed like this: 1 000,00 ₽?

    P.S need in pure JavaScript
    JavaScript Anonymous, Apr 25, 2020

  • 3 Answers
  • 0
    Install iMask



    import IMask from "imask";

    const input = document.getElementById("id123");
    const mask = new IMask(input, { mask: "0 000,00" });

    input.addEventListener("input", () => console.log(mask.unmaskedValue));




    1. Mask template as ordered.

    2. Returns a pure value, no mask.
    Anonymous

  • 0
    on the input event, track the input value and format
    Anonymous

  • 0
    I did it like this, not exactly what I wanted, but it will work.

    & lt; input type = "text" class = "amount" & gt;



    And my JavaScript curve:

    document.querySelectorAll('.amount').forEach(amount => {
    amount.oninput = function() {
    this.value = this.value.replace(/[\D\s\._\-]+/g, "")
    this.value = new Intl.NumberFormat('ru-RU', {
    currency: 'rub',
    }).format(this.value)
    }
    amount.onblur = () => {
    if (amount.value === '0') amount.value = ''
    }
    })
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!