Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda