I am new to React.
Trying to enforce min/max to Form.Control,
so min=0 max=1 step=0.01, it works on Up/Down arrows,
but what if user enters manually say 2.
It just accepts it, no errors, nothing.
Is there way to enforce that manual input validation,
so it basically doesn’t allow to enter 2?
Please help!
TIA,
Oleg.
2
Answers
you can add validation in onChange function that before add in input check value:
for example we can check the value ,if value larger than 1 , set 1 for input :
if you don’t want to add state, and use from Formik or react-hook-form , you can validation for number such as blew for react-hook-form :
in this way user can write greater than 1 ( for example 2) on input but can’t submit form.
If you want to enforce an user to do not enforce the input through UI, add min/max, if you want to enforce with validation you can add a "value" and an "onChange" attribute. The value attribute will handle the value and the onChange update the value (unless it doesn’t pass your validation).
You can try it by yourself in the example I made on playcode.io.