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

116
Visualizações
Only numbers on type text input on React.js with hooks

I am trying to avoid any type of string when entering an input. I found a code here on stack overflow which works. However it only works when a number is entered first, or if I put a letter and delete all; it does not work when typing the first letter. And I didn't understand the code that well. I don't even know what is /^[0-9\b]+$/

Here is the code

   const [value, setValue] = useState();

    const handleChange = (e) => {
        const re = /^[0-9\b]+$/;
        if (e.target.value === "" || re.test(e.target.value)) {
            setValue(e.target.value);
        }
    };
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

How about testing if conversion to Number returns NaN? Try code below:

 const [value, setValue] = useState();

 const handleChange = (e) => {
    if (!isNaN(Number(e.target.value))) {
        setValue(e.target.value);
    }
 };

And, btw, as guys in the comments below the original question correctly pointed out, this: /^[0-9\b]+$/ is indeed a Regular Expression

about 4 years ago · Juan Pablo Isaza 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