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

117
Visualizações
Regex Javascript block Input

i've just build a javascript function with a regex control that allows 3 numbers and one dot.

function restrictNumber(e) {
  var newValue = this.value.replace(new RegExp(/^(?!^(?:\d{1,3}|\d(?:\d?\.\d?|\.\d{2}))$).*/, 'gm'), "");
  this.value = newValue;
}

$('.decimal').on('input', restrictNumber);
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<input class="decimal" type="text">

The specs of the regex are:

  • max. 3 numbers
  • only one or no dot
  • dot can be anywhere

Here is a regex demo for it: https://regex101.com/r/3Ru1O3/3/

i tried to "block" the input when a char is put in that isn't fitting. But when i try to test it deletes my hole string.

How can i change that behaviour that i just can't set new numbers put the string isnt vanishing.

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

0

You didn't say much about your actual requirements, as in, if the decimal is required at a certain place value, etc.. but based on what your existing regex does, it could definitely be shortened.

function restrictNumber(e) {
  var newValue = this.value.replace(/[^\d\.]/, "").substr(0,4);
  this.value = newValue;
}

$('.decimal').on('input', restrictNumber);
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<input class="decimal" type="text">

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