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

230
Views
How to validate and get data of a radio button with input box in reactjs?

I am new to react. I have got a scenario where I have multiple radio button and few of the radio buttons have an input box associated with it. Like in the below image:
Multiple radio button

What I want is:

  • If the user checked any radio button which has an input box associated with it, then that input box will become required field. (display validation message "input is required" on button click. otherwise display data on the console).

  • If everything is okay then On click of save button show data on the
    console.

I have written some code. This code validates if none of the radio button is checked on button click (obvious case). But I am not understanding how to achieve the above requirement.

import React from "react";

const ContactUsForm = () => {
  const isFormValid = () => {
    var radios = document.getElementsByName("myRadio");
    var isValid = false;

    var i = 0;
    while (!isValid && i < radios.length) {
      if (radios[i].checked) isValid = true;
      i++;
    }

    return isValid;
  };

  const handleButtonClick = () => {
    if (!isFormValid()) {
      alert("Select atleast one radio button");
    } else {
      alert("Success");
    }
  };
  return (
    <div>
      {" "}
      <br />
      <label>
        <input type="radio" name="myRadio" value="Graduate" /> I am graduate
      </label>{" "}
      <br />
      <label>
        <input type="radio" name="myRadio" value="Something" /> Some radio button
      </label>{" "}
      <br />
      <label>
        <input type="radio" name="myRadio" />I am graduate from university name
      </label>{" "}
      <input type="text" id="other" />
      <br />
      <label>
        <input type="radio" name="myRadio" />
        Other
      </label>
      <input type="text" id="other2" />
      <br />
      <button onClick={handleButtonClick} type="submit">
        Save
      </button>{" "}
      <br />
    </div>
  );
};

export default ContactUsForm;

Can anybody help me in achieving the above requirement in react? Thank You
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

So you should use React State hook.

For every input, you need to add onClick event.

Declare state:

const [radioBtn, setRadioBtn] = useState(null);

Change state for each button click:

<input type="radio" name="myRadio" value="Graduate"  onClick={() => setRadioBtn('Graduate')}/> I am graduate

In the end, all you need to do is to click Save and you can manage all the stuff you need in handleButtonClick

<button onClick={handleButtonClick} type="submit">
    Save
</button>

Here you can validate all you need. Instead of alert you can use console.log(radioBtn) so you would get output to console.

  const handleButtonClick = () => {
    if (radioBtn) {
      alert(radioBtn);
    } else {
      alert(radioBtn + ' fail');
    }
  }
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!