PDA

View Full Version : Function Not Working



tomyknoker
09-04-2007, 05:25 AM
Hi there can someone take alook at the following and tell me what I am doing wrong? Basically if a user selects checkbox 2 & checkbox 3 in my form and try to submit, the can only do it if they have also selected a radio button... Just not sure if my combination is wrong...


myRadio = -1;
for (i=form.radio1.length-1; i > -1; i--) {
if((form.checkbox2.checked && form.checkbox3.checked) && form.radio1[i].checked) {
myRadio = i; i = -1;
}
}
if (myRadio == -1) {
theMessage = theMessage + "\n --> You selected checkbox 2 & 3, so you therefore need to select the radio button";
}

rdutton
09-04-2007, 06:24 AM
To be honest your logic seems a little strange for some basic form validation... however please post the entire HTML document or a link to it so I can have a complete look.
Ryan

tomyknoker
09-04-2007, 06:26 AM
It's a confidential form so can't do that... Would this work do you think?



if((form.checkbox2.checked && form.checkbox3.checked) && form.radio1.checked) {
theMessage = theMessage + "\n --> You selected checkbox 2 & 3, so you therefore need to select the radio button";
}

rdutton
09-04-2007, 06:35 AM
Your logic says:
If checkbox1 and checkbox2 and radio1 is checked, then display a message.


if((form.checkbox2.checked && form.checkbox3.checked) && form.radio1.checked) {
theMessage = theMessage + "\n --> You selected checkbox 2 & 3, so you therefore need to select the radio button";
}

I think you need to add a ! in front of 'form.radio1.checked' so that it only gives the message when a radio button is not selected


if((form.checkbox2.checked && form.checkbox3.checked) && !form.radio1.checked) {
theMessage = theMessage + "\n --> You selected checkbox 2 & 3, so you therefore need to select the radio button";
}

Ryan

tomyknoker
09-04-2007, 07:04 AM
Ryan thanks yes that worked, but I realised it's only working for my 1st radio button, there are 5 in the group... I tried this but no luck... I thought this was saying if checkbox 2 & 3 are selected and radio1 or radio2 etc isn't... But it doesn't work...


if((form.checkbox2.checked && form.checkbox3.checked) && !form.radio1.checked || !form.radio2.checked) {
theMessage = theMessage + "\n --> You selected checkbox 2 & 3, so you therefore need to select the radio button";
}

rdutton
09-05-2007, 02:38 AM
I believe this link will help you:

http://www.quirksmode.org/js/forms.html

It specifically describes how to see if a radio button in a group is checked.

Ryan

Twey
09-05-2007, 01:10 PM
I'm liking the quirksmode.org updates less and less every time I read them.

You should note that accessing form elements as document.formname.elementname is bad practice, and liable to cause conflict with other scripts. Instead, use the forms and elements collections (document.forms.formname.elements.elementname).