Online Survey Tutorials

SurveyGizmo Tutorials and Help Documentation

Tutorial: Scripting Sample: Verifying Two Text Fields Match

This series is dedicated to showing advanced functionality within SurveyGizmo using the Custom Scripting action (Pro plan or higher). While the tutorials are meant to be portable and easy to insert into your own survey, it will be necessary to understand advanced concepts such as Question IDs. These scripts are provided as-is without additional support for alterations.

This script uses the following question types or actions:

  • Text Field Question or Hidden Value Field – (2)
  • Descriptive Text Field

Description

This script compares the values entered into two separate text field questions. If the values are not an exact match, it returns them to the page containing the text fields and shows a customized descriptive text error message to notify the respondent that they do not match.

The Script

%%field1 = 3; //Question ID of first textbox
%%field2 = 4; //Question ID of second textbox
%%pageid = 1; //Page ID of page containing field1 and field2
%%errormsg_qid = 7; // Question ID for the Descriptive Text containing the error message
 
/**** DO NOT ALTER BELOW THIS POINT ****/
%%vala = sgapiGetValue(%%field1);
%%valb = sgapiGetValue(%%field2);
 
if (%%vala != %%valb){
  sgapiHideQuestion(%%errormsg_qid,false);
  sgapiJumpToPage(%%pageid);
}else{
  sgapiHideQuestion(%%errormsg_qid,true);
}

The Steps

  • Choose Add Descriptive Text and include an error message stating that the fields must match
  • Advanced Edit the Descriptive Text field. Under Advanced Formatting, set the Template Hook Name as sg_ErrorMessage. Under Advanced Logic & Piping set Disable Question to Yes
  • Create the Text Field questions on the same page in the survey and just below the Descriptive Text
  • On the page following, Add Action > Custom Scripting Control and paste in the above code
  • Alter the script for the proper question IDs
  • Save all changes and test in preview mode

A sample survey to demonstrate the script above is available! Verify Text Field Match

Warnings

Matching must be exact. Therefore, ‘the’ and ‘The’ would NOT match!