Forum Moderators: open

Message Too Old, No Replies

Form validation: if radio "no", then select dropdown req'd

Want the select requirement condition on radio button choice

         

timware

6:37 pm on Oct 19, 2004 (gmt 0)

10+ Year Member



I'm using JS for form validation and I have a radio button that if "No" value is checked, then the next field, a pulldown select, is required. If "Yes" then the pulldown select is not required.

This checks for null and empty:

function isFilled(elm) {
if (elm.value == "" ¦¦
elm.value == null)
return false;
else return true;
}

This checks for the yes/no radio:

var open;
for (var i=0; i<form.Business_open.length; i++) {
if (form.Business_open[i].checked) {
open = 1;
break;
}
}
if (open!= 1) {
alert("Please tell us if your business is open now.");
return false;
}
And this makes the pulldown select required, no matter what. I want it to be conditional on the above radio option being the "no" value; that is, if "no" then select is required:

if(form.When_are_you_opening.options[form.When_are_you_opening.selectedIndex].value == "Select") {

alert("Please tell us when your business is opening.")
form.When_are_you_opening.focus();
return false;
}

Thanks for any help!

Tim

RonPK

8:31 pm on Oct 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you'll need read the value of the checked radio button. Add a line to the for-loop, something like this:

var open;
for (var i=0; i<form.Business_open.length; i++) {
if (form.Business_open[i].checked) {
open = 1;
var radioVal = form.Business_open[i].value;
break;
}
}

radioVal can now be either 'no' or 'yes', presuming these are the values you gave the input elements.

timware

3:56 pm on Oct 21, 2004 (gmt 0)

10+ Year Member



Thanks, but I'm still a bit confused. As you can tell, my JS skills are rudimentary at best. Looking at the code I provided, how do I set the pulldown select to be required only if the "no" value checkbox was checked. I played around with what you provided but got nowhere.

Thanks again!

Tim