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

119
Visualizações
How to remain the textfield to 0? Clearing up the textfield will cause another value to isNaN

If I leave this as a blank field, this will cause the total to display as isNaN and I don't want the form to be submitted if it is an isNaN. How do I prevent the form from being submitted if the total value shows as isNaN?

export default function BasicTextFields() {
  const [fee, setFee] = useState(0);
  const amount = parseInt(1000);
  const total = Number(amount) + Number(fee);
  const handleSubmit = async (e) => {
    e.preventDefault();
    console.log(" submit");
  };
  return (
    <form onSubmit={{ handleSubmit }}>
      <TextField
        label="Fee"
        type="number"
        value={fee}
        onChange={(e) => setFee(parseInt(e.target.value))}
        InputProps={{
          inputProps: {
            min: 0
          }
        }}
      />
      <button type="submit">Submit</button>
      <br />
      Total: {total}
    </form>
  );
}

codesandbox: https://codesandbox.io/s/basictextfields-material-demo-forked-7sfdph?file=/demo.js:171-809

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use

 onChange={(e) => {
          if(e.target.value===""){
            setFee(0);
          }else{
          setFee(parseInt(e.target.value))}}
        }
about 4 years ago · Juan Pablo Isaza Relatório

0

import React, { useState } from "react";
import Box from "@mui/material/Box";
import TextField from "@mui/material/TextField";

export default function BasicTextFields() {
  const [fee, setFee] = useState(0);
  const amount = parseInt(1000);
  const total = Number(amount) + Number(fee);

  // async is not required if you are not using await keywork in function.
  const handleSubmit = (e) => {
    e.preventDefault();
    if (isNaN(fee)) {
      console.log("its not a number");
    } else {
      console.log("its a number");
      
      // this submit will refresh page.
      // e.target.submit();


      // if you do not want page to refresh.
      // remeber there is different approch for image data.
      var formData = new FormData();
      formData.append('fee', fee);
      console.log(formData);
      // continue with sending data to your backend server
    }
  };

  const changeFee = (e) => {
    setFee(e.target.value);
  }
  return (
    // use only one curly braces
    <form onSubmit={handleSubmit}>
      <TextField
        label="Fee"
        type="text"
        value={fee}
        onChange={changeFee}
        InputProps={{
          inputProps: {
            min: 0
          }
        }}
      />
      <button type="submit">Submit</button>
      <br />
      Total: {total}
    </form>
  );
}

read the all comments mentioned in above code maybe it helps.

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