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

125
Views
Allowing only positive numbers with decimal places

I found this neat solution to my negative numbers problem https://stackoverflow.com/a/46039201

Here it is below

onInput={(e)=>{
 e.target.value = !!e.target.value && Math.abs(e.target.value) >= 0 ? Math.abs(e.target.value) : null;
}}

But I have a bit of an issue it only allows numbers and I do require decimal places because these inputs are for currencies such as activation fee, billing amount and such. Otherwise than that the above solution works like a charm

Does anyone here have a similar solution or something that can be added to this to make it accept decimal places ?

Ive tried looking into some others but then the user can type in -1111 and it saves it even if you add min={0} to the input field

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It wasn't easy for me to find a solution. In the end, what worked was a combination of a pattern and a custom attribute to store the previous value:

const restricted = document.querySelector('#restricted')
restricted.addEventListener('keyup', event => {
  const value = event.target.value;
  if (!/^[0-9]+\.?[0-9]?$/.test(value) && value !== '') {
    event.target.value =  event.target.getAttribute('data-value');
  } else {
    event.target.setAttribute('data-value', value);
  }
});
<input id="restricted">

about 4 years ago · Juan Pablo Isaza Report

0

Make use of step

<input type="number" min="0" step="0.1" oninput="this.value = Math.abs(this.value)">

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!