Need to make a textbox required if radio button is selected
Hello all. New user (and new to javascript) looking for some guidance.
I have a form with a radio button list that contains 4 selections (A,B,C and "Other"). The "Other" option has a textbox next to it that I want to make required if the "Other" option is selected from the list.
<html>
<head>
<title>RBtn Other Selection</title>
<script type="text/javascript">
function ShowSpan(IDS,flag) {
IDS += 'Other';
var sel = document.getElementById(IDS);
if (flag == true) { sel.style.display = 'inline'; }
else { sel.style.display = 'none'; }
}
/*
Validation function not coded
Process:
Look at status of each 'RBtnPick#'.
If checked, use value
If value is '', then use contents of 'RBtnPickOther#' instead
If other text value is blank, then fail validation until filled or 'other' button is NOT checked.
*/
</script>
</head>
<body>
Please choose one of the following:<br>
<input type="radio" value="A" name="RBtnPick1" onclick="ShowSpan(this.name,false)">A
<input type="radio" value="B" name="RBtnPick1" onclick="ShowSpan(this.name,false)">B
<input type="radio" value="C" name="RBtnPick1" onclick="ShowSpan(this.name,false)">C
<input type="radio" value="" name="RBtnPick1" onclick="ShowSpan(this.name,true)">Other
<span id="RBtnPick1Other" style="display:none">
Why? <input type="text" id="RBtnPickOther1" value="">
</span><p>
<input type="radio" value="A" name="RBtnPick2" onclick="ShowSpan(this.name,false)">A
<input type="radio" value="B" name="RBtnPick2" onclick="ShowSpan(this.name,false)">B
<input type="radio" value="C" name="RBtnPick2" onclick="ShowSpan(this.name,false)">C
<input type="radio" value="" name="RBtnPick2" onclick="ShowSpan(this.name,true)">Other
<span id="RBtnPick2Other" style="display:none">
Why? <input type="text" id="RBtnPickOther2" value="">
</span><p>
</body>
</html>
This may work. Not exactly as I thought, but it may do the job. If I make the input required, since the "Other" option has a null value it would fail upon submit...right?
This may work. Not exactly as I thought, but it may do the job. If I make the input required, since the "Other" option has a null value it would fail upon submit...right?
Which 'input' are you referring to? The radio buttons or the text box?
Question: I thought you only wanted the 'Other' radio button to be
accepted if the text box has something other than a blank string.
All other cases should ignore the "Other" text information.
Bookmarks