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

216
Views
Validating a number field with yup, using custom message and checking for empty field

I am validating a field that can only take a positive decimal number. My Yup schema looks as follow:

yup.object().shape({
Duration: yup
    .number()
    .typeError('Please enter a duration. The field cannot be left blank.')
    .positive('Must be a positive number.'),
})

As a result,

  1. When the user enters a negative number (including 0), the 'Must be a positive number' message is displayed
  2. When the user leaves the field blank, the 'Please enter a duration. The field cannot be left blank' message is displayed.
  3. But, when the user enters a string such as (two minutes or any other string), the 'Please enter a duration. The field cannot be left blank' message is displayed which does not make sense.

I could change the typeError() message for something more generic but I was wondering if there was a more comprehensive way to handle the scenario.

So, when the user enters

  1. a string I can display a 'Must be a number type' message and,
  2. when the field is left blank, I can display the 'Please enter a duration. The field cannot be left blank' message.
about 4 years ago · Juan Pablo Isaza
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!