Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda