I’m using input type=number
and validation to it.
What I am trying to do:
- if value is not number – show error
only numbers allowed
- if input is empty – show error
value is required
yup.object().shape({ fieldName: yup.number().typeError("only numbers allowed").required("value required").min(0) })
But it returns always only typerror
2
Answers
You are trying to validate an input field or type = number, if an empty string is passed it bound to produce an error. The Yup validation is actively checking if the value is a valid JavaScript number, and empty values or non-numeric strings do not meet this criterion.
Here’s the modified code: