Have an idea?

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

Javascript verification when variable called may be blank

I have a numeric grid question (QA7) that also has a Not Applicable option (QA7DK)  from the Question Library.  In the next numeric grid question (QA8) I am returning an error message if the QA8 total doesn't match the QA7 total.  That seems to be working if a response was given at QA7.  However, if QA7DK was selected there is no error at QA8 but I just can't move forward.  I'm sure it has to do with the A7 variables being blank but not sure how to fix it.

2nd question, how can I also return the QA7TOT in the error message?

var QA7TOT=[% QA7_r1_c1 %]+[% QA7_r2_c1 %]+[% QA7_r3_c1 %];
var QA8Row1=SSI_GetValue("QA8_r1_c1");
var QA8Row2=SSI_GetValue("QA8_r2_c1");
var QA8TOT=QA8Row1+QA8Row2;
if ([% QA7DK_1 %]==0 && QA8TOT!=QA7TOT)
{strErrorMessage="The Total must match the total of the previous question";}
asked Oct 25, 2022 by Jay Rutherford Platinum (52,770 points)
One easy way to handle blank Sawtooth Script responses in JavaScript is to surround them with the Number function.  Because Number returns 0 when no arguments are provided, this will effectively convert blanks to zeroes:

Number([% NumericField %])

Is this enough for the error message?

var QA7TOT = ...;
strErrorMessage = "The total must be " + QA7TOT + ".";
Perfect!  I knew converting it to a number was the answer I just couldn't remember how to apply it.  Error message is exactly what I needed (again, just having a brain freeze).  Thanks as always!

Your solution to the original question

Please only use this to answer the original question. Otherwise please use comments.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:

To avoid this verification in future, please log in or register.