/    Sign up×
Community /Pin to ProfileBookmark

Please help looping though questions while validating radio buttons

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

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERApr 02.2009 — I'm having a little trouble understanding exactly what you want to do -- fairly complex.

Perhaps you can give a one or two question synopsis of what type of questions

that the given radio responses will apply toward?

Perhaps that will help me understand the requirements and order of the checks.
Copy linkTweet thisAlerts:
@J1000authorApr 03.2009 — Ok

the webpage is a form with a series of 15 parts of the body of 3 groups of questions for each body part. First group of questions do you have any problems with that part of the body? (five options, first option is basically no and other four is frequency) Secondly If yes how severe is it? (3 options of severity) Thirdly if yes how much does it affect your work? (3 options again)

Therefore if you select no to first part then second two parts must be left blank. If you select yes then both second parts must be answered. This must loop through for all questions.

I hope this explains it well enough. As I was saying with initial code I have worked out how to do most of it except for how to loop through all questions besides rewriting all code and also how to narrow down validation so it can pinpoint which part is wrong

Thanks in advance for your help
Copy linkTweet thisAlerts:
@JMRKERApr 03.2009 — Post the webform so we can see how the questions relate to the perceived user responses.
Copy linkTweet thisAlerts:
@J1000authorApr 06.2009 — <FORM name="testform" action='./pr_emlapd.php' method='post' onsubmit= "return checkFrequency()">

<table align="center" border="1" width="40%">

<tr><td><b>Name:</b></td> <td><input type='text' size='53' name="name"> <br></td></tr>

<tr><td><b>Surname:</b></td> <td><input type='text' size='53' name="surname"> <br></td></tr>

<tr><td><b>Age: </b></td> <td><input type='text' size='53' name="age"> <br></td></tr>

<tr><td><b>Company:</b></td> <td><input type='text' size='53' name='company'> <br></td> </tr>

<tr><td><b>Location:</b></td> <td><input type='text' size='53' name='location'> <br></td></tr>

<tr><td><b>Position:</b></td> <td><input type='text' size='53' name='position'> <br></td></tr>

</table>

<br>


<table width="100%" align="center" border="5">

<tr><td width="550">


<center>During the last week how often did you experience any problems with the following body areas:</center>

</td>

<td>

</td><td width="300">

<center><b>If you experienced any problems how uncomfortable was it?</b></center>

</td>

<td>

</td><td width="275">

<center>did this interfere with your work?</center>

</td>

</tr>

</table>


<table align="center" border="1">

<tr><td valign='top'>


<b>BODY <br> SEGMENT</b>

</td><td>

&nbsp;</td>

<td>

<center>Never</center>

</td><td>

<center>1-2 times last week</center>

</td><td>

<center>3-4 times last week</center>

</td><td>

<center>Once every day</center>

</td><td>

<center>Several times every day</center>

</td><td>

<center><b>Slightly uncomfortable</b></center>

</td><td>

<center><b>Moderately uncomfortable</b></center>

</td><td>

<center><b>Very uncomfortable</b></center>

</td><td>

<center>Not at all</center>

</td><td>

<center>Slightly interfered</center>

</td><td>

<center>Substantially interfered</center>

</td>

</tr>

<tr>

<td>

Neck

</td>

<td>&nbsp;</td>

<td height="40">

<div class="qselections">

<center><input type="radio" value='0' name="neckf"></center>

</div>

</td>

<td height="40">

<div class="qselections">

<center><input type="radio" value='1' name="neckf"></center>

</div>

</td>

<td height="40">

<div class="qselections">

<center><input type="radio" value='2' name="neckf"></center>

</div>

</td>

<td height="40">

<div class="qselections">

<center><input type="radio" value='3' name="neckf"></center>

</div>

</td>

<td height="40">

<div class="qselections">

<center><input type="radio" value='4' name="neckf"></center>

</div>

</td>


<td height="40">

<div class="qselections">

<center><input type="radio" value='1' name="necksev"></center>

</div>

</td><td height="40">

<div class="qselections">

<center><input type="radio" value='2' name="necksev"></center>

</div>

</td><td height="40">

<div class="qselections">

<center><input type="radio" value='3' name="necksev"></center>

</div>

</td>





<td height="40">

<div class="qselections">

<center><input type="radio" value='1' name="neckwi"></center>

</div></td><td height="40">

<div class="qselections">

<center><input type="radio" value='2' name="neckwi"></center>

</div></td><td height="40">

<div class="qselections">

<center><input type="radio" value='3' name="neckwi"></center>

</div></td>



</tr>

<tr>

<td height="40">

Shoulder

</td>

<td>R</td>

<td height="40">

<div class="qselections">

<center><input type="radio" value="0" name="rshoulderf"></center>

</div></td>

<td height="40">

<div class="qselections">

<center><input type="radio" value="1" name="rshoulderf"></center>

</div></td><td height="40">

<div class="qselections">

<center><input type="radio" value="2" name="rshoulderf"></center>

</div></td><td height="40">

<div class="qselections">

<center><input type="radio" value="3" name="rshoulderf"></center>

</div></td><td height="40">

<div class="qselections">

<center><input type="radio" value="4" name="rshoulderf"></center>

</div>

</td>


<td height="40">

<div class="qselections">

<center><input type="radio" value="1" name="rshouldersev"></center>

</div></td><td height="40"><div class="qselections">

<center><input type="radio" value="2" name="rshouldersev"></center>

</div></td><td height="40"><div class="qselections">

<center><input type="radio" value="3" name="rshouldersev"></center>

</div>

</td>


<td height="40">

<div class="qselections">

<center><input type="radio" value="1" name="rshoulderwi"></center>

</div></td><td height="40"><div class="qselections">

<center><input type="radio" value="2" name="rshoulderwi"></center>

</div></td><td height="40"><div class="qselections">

<center><input type="radio" value="3" name="rshoulderwi"></center>

</div>

</td>

</tr>

<tr>

<td height="40">

Shoulder

</td>

<td>L</td>

<td height="40">

<div class="qselections">

<center><input type="radio" value="0" name="lshoulderf"></center>

</div></td>

<td height="40">

<div class="qselections">

<center><input type="radio" value="1" name="lshoulderf"></center>

</div></td><td height="40">

<div class="qselections">

<center><input type="radio" value="2" name="lshoulderf"></center>

</div></td><td height="40">

<div class="qselections">

<center><input type="radio" value="3" name="lshoulderf"></center>

</div></td><td height="40">

<div class="qselections">

<center><input type="radio" value="4" name="lshoulderf"></center>

</div>

</td>


<td height="40">

<div class="qselections">

<center><input type="radio" value="1" name="lshouldersev"></center>

</div></td><td height="40"><div class="qselections">

<center><input type="radio" value="2" name="lshouldersev"></center>

</div></td><td height="40"><div class="qselections">

<center><input type="radio" value="3" name="lshouldersev"></center>

</div>

</td>


<td height="40">

<div class="qselections">

<center><input type="radio" value="1" name="lshoulderwi"></center>

</div></td><td height="40"><div class="qselections">

<center><input type="radio" value="2" name="lshoulderwi"></center>

</div></td><td height="40"><div class="qselections">

<center><input type="radio" value="3" name="lshoulderwi"></center>

</div>

</td>

</tr>

</table>

<table>

<tr><td colspan='2'>&nbsp;</td><td><input type="submit" name='submit' value="Submit" onclick= "return validate()">&nbsp;&nbsp;<input type='reset' name='reset' value='Clear Form'></td></tr>

</table>

</FORM>


Here is most of the form, there are many more body section however hopefully this 3 should be enough to get the idea. Really struggling on getting the full functionality I require so hopefully some1 out there can help me out. Thankyou very much
Copy linkTweet thisAlerts:
@J1000authorApr 06.2009 — <FORM name="apd" action='./pr_emlapd.php' method='post' onsubmit='return checkform(this)'>

<table align="center" border="1">


<tr>

<td>

Neck

</td>

<td>&nbsp;</td>

<td height="40">

<div>

<center><input type="radio" value='0' id='1' name="freq1"></center>

</div>

</td>

<td height="40">

<div>

<center><input type="radio" value='1' id='1' name="freq1"></center>

</div>

</td>

<td height="40">

<div>

<center><input type="radio" value='2' id='1' name="freq1"></center>

</div>

</td>

<td height="40">

<div>

<center><input type="radio" value='3' id='1' name="freq1"></center>

</div>

</td>

<td height="40">

<div>

<center><input type="radio" value='4' id='1' name="freq1"></center>

</div>

</td>


<td height="40">

<div>

<center><input type="radio" value='1' id='1' name="sev1"></center>

</div>

</td><td height="40">

<div>

<center><input type="radio" value='2' id='1' name="sev1"></center>

</div>

</td><td height="40">

<div>

<center><input type="radio" value='3' id='1' name="sev1"></center>

</div>

</td>





<td height="40">

<div>

<center><input type="radio" value='1' id='1' name="wi1"></center>

</div></td><td height="40">

<div>

<center><input type="radio" value='2' id='1' name="wi1"></center>

</div></td><td height="40">

<div>

<center><input type="radio" value='3' id='1' name="wi1"></center>

</div></td>



</tr>

<tr>

<td height="40">

R Shoulder

</td>

<td>R</td>

<td height="40">

<div>

<center><input type="radio" value="a" id='2' name="freq2"></center>

</div></td>

<td height="40">

<div>

<center><input type="radio" value="b" id='2' name="freq2"></center>

</div></td><td height="40">

<div>

<center><input type="radio" value="c" id='2' name="freq2"></center>

</div></td><td height="40">

<div>

<center><input type="radio" value="d" id='2' name="freq2"></center>

</div></td><td height="40">

<div>

<center><input type="radio" value="d" id='2' name="freq2"></center>

</div>

</td>


<td height="40">

<div>

<center><input type="radio" value="a" id='2' name="sev2"></center>

</div></td><td height="40"><div>

<center><input type="radio" value="b" id='2' name="sev2"></center>

</div></td><td height="40"><div>

<center><input type="radio" value="c" id='2' name="sev2"></center>

</div>

</td>


<td height="40">

<div>

<center><input type="radio" value="a" id='2' name="wi2"></center>

</div></td><td height="40"><div>

<center><input type="radio" value="b" id='2' name="wi2"></center>

</div></td><td height="40"><div>

<center><input type="radio" value="c" id='2' name="wi2"></center>

</div>

</td>

</tr>

</table>

<table>

<tr><td colspan='2'>&nbsp;</td><td><input type="submit" name='submit' value="Submit"" >&nbsp;&nbsp;<input type='reset' name='reset' value='Clear Form'></td></tr>

</table>

</FORM>


I was also trying to do it this way thinking I may be able to use id to loop through adn group the 3 different groups of questions for each body area

Thanks
Copy linkTweet thisAlerts:
@JMRKERApr 06.2009 — Take a look at the following. [COLOR="Magenta"][SIZE="4"]It is not finished yet[/SIZE][/COLOR] as I am still evaluating the

radio button logic you want, but see if you can see where I going with it.

It's getting late here and I make more mistakes when I tired, so I'll stop for now.

Let me know if this is a direction you wish to continue in.

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;40 Questions&lt;/title&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?t=206037

function checkFrequency() {
ShowResults();
return false;
}
var Area_Side = [
'Neck:&amp;nbsp;',
'Shoulder:R',
'Shoulder:L',
'Arm:R',
'Arm:L',
'Hand:R',
'Hand:L',
'Leg:R',
'Leg:L',
'Foot:R',
'Foot:L'
// Note: No comma after end entry
];

function writeRow(Locn) {
var tmp = Area_Side[Locn].split(':');
var Area = tmp[0];
var Side = tmp[1];
var str = '&lt;tr&gt;';
str += '&lt;td&gt;'+tmp[0]+'&lt;/td&gt;';
str += '&lt;td&gt;'+tmp[1]+'&lt;/td&gt;';
if (tmp[1] != '&amp;nbsp;') { Area = Side+Area; }
str += '&lt;td class="td_freq"&gt;&lt;input type="radio" value="0" name="'+Area+'_f"&gt;&lt;/td&gt;';
str += '&lt;td class="td_freq"&gt;&lt;input type="radio" value="1" name="'+Area+'_f"&gt;&lt;/td&gt;';
str += '&lt;td class="td_freq"&gt;&lt;input type="radio" value="2" name="'+Area+'_f"&gt;&lt;/td&gt;';
str += '&lt;td class="td_freq"&gt;&lt;input type="radio" value="3" name="'+Area+'_f"&gt;&lt;/td&gt;';
str += '&lt;td class="td_freq"&gt;&lt;input type="radio" value="4" name="'+Area+'_f"&gt;&lt;/td&gt;';

<i> </i>str += '&lt;td class="td_sev"&gt;&lt;input type="radio" value="1" name="'+Area+'_sev"&gt;&lt;/td&gt;';
<i> </i>str += '&lt;td class="td_sev"&gt;&lt;input type="radio" value="2" name="'+Area+'_sev"&gt;&lt;/td&gt;';
<i> </i>str += '&lt;td class="td_sev"&gt;&lt;input type="radio" value="3" name="'+Area+'_sev"&gt;&lt;/td&gt;';

<i> </i>str += '&lt;td class="td_wi"&gt;&lt;input type="radio" value="1" name="'+Area+'_wi"&gt;&lt;/td&gt;';
<i> </i>str += '&lt;td class="td_wi"&gt;&lt;input type="radio" value="2" name="'+Area+'_wi"&gt;&lt;/td&gt;';
<i> </i>str += '&lt;td class="td_wi"&gt;&lt;input type="radio" value="3" name="'+Area+'_wi"&gt;&lt;/td&gt;';
<i> </i>str += '&lt;/tr&gt;';
<i> </i>return str;
}
function getRBtnName(GrpName) {
var sel = document.getElementsByName(GrpName);
var fnd = -1;
var str = '';
for (var i=0; i&lt;sel.length; i++) {
if (sel[i].checked == true) { str = sel[i].value; fnd = i; }
}
// return fnd; // return option index of selection
// comment out next line if option index used in line above <br/>
return str;
}

function ShowResults() {
var str = '';
var tmp = [];
var Area, Side;
var CkList = [];
for (var i=0; i&lt;Area_Side.length; i++) {
tmp = Area_Side[i].split(':');
Area = tmp[0];
Side = tmp[1]; <br/>
if (Side != '&amp;nbsp;') { AreaSide = Side+Area; } else { AreaSide = Area; }
str = AreaSide+',';
str += getRBtnName(AreaSide+'_f')+','+getRBtnName(AreaSide+'_sev')+','+getRBtnName(AreaSide+'_wi');
CkList.push(str);
}
alert(CkList.join('|')); // alert(CkList.join('n'));
}
&lt;/script&gt;

&lt;style type="text/css"&gt;
.td_freq { background-color:yellow;
height:40px;
text-align:center;
}
.td_sev { background-color:pink;
height:40px;
text-align:center;
}
.td_wi { background-color:lightgreen;
height:40px;
text-align:center;
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;!-- FORM name="testform" action='./pr_emlapd.php' method='post' onsubmit= "return checkFrequency()" --&gt;
&lt;FORM name="testform" action='javascript:alert("Success!")' method='post' onsubmit="return checkFrequency()"&gt;

&lt;table align="center" border="1" width="40%"&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Name:&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' size='53' name="name"&gt; &lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Surname:&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' size='53' name="surname"&gt; &lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Age: &lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' size='53' name="age"&gt; &lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Company:&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' size='53' name='company'&gt; &lt;br&gt;&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Location:&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' size='53' name='location'&gt; &lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Position:&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' size='53' name='position'&gt; &lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;br&gt;

&lt;table width="100%" align="center" border="5"&gt;
&lt;tr&gt;
&lt;th width="550" class="td_freq"&gt;
During the last week how often did you experience any problems with the following body areas:
&lt;/th&gt;
&lt;td&gt;&lt;/td&gt;
&lt;th width="300" class="td_sev"&gt;
&lt;b&gt;If you experienced any problems how uncomfortable was it?&lt;/b&gt;
&lt;/th&gt;
&lt;td&gt;&lt;/td&gt;
&lt;th width="275" class="td_wi"&gt;
Did this interfere with your work?
&lt;/th&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;table align="center" border="1"&gt;
&lt;tr&gt;
&lt;th valign='top'&gt;&lt;b&gt;BODY &lt;br&gt; SEGMENT&lt;/b&gt;&lt;/th&gt;
&lt;th&gt;&amp;nbsp;&lt;/th&gt;
&lt;th class="td_freq"&gt; Never&lt;/th&gt;
&lt;th class="td_freq"&gt; 1-2 times last week&lt;/th&gt;
&lt;th class="td_freq"&gt;3-4 times last week&lt;/th&gt;
&lt;th class="td_freq"&gt;Once every day&lt;/th&gt;
&lt;th class="td_freq"&gt;Several times every day&lt;/th&gt;
&lt;th class="td_sev"&gt;&lt;b&gt;Slightly uncomfortable&lt;/b&gt;&lt;/th&gt;
&lt;th class="td_sev"&gt;&lt;b&gt;Moderately uncomfortable&lt;/b&gt;&lt;/th&gt;
&lt;th class="td_sev"&gt;&lt;b&gt;Very uncomfortable&lt;/b&gt;&lt;/th&gt;
&lt;th class="td_wi"&gt;Not at all&lt;/th&gt;
&lt;th class="td_wi"&gt;Slightly interfered&lt;/th&gt;
&lt;th class="td_wi"&gt;Substantially interfered&lt;/th&gt;
&lt;/tr&gt;

&lt;script type="text/javascript"&gt;
for (var i=0; i&lt;Area_Side.length; i++) { document.write(writeRow(i)); }
&lt;/script&gt;

&lt;tr&gt;
&lt;th valign='top'&gt;&lt;b&gt;BODY &lt;br&gt; SEGMENT&lt;/b&gt;&lt;/th&gt;
&lt;th&gt;&amp;nbsp;&lt;/th&gt;
&lt;th class="td_freq"&gt; Never&lt;/th&gt;
&lt;th class="td_freq"&gt; 1-2 times last week&lt;/th&gt;
&lt;th class="td_freq"&gt;3-4 times last week&lt;/th&gt;
&lt;th class="td_freq"&gt;Once every day&lt;/th&gt;
&lt;th class="td_freq"&gt;Several times every day&lt;/th&gt;
&lt;th class="td_sev"&gt;&lt;b&gt;Slightly uncomfortable&lt;/b&gt;&lt;/th&gt;
&lt;th class="td_sev"&gt;&lt;b&gt;Moderately uncomfortable&lt;/b&gt;&lt;/th&gt;
&lt;th class="td_sev"&gt;&lt;b&gt;Very uncomfortable&lt;/b&gt;&lt;/th&gt;
&lt;th class="td_wi"&gt;Not at all&lt;/th&gt;
&lt;th class="td_wi"&gt;Slightly interfered&lt;/th&gt;
&lt;th class="td_wi"&gt;Substantially interfered&lt;/th&gt;
&lt;/tr&gt;

&lt;/table&gt;

&lt;table&gt;
&lt;tr&gt;
&lt;td colspan='2'&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;
&lt;input type="submit" name='submit' value="Submit"&gt; &lt;!-- onclick="ShowResults()" --&gt;
&amp;nbsp;&amp;nbsp;
&lt;input type='reset' name='reset' value='Clear Form'&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;/FORM&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@J1000authorApr 06.2009 — Thank you so much for your fast reply JMRKER I am now trying to understand exactly where you are going. Obviously because it is not finished it is a little tricky to understand exact output or validation as this is what I was having trouble with. However see how I go.

What I can see is that you have split it up into side and area and grouped them by name into a string and from the alert of the string I can see that through this code it still seperates the 3 questions for each area but they are grouped together as values per group ie [RArm,2,2,2] in the string which is what I need as then you can search the string and for any given area the following conditions must be met to proceed

if first value null then alert they need to enter a value for all areas of body for first part ie [COLOR="red"] incorrect [RArm,,,] [/COLOR] correct [RArm,2,1,2]

if first value is 0 then both second values must be null to have success. [COLOR="red"] incorrect [RArm,0,1,2] [/COLOR] correct [RArm,0,,]

Whereas if the first value is 1,2,3or4 then both the other values must be either 1,2or3 and cannot be null. [COLOR="red"] incorrect [RArm,1,,] [/COLOR] correct [RArm,1,3,2]

I am not sure if I fully grasped your method so please don't hesitate to ask for any more info. Nevertheless I am extremely grateful for your assistance

Thanks again
Copy linkTweet thisAlerts:
@JMRKERApr 06.2009 — Two questions:

  • 1. Do you really need the second column of your input table?

    It seems to be used only for R || L.

    Could you put that into the 1st column along with the area, like this:

    Shoulder - Right

    It would simplify the code testing a bit.


  • 2. Instead of blank entries like [RArm,0,,]

    it would be easier to test for a non-blank field.


    Can the checks be for '-' instead of blank, like: [RArm,0.-,-] ?

    It does not effect the display but might make for better test logic.
  • Copy linkTweet thisAlerts:
    @JMRKERApr 06.2009 — Version 2;
    <i>
    </i>&lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;40 Questions&lt;/title&gt;
    &lt;script type="text/javascript"&gt;
    // From: http://www.webdeveloper.com/forum/showthread.php?t=206037

    function checkFrequency() {
    return ShowResults();
    }
    var Area_Side = [
    'Neck:',
    'Shoulder:R',
    'Shoulder:L',
    'Arm:R',
    'Arm:L',
    'Hand:R',
    'Hand:L',
    'Leg:R',
    'Leg:L',
    'Foot:R',
    'Foot:L' // Note: No comma after end entry
    ];

    function writeRow(Locn) {
    var tmp = Area_Side[Locn].split(':');
    var Area = tmp[0]; var Side = tmp[1];
    var str = '&lt;tr&gt;';
    str += '&lt;td&gt;'+tmp[0]+'&lt;/td&gt;';
    if (Side == '') {
    str += '&lt;td&gt;&amp;nbsp;&lt;/td&gt;';
    } else {
    Area = Side+Area;
    str += '&lt;td&gt;'+Side+'&lt;/td&gt;';
    }
    str += '&lt;td class="td_freq"&gt;&lt;input type="radio" value="0" name="'+Area+'_f"&gt;&lt;/td&gt;';
    str += '&lt;td class="td_freq"&gt;&lt;input type="radio" value="1" name="'+Area+'_f"&gt;&lt;/td&gt;';
    str += '&lt;td class="td_freq"&gt;&lt;input type="radio" value="2" name="'+Area+'_f"&gt;&lt;/td&gt;';
    str += '&lt;td class="td_freq"&gt;&lt;input type="radio" value="3" name="'+Area+'_f"&gt;&lt;/td&gt;';
    str += '&lt;td class="td_freq"&gt;&lt;input type="radio" value="4" name="'+Area+'_f"&gt;&lt;/td&gt;';

    <i> </i>str += '&lt;td class="td_sev"&gt;&lt;input type="radio" value="1" name="'+Area+'_sev"&gt;&lt;/td&gt;';
    <i> </i>str += '&lt;td class="td_sev"&gt;&lt;input type="radio" value="2" name="'+Area+'_sev"&gt;&lt;/td&gt;';
    <i> </i>str += '&lt;td class="td_sev"&gt;&lt;input type="radio" value="3" name="'+Area+'_sev"&gt;&lt;/td&gt;';

    <i> </i>str += '&lt;td class="td_wi"&gt;&lt;input type="radio" value="1" name="'+Area+'_wi"&gt;&lt;/td&gt;';
    <i> </i>str += '&lt;td class="td_wi"&gt;&lt;input type="radio" value="2" name="'+Area+'_wi"&gt;&lt;/td&gt;';
    <i> </i>str += '&lt;td class="td_wi"&gt;&lt;input type="radio" value="3" name="'+Area+'_wi"&gt;&lt;/td&gt;';
    <i> </i>str += '&lt;/tr&gt;';
    <i> </i>return str;
    }
    function getRBtnName(GrpName) {
    var sel = document.getElementsByName(GrpName);
    var fnd = -1;
    var str = '';
    for (var i=0; i&lt;sel.length; i++) {
    if (sel[i].checked == true) { str = sel[i].value; fnd = i; }
    }
    return fnd; // return option index of selection
    // comment out next line if option index used in line above <br/>
    // return str;
    }

    function ShowResults() {
    var str = ''; var msg = '';
    var grp0 = ''; var grp1 = ''; var grp2 = ''; errAt = -1;
    var tmp = [];
    var Area, Side;
    var CkList = [];

    // for (var i=Area_Side.length-1; i&gt;=0; i--) {
    for (var i=0; i&lt;Area_Side.length; i++) {

    <i> </i>tmp = Area_Side[i].split(':');
    <i> </i>Area = tmp[0];
    <i> </i>Side = tmp[1];
    <i> </i>if (Side != '') { AreaSide = Side+Area; } else { AreaSide = Area; }
    <i> </i>str = AreaSide+',';

    /* following test logic is applied:
    if first value null then alert they need to enter a value for all areas of body for first part
    ie incorrect [RArm,,,] correct [RArm,2,1,2]
    if first value is 0 then both second values must be null to have success.
    incorrect [RArm,0,1,2] correct [RArm,0,,]
    Whereas if the first value is 1, 2, 3 or 4 then both the other values must be either 1, 2 or 3 and cannot be null.
    incorrect [RArm,1,,] correct [RArm,1,3,2]
    */
    grp0 = getRBtnName(AreaSide+'_f');
    if (grp0 &lt; 0) {
    str += '-,,'; errAt = i; msg = Area+' ('+Side+') frequency question not answered'; }

    <i> </i>if (grp0 == '0') { str += '0,,'; } // if 'NEVER', then ignore rest of RBtn in row
    <i> </i>else {
    <i> </i> grp1 = getRBtnName(AreaSide+'_sev');
    <i> </i> grp2 = getRBtnName(AreaSide+'_wi');
    <i> </i> if ((grp1 &lt; 0) || (grp2 &lt; 0)) { errAt = i; msg = Area+' ('+Side+') is missing qualifier'; }
    <i> </i> str += grp0+','+(grp1+1)+','+(grp2+1);
    <i> </i>}
    <i> </i>CkList.push(str);
    }

    if (errAt &lt; 0) {
    alert(CkList.join('|')); // alert(CkList.join('n'));
    return true;
    } else {
    alert(msg); <br/>
    return false;
    }
    }
    &lt;/script&gt;

    &lt;style type="text/css"&gt;
    .td_freq { background-color:yellow;
    height:40px;
    text-align:center;
    }
    .td_sev { background-color:pink;
    height:40px;
    text-align:center;
    }
    .td_wi { background-color:lightgreen;
    height:40px;
    text-align:center;
    }
    &lt;/style&gt;

    &lt;/head&gt;
    &lt;body&gt;
    &lt;!-- FORM name="testform" action='./pr_emlapd.php' method='post' onsubmit= "return checkFrequency()" --&gt;
    &lt;FORM name="testform" action='javascript:alert("Success!")' method='post' onsubmit="return checkFrequency()"&gt;

    &lt;table align="center" border="1" width="40%"&gt;
    &lt;tr&gt;&lt;td&gt;&lt;b&gt;Name:&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' size='53' name="name"&gt; &lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;&lt;b&gt;Surname:&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' size='53' name="surname"&gt; &lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;&lt;b&gt;Age: &lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' size='53' name="age"&gt; &lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;&lt;b&gt;Company:&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' size='53' name='company'&gt; &lt;/td&gt; &lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;&lt;b&gt;Location:&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' size='53' name='location'&gt; &lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;&lt;b&gt;Position:&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' size='53' name='position'&gt; &lt;/td&gt;&lt;/tr&gt;
    &lt;/table&gt;
    &lt;br&gt;

    &lt;table width="100%" align="center" border="2"&gt;
    &lt;tr&gt;
    &lt;th colspan="2"&gt;BODY&lt;br&gt;SEGMENT&lt;/th&gt;
    &lt;th colspan="5" class="td_freq"&gt;
    During the last week how often did you experience any problems with the following body areas:
    &lt;/th&gt;
    &lt;th colspan="3" class="td_sev"&gt;
    &lt;b&gt;If you experienced any problems how uncomfortable was it?&lt;/b&gt;
    &lt;/th&gt;
    &lt;th colspan="3" class="td_wi"&gt;
    Did this interfere with your work?
    &lt;/th&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
    &lt;th&gt;&amp;nbsp;&lt;/th&gt;
    &lt;th&gt;&amp;nbsp;&lt;/th&gt;
    &lt;th class="td_freq"&gt; Never&lt;/th&gt;
    &lt;th class="td_freq"&gt; 1-2 times last week&lt;/th&gt;
    &lt;th class="td_freq"&gt;3-4 times last week&lt;/th&gt;
    &lt;th class="td_freq"&gt;Once every day&lt;/th&gt;
    &lt;th class="td_freq"&gt;Several times every day&lt;/th&gt;
    &lt;th class="td_sev"&gt;&lt;b&gt;Slightly uncomfortable&lt;/b&gt;&lt;/th&gt;
    &lt;th class="td_sev"&gt;&lt;b&gt;Moderately uncomfortable&lt;/b&gt;&lt;/th&gt;
    &lt;th class="td_sev"&gt;&lt;b&gt;Very uncomfortable&lt;/b&gt;&lt;/th&gt;
    &lt;th class="td_wi"&gt;Not at all&lt;/th&gt;
    &lt;th class="td_wi"&gt;Slightly interfered&lt;/th&gt;
    &lt;th class="td_wi"&gt;Substantially interfered&lt;/th&gt;
    &lt;/tr&gt;

    &lt;script type="text/javascript"&gt;
    for (var i=0; i&lt;Area_Side.length; i++) { document.write(writeRow(i)); }
    &lt;/script&gt;

    &lt;tr&gt;
    &lt;th colspan="2"&gt;BODY&lt;br&gt;SEGMENT&lt;/th&gt;
    &lt;th class="td_freq"&gt; Never&lt;/th&gt;
    &lt;th class="td_freq"&gt; 1-2 times last week&lt;/th&gt;
    &lt;th class="td_freq"&gt;3-4 times last week&lt;/th&gt;
    &lt;th class="td_freq"&gt;Once every day&lt;/th&gt;
    &lt;th class="td_freq"&gt;Several times every day&lt;/th&gt;
    &lt;th class="td_sev"&gt;&lt;b&gt;Slightly uncomfortable&lt;/b&gt;&lt;/th&gt;
    &lt;th class="td_sev"&gt;&lt;b&gt;Moderately uncomfortable&lt;/b&gt;&lt;/th&gt;
    &lt;th class="td_sev"&gt;&lt;b&gt;Very uncomfortable&lt;/b&gt;&lt;/th&gt;
    &lt;th class="td_wi"&gt;Not at all&lt;/th&gt;
    &lt;th class="td_wi"&gt;Slightly interfered&lt;/th&gt;
    &lt;th class="td_wi"&gt;Substantially interfered&lt;/th&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
    &lt;th colspan="2"&gt;
    &lt;input type="submit" name='submit' value="Submit"&gt;
    &lt;/th&gt;
    &lt;td colspan="10"&gt;&amp;nbsp;&lt;/td&gt;
    &lt;th&gt;
    &lt;input type='reset' name='reset' value='Clear Form'&gt;
    &lt;/th&gt;
    &lt;/tr&gt;
    &lt;/table&gt;

    &lt;/FORM&gt;
    &lt;/body&gt;
    &lt;/html&gt;
    Copy linkTweet thisAlerts:
    @J1000authorApr 07.2009 — Thanks again JMRKER

    I am in the process of testing this code to see how it works this morning and will get back to you asap. In reference to yourr questions

  • 1. Do you really need the second column of your input table?

    It seems to be used only for R || L.

    Could you put that into the 1st column along with the area, like this:

    Shoulder - Right

    It would simplify the code testing a bit.


  • This is fine. No an extra column for R and L is not essential or even necessary

  • 2. Instead of blank entries like [RArm,0,,]

    it would be easier to test for a non-blank field.

    Can the checks be for '-' instead of blank, like: [RArm,0.-,-] ?

    It does not effect the display but might make for better test logic.


  • Again this is fine we can fill the null fields with - and simply test for that so if [RArm,0,-,-] is correct then both [RArm,0,-,2] and [RArm,-,2,1] would not be correct and user would be prompted to correct the issue

    Thanks again I will reply again shortly as soon as I have a chance to properly test all. At first glance and usage it seems to work well

    Thankyou for everything
    Copy linkTweet thisAlerts:
    @J1000authorApr 07.2009 — This works great. I have thoroughly tested most possibilities and it seems to work for everything except if they select Never and one or both the options from the other two columns ie [RArm,0,,1]. I take it this was what you were referring to in the questions to initialise the - value instead of null.

    Am very happy with the capabilities of this form now and think this is the last problem for my initial question. Hopefully if we can get this sorted then I can start to add further validation for names and so forth (I can do all this) and test the output which is what I am working on at the moment.

    Thanks so much it is all coming together
    Copy linkTweet thisAlerts:
    @JMRKERApr 07.2009 — This works great. I have thoroughly tested most possibilities and it seems to work for everything except if they select Never and one or both the options from the other two columns ie [RArm,0,,1]. I take it this was what you were referring to in the questions to initialise the - value instead of null.

    .....
    [/QUOTE]


    [CODE]
    if (grp0 == '0') { str += '0,,'; } // if 'NEVER', then ignore rest of RBtn in row
    [/CODE]


    I initialized the report value for the row to be blank regardless of the other 2 groups if the NEVER selection was chosen in the first group.


    I do not even bother to test the last 2 groups if it does not appear to be a factor.

    Note, if you need the '-' change to str += '0,-,-'; above.

    You're most welcome to the help.

    Glad I was able to understand the question after you provided the examples.

    Good Luck!

    ?
    Copy linkTweet thisAlerts:
    @J1000authorApr 07.2009 — Thanks again that solved all parts of my original question for now... You've made my life a lot easier

    ?
    ×

    Success!

    Help @J1000 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...