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

115
Views
Regex Javascript block Input

i've just build a javascript function with a regex control that allows 3 numbers and one dot.

function restrictNumber(e) {
  var newValue = this.value.replace(new RegExp(/^(?!^(?:\d{1,3}|\d(?:\d?\.\d?|\.\d{2}))$).*/, 'gm'), "");
  this.value = newValue;
}

$('.decimal').on('input', restrictNumber);
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<input class="decimal" type="text">

The specs of the regex are:

  • max. 3 numbers
  • only one or no dot
  • dot can be anywhere

Here is a regex demo for it: https://regex101.com/r/3Ru1O3/3/

i tried to "block" the input when a char is put in that isn't fitting. But when i try to test it deletes my hole string.

How can i change that behaviour that i just can't set new numbers put the string isnt vanishing.

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

0

You didn't say much about your actual requirements, as in, if the decimal is required at a certain place value, etc.. but based on what your existing regex does, it could definitely be shortened.

function restrictNumber(e) {
  var newValue = this.value.replace(/[^\d\.]/, "").substr(0,4);
  this.value = newValue;
}

$('.decimal').on('input', restrictNumber);
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<input class="decimal" type="text">

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!