I am validating an input type number, it only must accepts values from 0 to 9999. There can not be dots/decimals or anything like that.
I am doing it in a hacky way but it still throws an error when you enter 0.0. The input can not receive any kind of special characters, only integer numbers from 0 to 9999.
// Initial value is an string
integersOnly(value) {
// So I need to convert it to a number
const turnValueIntoANumber = Number(value);
return (isNaN(turnValueIntoANumber) || !Number.isInteger(turnValueIntoANumber)) && 'Only integers allowed';
}
The field:
<Field
min={0}
max={9999}
name="time"
type="number"
validate={[this.integersOnly]}
>
TIME
</Field>
What am I doing wrong?
i think you are using formik .
what about using yup schema builder for validation.
check this :
yup documentation
you can use
yup.number().required().positive().integer()