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

158
Visualizações
I need to allow negative numbers into this function

this function does the following

  • doesn't allow characters
  • allows backspace
  • has a high limit
  • has a low limit
  • has a minimun step size of 0.1 ie no number less than 0.1 ie not 0.01

what is not working are allowing negative numbers into the function

<html>
    <input id="setPoint" type="text" name="setPoint" onkeydown="myFunction(event,-80, 150)"   />

<script>


function myFunction(e, low, high) {
  console.log("low" + low);
  console.log("high" + high);

  var nextValue = e.target.value + e.key;
  console.log("NEXT VALUE:" + nextValue);
  if (e.which == 8) { // allow backspace
    return;
  }
  if (!/^(\d+)?([.]?\d{0,1})?$/.test(nextValue)) {
    e.preventDefault(); // non-number, don't allow
  }
  if (nextValue > high) {
    e.preventDefault();
  }
  if(nextValue< low){
    e.preventDefault();
  }
  
}
</script>
</html>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your regex was nearly there, however it needed a few additions. Try this regex instead:

/^-?\d+(\.\d+)?$/

Your current implementation also didn't allow for, eg, 10.24, the above should work fine with that. If that was intentional, just change the \d+ to \d - this'll ensure that, if the user does enter a ., they at least have one number after.

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