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
Only numbers on type text input on React.js with hooks

I am trying to avoid any type of string when entering an input. I found a code here on stack overflow which works. However it only works when a number is entered first, or if I put a letter and delete all; it does not work when typing the first letter. And I didn't understand the code that well. I don't even know what is /^[0-9\b]+$/

Here is the code

   const [value, setValue] = useState();

    const handleChange = (e) => {
        const re = /^[0-9\b]+$/;
        if (e.target.value === "" || re.test(e.target.value)) {
            setValue(e.target.value);
        }
    };
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

How about testing if conversion to Number returns NaN? Try code below:

 const [value, setValue] = useState();

 const handleChange = (e) => {
    if (!isNaN(Number(e.target.value))) {
        setValue(e.target.value);
    }
 };

And, btw, as guys in the comments below the original question correctly pointed out, this: /^[0-9\b]+$/ is indeed a Regular Expression

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!