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

1K
Views
How to make Vue js vuetify input allow only 3 digits before decimal and 2 digits after decimal

I need to set limit to the total input number to 3. For example, user can input 333, 123 etc. However, if they use decimal point, they will be allowed to enter 2 digits after decimal point. So, these inputs are valid:

123

123.33

Here is the code I have written so far:

isNumber (event, message) {
  // preventing multiple decimal
  if (!/\d/.test(event.key) && (event.key !== '.' || /\./.test(message))) {
    return event.preventDefault()
  }

  // limiting three numbers before decimal point and two digits after decimal point
  if (/^(\d{0,3}\.)?\d{1,2}$/.test(message)) return event.preventDefault()
 
}

But, this regex, /^(\d{0,3}\.)?\d{1,2}$/ is not working for my case

CodePen Demo

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You may consider using a number input (<input type="number">), with a max property to control the number of digits, and a small keyup handler to control the value.

document.addEventListener('keyup', handle);

function handle(evt) {
  if (evt.target.id === `num`) {
    if (evt.key === `Backspace`) { return true; }
    const [number, int] = [parseFloat(evt.target.value), parseInt(evt.target.value)];
    evt.target.value = number > evt.target.max 
      ? evt.target.max : number === int 
        ? int : number.toFixed(2);
    document.querySelector(`#value`).textContent = `current value: ${evt.target.value}`;
  }
}
<input id="num" type="number" step="0.1" max="999.99">
<div id="value"></div>

over 4 years ago · Santiago Trujillo Report

0

You may use the following regex:

^\d{0,3}(?:\.\d{1,2})?$

The thing to remember is to make the fractional part, including the ., optional.

Demo

over 4 years ago · Santiago Trujillo 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!