Really need some help here, can’t figure out how do the last and most important part. Please help with loop through questions after validating certain criteria from 3 grou
I posted my problem yesterday and have not had any replies so I thought I’d try again.
Basically what I want to do is have 3 groups of radio buttons for different questions. If group 1 is not selected you should be alerted that group 1 requires a selection. If group 1, option 1 (value 0) is selected it should return true if options 2 or 3 are not selected. However if group 1 option 2,3,4,5 are selected (values 1,2,3,4) then check if groups 2 and 3 have selections if they dont alert error message they must have selections. If all this is satisfied then it should loop through the remaining questions doing the same thing until all questions are answered correctly and then submits.
I have written the sort of code I would like to find correct syntax/ arguments/ operators etc for however this is a mutant language of my own that obviously doesn’t work. I am not sure you can use id’s for this sort of thing Ihave tryed putting all 3 groups (freq, sev & wi) into one id and Iteratively cycling through
function checkform(form)
{
for (id=1; id<=15; id++) {
if (freq(id)==””){
alert(“You must select the first option for all questions”);
return false;
}
if (freq(id)==0 && (sev(id)!=”” || wi(id)!=””)){
alert(“You must not select options from groups 2 or 3 if option 1 you selected the first option”);
return false;
}
if (freq(id)!=0 && (sev(id)==”” || wi(id)==””)){
alert(“You must select options from groups 2 or 3 if option 1 you selected the anything but the first option”);
}}
return true;
}
I have already achieved all parts of what I wanted to do however I cannot figure out a way to loop through all questions after I have validated the first one see below
BELOW IS MY ORIGINAL THREAD
To anyone that may be able to help this is really doing my head in.
Essentially there are 3 groups of radio buttons for each area of the body, everyone must select an option from the first group for each part of the body. If any other value besides the first value from this group (value=0) is selected the user must select an option from each of the second and third group options for that specific part of the body.
I have managed to do this for one section of the body, however there are another 15 areas that I need to validate all in the same way. Besides re writing the code for each area I know there must be an easier way to do it but for the life of me can’t get my head around it.
Here is my code that works for one area
function checkFrequency() {
var a = document.forms[0].elements;
for(var i = 0 ; i < a.length ; ++i) {
if(a[i].name == “area1”) {
var radiogroup = a[a[i].name];
var itemchecked = false;
for(var j = 0 ; j < radiogroup.length ; ++j) {
if(radiogroup[j].checked){
itemchecked = true;
break;
}
}
if(!itemchecked) {
alert(“Please select an option 0-4 for first group “);
if(a[i].focus)
a[i].focus();
return false;
}
}
}
return checkSevWorkInt(j);
}
function checkSevWorkInt(j) {
if (j!=0) {
var a = document.forms[0].elements;
for(var i = 0 ; i < a.length ; ++i) {
if(a[i].name == “area1group2” || a[i].name == “area1group3”){
var radiogroup = a[a[i].name];
var itemchecked = false;
for(var k = 0 ; k < radiogroup.length ; ++k) {
if(radiogroup[k].checked){
itemchecked = true;
break;
}
}
if(!itemchecked) {
alert(“Please complete all other groups as you have selected an option other then 0 selected for group 1 “);
if(a[i].focus)
a[i].focus();
return false;
}
}
}
}
return true();
}
Here is what I was trying but obviously doesn’t work because simply finds first occurrence where not equal 0
function checkFrequency() {
var a = document.forms[0].elements;
for(var i = 0 ; i < a.length ; ++i) {
if(a[i].name == “area1” || a[i].name == “area2”) {
var radiogroup = a[a[i].name];
var itemchecked = false;
for(var j = 0 ; j < radiogroup.length ; ++j) {
if(radiogroup[j].checked){
itemchecked = true;
break;
}
}
if(!itemchecked) {
alert(“Please select an option 0-4 for first group “);
if(a[i].focus)
a[i].focus();
return false;
}
}
}
return checkSevWorkInt(j);
}
function checkSevWorkInt(j) {
if (j!=0) {
var a = document.forms[0].elements;
for(var i = 0 ; i < a.length ; ++i) {
if(a[i].name == “area1group2” || a[i].name == “area1group3” || a[i].name == “area2group2” || a[i].name == “area2group3”){
var radiogroup = a[a[i].name];
var itemchecked = false;
for(var k = 0 ; k < radiogroup.length ; ++k) {
if(radiogroup[k].checked){
itemchecked = true;
break;
}
}
if(!itemchecked) {
alert(“Please complete all other groups as you have selected an option other then 0 selected for group 1 “);
if(a[i].focus)
a[i].focus();
return false;
}
}
}
}
return true();
}
any help or guidance anyone can give me would be much appreciated. I understand already that I am probably coding to much and there is probably a simpler way to do what I have already done, however the only way I can think of solving my complete problem is by a ridiculous amount of coding
Thanks to everyone