Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

JavaScript verfication in constructed question

Hi All,

I got a constructed question with 1 column and 7 rows (rows are numeric input with constant sum).  Rows are visible depending on selection in earlier question. I got a problem with JavaScript verification as my code doesn't work with this constructed question. When I changed it to 'normal' row list the code works fine.
Can anybody advise  sth ?

In the below code "Q54" is a grid constructed question and "Q54unable"  is a single checkbox question.


if
((document.mainform["Q54_r1_c1"].value).length == 0 && 
(document.mainform["Q54_r2_c1"].value).length == 0 && 
(document.mainform["Q54_r3_c1"].value).length == 0 && 
(document.mainform["Q54_r4_c1"].value).length == 0 && 
(document.mainform["Q54_r5_c1"].value).length == 0 && 
(document.mainform["Q54_r6_c1"].value).length == 0 && 
(document.mainform["Q54_r7_c1"].value).length == 0 && 

SSI_GetValue ("Q54unable_one_1")== 0)

{
var strErrorMessage = "Please answer on Q54 OR select that you are not able to answer";
}
asked Dec 8, 2014 by robson Bronze (700 points)

1 Answer

0 votes
Whilst I'm not the most experienced JavaScript programmer, this example may assist?

I use a similar technique in a constant sum question but instead of having a check box to tick, I have a "don't know" entry as part of the constant sum question. No check box required.

I use the following JavaScript verification which works fine (I'll try to make it look something like your example) ...
var count=0

if(document.mainform["Q54_r8_c1"])
 {
  if((document.mainform["Q54_r8_c1"].value>0)&&(document.mainform["Q54_r8_c1"].value<100))
   {
    count=count+1
   }
 }

if (count>0)
  {
   strErrorMessage="You must select a response for Don't know of 0% or 100%. Please review your answers before continuing.";
  }
else
 {
  strErrorMessage="";
 }

This example assumes "Q54_r8_c1" represents the "Don't know" entry. It can only take on 2 values - 0% or 100%. The JavaScript verification code checks for this. And it bases the condition on the "Don't know" code as it will always appear in the question.

Looking at your script, I don't think you need to check the ".length". Can't you apply the conditions by using "document.mainform["Q54_r8_c1"].value"?

And I suspect part of your problem is this is a constructed list and your conditions are assuming all codes are present, hence why it works off the parent list and not the constructed list.
answered Dec 8, 2014 by Paul Moon Platinum (74,405 points)
Thank you Paul for your answer. Interesting approach and probably I don't need ".length" - good point, nevertheless I still  will look for the solution that can sort out the error message with the constructed list. I hope an expert in JavaScript finds this e-mail...
I didn't want to leave you guessing so ...

I just tested a couple of simple examples using multiple numeric questions with a check box on the same page and then I performed the same test with a grid question (using the rows as a constructed list) and a check box on the same page. I got it all to work.

Use this ...
var count=SSI_GetValue("Q54_r1_c1")+SSI_GetValue("Q54_r2_c1")+SSI_GetValue("Q54_r1_c1")+SSI_GetValue("Q54_r4_c1")+SSI_GetValue("Q54_r5_c1")+SSI_GetValue("Q54_r6_c1")+SSI_GetValue("Q54_r7_c1");

if ((count==0 && SSI_GetValue("Q54unable_one_1")==0) || (count>0 && SSI_GetValue("Q54unable_one_1")==1))

{
var strErrorMessage="Please answer on Q54 OR select that you are not able to answer";
}
Wow ! Thank you Paul ! You are smart guy.  Just slightly different approach and Voila !   Respect and thank you once again !
...