Have an idea?

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

custom error message for grid having checkboxes

I'm new to Javascript, i wanted to customize a error message for a grid questions containing 2 columns and 36 rows.

If nothing is selected; i wish to show the error "Please select at least one option from each column."

im using this script; but it isnt working.

var showError = 0;
if(SSI_GetValue("Q1_r1_c1")==0 && SSI_GetValue("Q1_r2_c1")==0) && SSI_GetValue("Q1_r3_c1")==0) && SSI_GetValue("Q1_r4_c1")==0) && SSI_GetValue("Q1_r5_c1")==0) && SSI_GetValue("Q1_r6_c1")==0) && SSI_GetValue("Q1_r7_c1")==0) && SSI_GetValue("Q1_r8_c1")==0) && SSI_GetValue("Q1_r9_c1")==0) && SSI_GetValue("Q1_r10_c1")==0) && SSI_GetValue("Q1_r11_c1")==0) && SSI_GetValue("Q1_r12_c1")==0) && SSI_GetValue("Q1_r13_c1")==0) && SSI_GetValue("Q1_r14_c1")==0) && SSI_GetValue("Q1_r15_c1")==0) && SSI_GetValue("Q1_r16_c1")==0) && SSI_GetValue("Q1_r17_c1")==0) && SSI_GetValue("Q1_r18_c1")==0) && SSI_GetValue("Q1_r19_c1")==0) && SSI_GetValue("Q1_r20_c1")==0) && SSI_GetValue("Q1_r21_c1")==0) && SSI_GetValue("Q1_r22_c1")==0) && SSI_GetValue("Q1_r23_c1")==0) && SSI_GetValue("Q1_r24_c1")==0) && SSI_GetValue("Q1_r25_c1")==0) && SSI_GetValue("Q1_r26_c1")==0) && SSI_GetValue("Q1_r27_c1")==0) && SSI_GetValue("Q1_r28_c1")==0) && SSI_GetValue("Q1_r29_c1")==0) && SSI_GetValue("Q1_r30_c1")==0) && SSI_GetValue("Q1_r31_c1")==0) && SSI_GetValue("Q1_r32_c1")==0) && SSI_GetValue("Q1_r33_c1")==0) && SSI_GetValue("Q1_r34_c1")==0) && SSI_GetValue("Q1_r35_c1")==0) && SSI_GetValue("Q1_r36_c1")==0) {
    showError = 1;
}
if(showError==1) {
    strErrorMessage = "Please select at least one option from each column.";
}
asked Mar 10 by Karuna Tandon (180 points)

1 Answer

0 votes
Your script has more close parentheses than open parentheses.  This probably came from copy-and-pasting the close paren in producing this script.  Fixing this will get your script working.

This sort of mistake is easy to make and can be tricky to find when repeating nearly-identical code like this.  I would recommend you look into looping in JavaScript, which can help you avoid this.  Check out this version of your custom verification where I use a for loop to go from row 1 to row 36:

var showError = 1;
for (var i = 1; i <= 36; i++) {
    if (SSI_GetValue('Q1_r' + i + '_c1') == 1) {
        showError = 0;
    }
}
if (showError == 1) {
    strErrorMessage = 'Please select at least one option from each column.';
}


This version is more technical, but the solution I think you'll come to if you keep up your JavaScript learning.  It even handles multiple columns for you.

$('input[name="hid_col_list_[% QuestionName() %]"]').val().split(',').forEach(function(column){
    var isChecked = false;
    $('input[name="hid_row_list_[% QuestionName() %]"]').val().split(',').forEach(function(row){
        if (SSI_GetValue('[% QuestionName() %]_r' + row + '_c' + column)) {
            isChecked = true;
        }
    });
    if (!isChecked) {
        strErrorMessage = 'Please select at least one option from each column.';
    }
});
answered Mar 10 by Zachary Platinum Sawtooth Software, Inc. (171,950 points)
...