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

146
Views
Regex for maximum 4 digit with dot, accept only number and separate with dot every thousand

I have a problem to convert input in HTML to specific criteria :

  • Maximum 4 digit with dot (ex : 5.100)
  • Only accept number
  • Separate with dot if thousand

I am able to achieve the last two with this :

value.replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ".");

How to add the first criteria ?

And seems like my regex is too long, a shorter one would be great.

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

0

Remove all but first four digits:

const value = `123456`
console.log(
   value.replace(/\D/g, "").replace(/^(\d{4})\d+/, '$1').replace(/\B(?=(\d{3})+(?!\d))/g, ".")
)

about 4 years ago · Juan Pablo Isaza Report

0

I would use a mask for the input,

("#id").inputmask({
                mask: "9.999", 
                placeholder: "",
                separator: "", 
               });

Then to check the value,

value.replace('.',''); 

and with a regular expression to verify that it is true that they are only digits and at most 4.

let regex_aux = /^\d{4}/;
 if(regex_aux.test(value)){
        //do something
    }
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!