Have an idea?

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

Problem with Javascript validation (comparing two numbers given in "other (specify)" boxes

I have two questions "kmpurchased" followed by "kmcurrent" where respondents must state the number of km their second-hand car had when purchased and then the number of km today. I am using select type questions where the first option is "km (specify)" and the second option "I don't know". In the second question ("kmcurrent")I am trying to implement the following code in Javascript custom validation:

var
check=SSI_GetValue("kmcurrent_1_other");
if(isNaN(check)) 
{
var strErrorMessage="Answer must be numeric";
}

if(SSI_GetValue("kmcurrent_1_other") < SSI_GetValue("kmpurchased_1_other"))
{strErrorMessage = "You cannot have less km today than when the car was purchased";}


The first part of this code, checking if respondents' answer is numeric, works flawlessly (thanks to a previous answer I got on this forum). The second part however, aiming at comparing the number of km today with number of km when purchased does no give any results, despite this method being from a previous post on this forum (https://www.sawtoothsoftware.com/forum/1738/javascript-validation-comparing-values-numeric-text-boxes).

Note that I am using version 8.3.4 hence the SSi_GetValue function should work.

Any idea where the mistake is hidden?
asked May 23, 2014 by Albatus Bronze (570 points)
retagged May 23, 2014 by Walter Williams

1 Answer

0 votes
Hi ,

Please use [%kmpurchased_1_other  %] to call previous question value instead of SSI_GetValue("kmpurchased_1_other") . See code below.

var check=SSI_GetValue("kmcurrent_1_other");
if(isNaN(check)) 
{
var strErrorMessage="Answer must be numeric";
}

if(SSI_GetValue("kmcurrent_1_other") < ([%kmpurchased_1_other  %]))
{strErrorMessage = "You cannot have less km today than when the car was purchased";}


But when I look for more detail in your problem, I found some occurrence, when user can choose "I don't know" in any question. So based on your requirment. I would suggest to use below code.

var check=SSI_GetValue("kmcurrent_1_other");
if(isNaN(check)) 
{
    strErrorMessage="Answer must be numeric";
}
if(([% kmpurchased %] ==1) && (SSI_GetValue("kmcurrent") ==1) && ((SSI_GetValue("kmcurrent_1_other")) < (parseInt([%kmpurchased_1_other  %]))))
{
    strErrorMessage = "You cannot have less km today than when the car was purchased";
}


Let me know if you still have question.
answered May 23, 2014 by Rajesh Rana Gold (24,715 points)
edited May 23, 2014 by Rajesh Rana
Ahh, was just typing up the same thing as I've had that problem calling up data from previous questions.
(Y) Jay,
...