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

209
Visualizações
Could someone please assist with the limiting functions of a 0.1 step and no characters and also a min and max value

Here is the code I have so far and the limiting functions that I want the input for to reject

  • a step value of 0.1 ie no value smaller than 0.1 ie 0.01
  • no characters ie w,r,t
  • a min value set by the function input ie 1
  • a max value set by the function input ie 5
  • please note the way I have written the rest of my code the type of the input form can not be ="number" it must be ="text"

any help would be greatly appreciated

function myFunction(e, low, high) {
  console.log("called");
  console.log("low" + low);
  console.log("high" + high);
  console.log(e.target.value + e.key);

  var charValue = String.fromCharCode(e.keyCode);
  var nextValue = e.target.value + e.key;
  if (((!/^(\d+)?([.]?\d{0,1})?$/.test(e.target.value + e.key)) && (e.which != 8)) && (nextValue < 1 || nextValue > 5)) {
    e.preventDefault()
  }
}
<!DOCTYPE html>
<html>

<form name="addtext">
  SetPoint :<input id="setPoint" type="text" name="setPoint" onkeydown="myFunction(event,3, 5)" /><br />
</form>

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

0

Unless you're playing Code Golf, there are no prizes for cramming all the logic into a single condition.

Split the condition into multiple if statements so you can implement your logic clearly.

You need to replace the hard-coded 1 and 5 with low and high.

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

  var nextValue = e.target.value + e.key;
  console.log(nextValue);
  if (e.which == 8) { // allow backspace
    return;
  }
  if (!/^(\d+)?([.]?\d{0,1})?$/.test(nextValue)) {
    e.preventDefault(); // non-number, don't allow
  }
  if (nextValue < low || nextValue > high) {
    e.preventDefault();
  }
}
<!DOCTYPE html>
<html>

<form name="addtext">
  SetPoint :<input id="setPoint" type="text" name="setPoint" onkeydown="myFunction(event,3, 5)" /><br />
</form>

about 4 years ago · Juan Pablo Isaza Relatório

0

Or, you could use an input that is made for this. No javascript required.

<input id="range" value="1" type="range" step="0.1" min="0.1" max="5.0">

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