Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
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.

almost 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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

You can check the detailed usage from the documentation.

almost 4 years ago · Santiago Trujillo Relatório

0

You can take advantage of react useState hook to implement this functionality.

export default function App() {
  const [x, setX] = useState(null);
  const handleChange = e => {
    const value = e.target.value;
    if (!value || /^[1-9]\d*/.test(value))
      setX(e.target.value)
  }
  return (
    <div className="App">
      <input type="number" value={x} onChange={handleChange} />
    </div>
  );
}
almost 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda