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

109
Visualizações
IF statement on submit in React

I have this code and I'm trying to add some IF statements to the submit button, so it does not submit anything empty, or other than numbers and arithmetic operators, or shows error when operands are more than operators and vice versa, however it doesn't seem to work. Here is the full code.

const handleSubmit = (e) => {
  if (inputValue.length === 0) {
    e.preventDefault();
    setResult("You did not enter anything!")
  }
  if (inputValue === isNaN && inputValue !== validOperators) {
    e.preventDefault();
    setResult("only numbers!")
  } else {
    e.preventDefault();
    setResult(solution);
    setInputValue("");
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

isNan is not a value, it's a function. You need to call it.

I made the assumption that validOperators is an array or a string containing the valid operators.

If so, this is what you want:

const handleSubmit = (e) => {
if (inputValue.length === 0) {
  e.preventDefault();
  setResult("You did not enter anything!")
} if (isNaN(inputValue) || !validOperators.includes(inputValue)) {
  e.preventDefault();
  setResult("only numbers (and valid operators)!")
} else {
  e.preventDefault();
  setResult(solution);
  setInputValue("");
 }
}

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