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

136
Views
Input[type="number"] handler not working properly [Formik, RegExp]

I have an input which can't have more than 2 decimal places. Also I have validation, which doesn't work properly for numbers with lots of zeros.

Expected behavior

Unexpected behavior

export default function App() {
  const isValidAmount = (amount) => /^$|^\d+\.?\d{0,2}$/.test(String(amount));

  return (
    <div>
      <Formik initialValues={{ decimalNumber: "" }}>
        {({ handleChange }) => (
          <Field
            className="input"
            type="number"
            name="decimalNumber"
            placeholder="Decimal Number"
            component={TextField}
            InputProps={{
              onChange: (e) => {
                if (!isValidAmount(e.target.value)) return;
                handleChange(e);
              }
            }}
          />
        )}
      </Formik>
    </div>
  );
}

Code here

Can you please explain: What is going on?

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

0

Use the HTML step attribute to limit the allowable numbers. In this case you want to limit to hundredths:

<Field
  /* ... */
  step="0.01"
/>
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!