Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React hook form only allow numbers greater than 0

I'm using React Hook Form to make an input for height, weight and length. The value cannot be 0 - it has to be at least 1 or more. I tried different ways such as: pattern with regex, validate and more. But I am unable to figure out how to prevent the input field from letting the user type in '0' ( only as the first character ). I also tried type="number" and min="1" however it's not changing anything. Any ideas would be greatly appreciated.

                    <Input
                      {...form.register(`pallets.${index}.weight`, {
                        required: t('errors.emptyInput'),
                        pattern: {
                          value: /^[^1-9]/,
                          message: 'hello',
                        },
                        validate: (value) => {
                          return [/^[^1-9]/].every((pattern) => pattern.test(value))
                        },
                      })}
                      type='number'
                    />

It appears to me that pattern does not change anything. Only type= 'number' actually affects the way the input field works.

like image 617
Mateusz Szumilo Avatar asked Dec 06 '25 17:12

Mateusz Szumilo


1 Answers

<input
  type="number"
  {...register("test", {
    min: 1
  })}
/>

You can check the detailed usage from the documentation.

like image 195
Nurhak Altin Avatar answered Dec 08 '25 08:12

Nurhak Altin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!