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

79
Views
Validating form inputs with React

I am trying to check that the required fields are not empty and making sure that the input type is correct.

const CreateSensor = () => {
    const [deveui, setDeveui] = useState('');
    const [location, setLocation] = useState('');
    const [levelid, setLevel] = useState('');

const submitValue = () => {

    let data = {deveui,location,levelid};
    //POST method
    fetch("api")
          
    ClearFields();      
}
function ClearFields(){
    document.getElementById("dev").value = "";
    document.getElementById("location").value = "";
    document.getElementById("level").value = "";
}

return(
    <>
    <hr/>
    <input type="text" id="dev" placeholder="deveui" onChange={e => setDeveui(e.target.value)} />
    <input type="text" id="location"placeholder="Location" onChange={e => setLocation(e.target.value)} />
    <input type="text" id="level" placeholder="Levelid" onChange={e => setLevel(e.target.value)} /> 
    <button onClick={submitValue}>Submit</button>
    </>
    )
}

the submit button will check whether deveui is not empty and the levelid is set to an integer. I have tried changing the input type for levelid to numbers but there is arrows on it which I feel is unnecessary.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I strongly recommend using a React form library. Here's an example with react-hook-form

import { useForm } from "react-hook-form";

const CreateSensor = () => {
  const {
    register,
    handleSubmit,
    watch,
    reset,
    formState: { errors },
  } = useForm({ defaultValues: { deveui: "", location: "", levelid: "" } });

  const submitValue = ({deveui, location, levelid}) => {    
    // exclude 'deveui' from fetch payload
    const payload = { location, levelid }

    // POST data to api, for example
    fetch("https://myapi.com", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(payload),
      //   reset form state
    }).then((response) => reset());
  };

  return (
    <>
      <hr />
      <form onSubmit={handleSubmit(submitValue)}>
        <input
          {...register("deveui", { required: true })}
          type="text"
          id="dev"
          placeholder="deveui"
        />
        <input
          {...register("location", { required: true })}
          type="text"
          id="location"
          placeholder="Location"
        />
        <input
          {...register("levelid", { required: true })}
          type="text"
          id="level"
          placeholder="Levelid"
        />
        <button type="submit">Submit</button>
      </form>
    </>
  );
}


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!