0

How do I make an input/text field mandatory(required) when the radio button assigned to it is checked/selected?

I need an input field to become mandatory once the corresponding radio button is selected:

Radio Button 1 Text Field 1
Radio Button 2 Text Field 2
Radio Button 3 Text Field 3

So if Radio button 1 is selected, I need Text Field 1 to become mandatory. If Radio button 2 is selected, I need Text Field 2 to become mandatory and so on.

See the actual code below:

Please confirm your identity by selecting/using one of the below Government issued identification. Upon selecting an identity type, please fill in the text field next to it.

<TR>
    <TD width="253"><input type="radio" required name="IdentityType" id="IdentityType" value="Australian driver's licence number">
        <label for="IdentityType">**Australian driver's licence number:**</label> 
    </TD>
    <TD width="672">
        <span id="sprytextfield1">
            <input name="IdentityValue" type="text"  id="IdentityValue" value="" />
        </span>
    </TD>
</TR>

<TR>
    <TD style="text-indent:22px"> 
        <label for="IdentityType">**Issue state or territory:**</label>
    </TD>
    <TD>
        <input name="IdentityValue" type="text"  id="IdentityValue" value="" />
    </TD>
</TR>
<TR>
    <TD>
        <input type="radio" required name="IdentityType" id="IdentityType" value="Australian passport number">
        <label for="IdentityType">**Australian passport number:**</label>
    </TD>
    <TD>
        <input name="IdentityValue" type="text"  id="IdentityValue" value="" />
    </TD>
</TR>

<TR>
    <TD>
        <input type="radio" required name="IdentityType" id="IdentityType" value="Immicard number">
        <label for="IdentityType">**Immicard number:** </label>
    </TD>
</TR>
EF_1000
  • 45
  • 1
  • 6
  • 2
    Toggle the `required` attribute. See: http://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript – Matt Mokary Feb 09 '17 at 04:25
  • Thank you but not quite what I'm looking for @MattMokary. I need a input field to become mandatory once the corresponding radio button is selected. See example below: Radio Button 1 Text Field 1 Radio Button 2 Text Field 2 Radio Button 3 Text Field 3 So if Radio button 1 is selected, I need Text Field 1to become mandatory. If Radio button 2 is selected, I need Text Field 2 to become mandatory and on. – EF_1000 Feb 09 '17 at 06:42

1 Answers1

0

Let's pretend you have HTML like this

<input type="radio"  id="radio1"  />
<input type="textbox" value="t1"/>
<script>
if(document.getElementById('radio1').checked) {
 document.getElementById("t1").required = true;
}else  {
  document.getElementById("t1").required = false;
}</script>
Jaini
  • 90
  • 10