Thursday, March 17, 2016

Custom validation in SurveyGizmo

SurveyGizmo allows you to add JavaScript directly to a survey page with the JavaScript Action. They use jQuery for survey interaction so that's already loaded on the page and available. Using this feature it's easy to customize a survey page.

One of the possible uses is for custom validation. For example to validate user input in some way that SurveyGizmo does not support out of the box. To do this you could start with the JavaScript below. It hooks the Next button click event and then hides or shows the error message based on a custom check. Note, we use the Next button click event and not the page submit event because that is called even when the Back button is selected.


Wednesday, March 16, 2016

Working with Arrays in SurveyGizmo Custom Scripting

As mentioned in my previous posts (here) we do a lot of work with SurveyGizmo's custom scripting feature. It's kind of like PHP and kind of like JavaScript but different enough that I forget how to do basic things like create arrays. So thought I would post some examples of working with arrays.

Some examples of working with single dimension arrays:

// Declare array with values
%%locations = array('Jacksonville, FL',
        'Toronto, Canada',
        'Orlando, FL',
        'Chicago, IL');

// Declare new empty array
%%copied = array();

foreach(%%locations as %%location) {
        // Add element to array
        %%copied[] = %%location;
}

And with a for loop (NOTE: You must use curly brackets when using a variable as the key).
for (%%i=0; %%i<%%count; %%i++) {
 %%output .= "<br>".%%locations{%%i};
}

And with a 2-dimension array (or hashtable):

// Declare array with values
%%locations = array('Toronto' => 'Ontario', 'Jacksonville' => 'FL',
        'Orlando' => 'FL', 'Chicago' => 'IL');

foreach (%%locations as %%key => %%value) {
        %%output .= "City: ".%%key." State/Province".%%value;
}