Have an idea?

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

Creating a validation loop

Hi there,

Hopefully I'm about 2 tweaks away from getting this loop to work in a JS validation:

var strErrorMessage = "";

var i;
var S1I1ct=0;

for (i = 0; i < 15; i++) {
  if ([% "S1I1_" %]+ i == 1){
    S1I1ct = S1I1ct +1;

var NoneofThese = SSI_GetValue("S1I1NONE_1");

if(S1I1ct>0 && NoneofThese == 1)
    strErrorMessage = "You have selected at least one space and also selected 'None of These', please deselect and try again.";

The for loop isn't working because I'm not declaring properly. I just don't know the correct way to get it to declare. Basically I want S1I1_1 thru S1I1_15 to be checked in the loop but I can't get the variable lookup to work correctly. Any help?

closed with the note: All sorted!
asked Oct 22 by crawliano Bronze (505 points)
closed Oct 22 by crawliano
S1T1 and S1T1NONE are both questions on this page, right?  Or is S1T1 on a previous page?
All on the same page.
I'm also suspecting that this is one hell of a mish-mash of the wrong references to the variables!

1 Answer

+1 vote
It looks like there may be some confusion between JavaScript and Sawtooth Script.  The former is able to grab information off the current page, while the latter is used to grab information off the server about other pages, lists, etc.  If both of your questions are on this page, we'll only need to use JavaScript to read the current responses.  Try to replace this:

[% "S1T1_" %] + i

with this:

SSI_GetValue('S1T1_' + i);

Additionally, your current for loop will iteration between i=0 and i=14.  While zero-based counting is common in a lot of places in programming, Lighthouse Studio variables are one-based so we'll want these values to be one higher.  Try this:

for (i = 1; i <= 15; i++) {

Finally, I noticed that you have mismatched brace characters.  I would add a "}" between lines 9 and 10, and another after line 17.

Please give those changes a shot and we'll where that gets us.
answered Oct 22 by Zachary Platinum Sawtooth Software, Inc. (156,375 points)

Thank you :-)