Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
JavaScript formatting numbers in input by commas not working

I need to separate number with commas and I used code block from another question I found here but it's not working as expected

The input

<input type="text" name="budget" placeholder="Total Budget" class="form-control">

The JS

$('input[name=budget]').keyup(function (e) { 
    e.preventDefault();
    let value = $(this).val();
    let newValue = value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
    $(this).val(newValue);
});

When I tested this code I faced a problem

I typed 1000 and it showed correctly 1,000

But when I typed another 0 the number returned into this 1,0,000

And every 0 I increase it adds a comma after it like this 1,0,0,0,0,000

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to remove commas first

let newValue = value.toString().replace(/,/g,"").replace(/\B(?=(\d{3})+(?!\d))/g, ",")
about 4 years ago · Juan Pablo Isaza Relatório

0

There is a native solution for formatting numbers Intl.NumberFormat

I would use that, instead of a regExp replace.

const numberFormatter = new Intl.NumberFormat();
const newValue = numberFormatter.format(value);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda