Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!