Format number: Add "." or ","

Hello people.
I am working on a form and when the customer enter numbers in a input, i need it appears instantly one point every three digits in the same field, like this: 1111111 —> 1 . 111 . 111
I’ve got the next code, but im new and its really difficult make this work.

function formatNumber(num) {
 return num
    .replace(",", ".") // replace decimal point character
    .replace(/(\d)(?=(\d{3})+(?!\d))/g);
}
export function input3_change(event, $w) {
 let num = parseFloat($w("#input3).value)
    $w("#input3").value = .replace(/(\d)(?=(\d{3})+(?!\d))/g)
}


To this, while the user is writing:


Hope you can help me, thanks.

I don’t understand what the desired format is.
On one hand you said you wanted to add a point every 3 digits,
but on the other hand, your examples include points in other places: 1.111 . 111 (here it’s after the first digit), 12.345.678 (after the first 2 digits).
So what exactly is the desired format?

Anyway, if you want the point to appear every 3 digits, then:
If you want the points to be added while typing you can use this code:

$w.onReady(function () {
    $w("#input3").onKeyPress( (event) => {
        setTimeout(parseString, 10);
        })
 function parseString() {
     let value = $w("#input3").value;
     let stringDigits = value.replace(/[^0-9]/g,"");
     let finalString = stringDigits.replace(/...\B/g, "$&.");
     $w("#input3").value = finalString;
        }
})

If you want the points to appear only when the user gets out of the input box, you can use this code:

$w.onReady(function () {
    $w("#input3").onBlur( (event) => {
 let value = $w("#input3").value;
 let stringDigits = value.replace(/[^0-9]/g,"");
 let finalString = stringDigits.replace(/...\B/g, "$&.");
 $w("#input3").value = finalString;
})
})

Thanks a lot J.D. it works for me, i put a photo with your answer in my bedroom for remember it jajaja
Have a nice day.

:slight_smile: You’re welcome.

could you help me, I would like to write the point in thousands and millions.

I tried to use this code but when I type a seventh digit, what I am writing is deleted.

Thanks a lot

Do you want a point or a comma?
If you want a comma, then you can do:

let number = 4523445556;
let formattedNumber = number.toLocaleString('us-EN');

As they use commas as separators in the US.

To use a point:

let number = 453445556;
let formattedNumber = number.toLocaleString('de-DE');

Since they use points as separators in Germany.

If you want to use a point or a comma depend on the client-side location, then:

let number = 4523445556;
let formattedNumber = number.toLocaleString();

can you share me a code for the points to be added while typing please!

First you’ll need to set the user input type as text.
Then you can try this code:

$w.onReady(() => {
    $w("#input1").onKeyPress(event => {
        setTimeout(parseString, 10);
        })
 function parseString() {
     let value = $w("#input1").value;
     let stringDigits = value.replace(/[^0-9]/g,"");//to omit any charterer other than a digit;
     if(stringDigits.length === 0){
         $w("#input1").value = "";
      } else {
         let number = Number(stringDigits);
         $w("#input1").value = number.toLocaleString('de-DE');
      }
    }
})

I really appreciate your collaboration but something happens, when you get to type the seventh number everything is deleted I don’t know what will be happening

was the text format!

now is working! thanks a lot!