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

128
Visualizações
Can't put more than 3 numbers in an input in ReactJS

I've got a big issue on my ReactJS code. I have an input to enter only number "EC" is suppose to be a number. The user can enter his data directly in the put or he can press the button + or - to add or remove 1000. The button +/- works but my input wont work.

Errors :

  • when I press delete in my keyboard, the "000" of 2000 are all delete at once
  • I can't put more that 4 numbers in the input
  • when I delete everything an "NaN" appear and I can't put anthing else in the input.

I tried a lot of things such as:

  • pass my input in a type='number'
  • put the function directly in the onChange{}
  • put Number() instead of parseInt()

Nothing works. Can you help me ?

function Resistance() {
  const [EC, setEC]= React.useState(2000)

  function handlePressEC() {
    const testEC=EC-1000
    if(testEC<=0) {
      setEC(0)
    }
    else {
      setEC(testEC)
    }
  }

  function handleChangeEC(e) {
    const EC = parseInt(e.target.value)
    setEC(EC)
    console.log(EC)
  }

  return (
    <div>
      <div className='flexLigneOF flexLigneV'>
        <div className='textOF'>Economie de charge</div>
        <div className='flexSaisie'>
           <button className='buttonPM' onClick={handlePressEC} > 
              <div className='TextPM'> - </div>
           </button>
           <input className='inputResi'value={EC.toLocaleString().toString()} onChange={handleChangeEC} keyboardType='number-pad'/>
           <button className='buttonPM' onClick={() => {setEC(parseInt(EC)+1000)}}> 
             <div className='TextPM'> + </div>
          </button>
      </div>
     </div>
    </div>
  );
}

export default Resistance;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Okay, I removed your toLocaleString() which is the thing that was preventing you from entering more than 4 numbers.

Then I changed a bit your onChange function. You were converting to a number the value of your field. But you can't convert a null value to an integer. So I updated the state conditionally :

  function handleChangeEC(e) {
    if (e.target.value.length > 0) {
      let newEc = parseInt(e.target.value);
      setEC(newEc);
    } else {
      setEC(0);
    }
  }

If the length of the value of you field is greater than One (So your field is not empty) then set the state. But else, set the state to 0.

This gives you a working field but if your EC variable is equal to 0, the input field has a 0 inside (which I guess you don't want).

I changed the value attribute of your input to value={EC > 0 ? EC.toString() : ""} (If EC > 0, put EC, else put nothing inside)

Here's the complete code :

import { useState } from "react";

function Resistance() {
  const [EC, setEC] = useState(2000);

  function handlePressEC() {
    const testEC = EC - 1000;
    if (testEC <= 0) {
      setEC(0);
    } else {
      setEC(testEC);
    }
  }

  function handleChangeEC(e) {
    if (e.target.value.length > 0) {
      let newEc = parseInt(e.target.value);
      setEC(newEc);
    } else {
      setEC(0);
    }
  }

  return (
    <div>
      <div className="flexLigneOF flexLigneV">
        <div className="textOF">Economie de charge</div>
        <div className="flexSaisie">
          <button className="buttonPM" onClick={handlePressEC}>
            <div className="TextPM"> - </div>
          </button>
          <input
            className="inputResi"
            value={EC > 0 ? EC.toString() : ""}
            onChange={handleChangeEC}
          />
          <button
            className="buttonPM"
            onClick={() => {
              setEC(parseInt(EC) + 1000);
            }}
          >
            <div className="TextPM"> + </div>
          </button>
        </div>
      </div>
      <div>EC : {EC}</div>
    </div>
  );
}

export default Resistance;
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