Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

text box required when radio button is selected

Status
Not open for further replies.

Soti

Programmer
Jul 9, 2001
34
0
0
US
Does anyone know how I can make a text box required if a radio button is selected?
 
sure:

if (document.myFormName.myRadioName.checked &&
!document.myFormName.myTextName.value) {
alert("Please enter text.");
} =========================================================
if (!succeed) try();
-jeff
 
hi, that code generates this error:
line 28:
document.Form has no properties.

This is what I put in the header:
<SCRIPT language=JavaScript>
<!-- Begin
if (document.Form.F1.checked &&
!document.Form.F2.value) {
alert(&quot;Please enter text.&quot;);
}
// End -->

</SCRIPT>
 
form would be the name of your form. if you named it form then rename it _________________________________________________________
for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
 
Another question, How would this script differentiate between the radio buttons. WHat I mean is this , it is one question with 4 selections so, since they are radio buttons they are all F1, if the user chooses that last radio button &quot;other&quot; I want the text box to be mandatory.

thanks
 
you need to loop through the radio buttons and find out which has been selected and then perform your conditioning

example written in here
thread215-468144 _________________________________________________________
for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
 
I don't understand the example that you have referred me to, or how it applies in this case. Thanks
 
Can I differentiate by the value somehow, depending on the which value is chosen then the text box wil be required?
 
alrighty,
alrighty,
let's say you have 4 radios buttons named F1
<input type=&quot;radio&quot; name=&quot;F1&quot; value=&quot;A&quot;> A <br>
<input type=&quot;radio&quot; name=&quot;F1&quot; value=&quot;B&quot;> B <br>
<input type=&quot;radio&quot; name=&quot;F1&quot; value=&quot;C&quot;> C <br>
<input type=&quot;radio&quot; name=&quot;F1&quot; value=&quot;other&quot;> other <br>
add a button to see if they selected whatever
<input type=&quot;button&quot; value=&quot;check&quot; onClick=&quot;runFunction()&quot;>

now you need to loop through the radios in your function first to figure out what you have selected


function runFunction()
{
var radios = document.formname.F1
for( x=0; i < radios.length; x++)
if( radios[x].checked) {
var selectedRadio = radios[x].value;
//test it
alert(selectedRadio);
}
}

now you have something to work off of.
if(selectedRadio == &quot;other&quot;) {
perform the manditory condition
}


_________________________________________________________
for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
 
ha!!!
I did it again, well not really copy/paste form the otehr thread and forgot to fix the i thing
change this line
for( x=0; i < radios.length; x++)
to
for( x=0; x < radios.length; x++)
_________________________________________________________
for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top