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

128
Views
Prevent inserting value greater than max at the same time restrict to decimal

I want to restrict the user input to two decimal places only at the same time restrict user input not greater than the maxvalue. My code below doesn't work together.

JS Code

var validate = function(e) {
    var t = e.value;
    e.value = (t.indexOf(".") >= 0) ? (t.substr(0, t.indexOf(".")) + t.substr(t.indexOf("."), 3)) : t;
}

var input = document.getElementById("amt");
// Add event listener
input.addEventListener("input", function(e){
    var max = parseFloat(input.max);

    this.setCustomValidity("");

    console.log(this.value)
    
    if(parseFloat(this.value) > max){
        this.value = max; 
        
    } else if(this.validity.rangeUnderflow){
        this.setCustomValidity("Does not reach the amount requirement");
    }
});

html code

<input type="number" min="100" max="999.99" step=".01" name="amt" id="amt" oninput="validate(this)" required>
about 4 years ago · Juan Pablo Isaza
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!