Have an idea?

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

Validation for depending numeric variables on same page

I need to ask 4 open-ended numeric questions on the same page and to avoid nonsensical answers I need to show a customized error message above each question if the value is greater than the previous one (after the second),  so that :
If the value on Q1 < cutoff point, it says "Please enter a value greater than #"
if the value of Q2<Q1, it says "Please enter a greater value than in Q1".
if the value of Q3<Q2, it says "Please enter a greater value than in Q2".
if the value of Q4<Q3, it says "Please enter a greater value than in Q3".

I need the logic across all 4 questions to Q1<Q2<Q3<Q4.

The respondent should not be able to continue until all those conditions are met on the same page.

Could anybody share a code to do this with javascript or Perl?
asked Sep 4 by rinsights Bronze (740 points)
I'm not sure whether these questions are open-ends or numerics.  Assuming the latter, you should be able to use custom JavaScript verification like this in each of the questions besides the first:

if (SSI_GetValue('Q2') < SSI_GetValue('Q1')) {
    strErrorMessage = 'Please enter a greater value than in Q1.';
}
Thank you, Zach.  Is there a code to style the text with a bigger font or different color so it is more visible?
Adding this CSS to the page works in Lighthouse Studio v9.8.1:

<style>
.question_error_box .question_errors {
    font-size: 24px;
    color: purple;
}
</style>
Thank you, Zach. You are the best!
When I put the same value in all 4 questions, the code doesn't work. Let's me pass. Is it reading < as "less or equal tha". I need it to read "less than". How do I fix that?
The script is working if all answers are the same. If you want the answer to always be larger the change the error script to trigger if <= rather than <. Or are you looking for some other functionality?
I thought  that"<="  meant Less or equal and  "<" meant only Less than. It seems that it's the other way around. Thank you! That works. That's what I needed.
That is what it means.  In the script you are saying to trigger the alert if the answer given is less than or equal to the previous response.  Therefore, they can only continue if the response is greater than the previous response.
Got it. Thanks! Do you know what may be wrong with the code in this one: https://legacy.sawtoothsoftware.com/forum/26912/javascript-validation-numeric-value-based-previous-answer

1 Answer

0 votes
Are these all on the same page or different pages?  You could just set the minimum parameter at Q2 to be [% Q1 %], then at Q3 make the minimum [% Q2 %]  and at Q4 make the minimum [% Q3 %] .  Then the additional error scripting is unnecessary.
answered Sep 4 by Jay Rutherford Gold (37,705 points)
They are all on the same page. Would that logic work on the same page?
No, only different pages.  If on the same page do this.

On Q2 question, click advanced, then click the Call custom javascript box and put this code:

if (SSI_GetValue ("Q2") < SSI_GetValue ("Q1"))
{
var strErrorMessage = "Please enter a greater value than in Q1";
}

The do the same for Q3 and Q4 adjusting the script accordingly.
Thank you! Is there a code to style the text with a bigger font or different color so it is more visible?
Just wrap the error message in a font tag as below.  Play with the size for what you want and any color (or leave either blank).  You can also use an html color code to achieve any color you want.

if (SSI_GetValue ("Q2") < SSI_GetValue ("Q1"))
{
var strErrorMessage = "<font size='18' color='blue'>Please enter a greater value than in Q1</font>";
}
...