Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!