Six common scripts that can add flexibility and functionality to your surveys

InstantSurvey allows you to add advanced scripting capabilities to your survey response and logic capabilities. To input scripts within InstantSurvey, you simply need to select Edit Script from the More Actions drop-down menu while you are on the Edit Survey page. From there, you can add any of the following six script options:

1) Restrict open-text responses to zip code format

  • Click Add script next to the open-text response where you want to only allow zip code responses.
  • Make sure onValidate is selected from the drop-down menu.
  • Copy and paste the script below into the open-text box that appears.

if (thiselement.value.length!=5){ alert('Please enter a 5-digit zip code'); thiselement.focus(); Result='stop'; }

2) Require legitimate email format responses only

  • Click Add script next to the open-text response where you want to require respondents to enter an email address.
  • Make sure onValidate is selected from the drop-down menu.
  • Copy and paste the script below into the open-text box that appears.

var str = thiselement.value;
var regex = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
if (!regex.test(str)) {
alert('Please enter a valid email address');
thiselement.focus();
thiselement.select();
Result='stop';
}

3) Require a password or user ID before allowing respondents to continue

  • Click Add script next to the open-text response where you want respondents to enter the appropriate password or ID.
  • Make sure onValidate is selected from the drop-down menu.
  • Copy and paste the script below into the open-text box that appears (the accepted ID values for this example are test, 12345, and 54321. To add your passwords or IDs, replace those values in the text below).

if (thiselement.value== "test" ||
thiselement.value== 12345 ||
thiselement.value== 54321){
Result='go';
}else{
alert('Invalid ID');
thiselement.focus();
thiselement.select();
Result='stop';
}

4) Limit respondent answers to numeric values only

  • Find your open-text question, and click Add script.
  • A drop-down menu will appear along with an open-text box. Choose Custom from the drop-down menu, and then type in onKeyPress in the small box that appears to the right.
  • Then, in the large text box below, paste in the following script.

if (event.keyCode < 48 || event.keyCode > 57) {event.returnValue = false; alert('Please enter numbers only.');}

5) Limit respondent to select up to three checkboxes

  • Click Add script next to the question in which you would like to limit the respondents to three response selections (this question must be a checkbox format response).
  • Make sure onValidate is selected from the drop-down menu.
  • Copy and paste the script below into the open-text box that appears.

selectedcount=0;
for (var i=0;i<thiselement.length;i++) {
if (thiselement[i].checked) selectedcount++;
}
if (selectedcount>3) {
alert('Please select 3 or less options');
Result='stop';
}

  • If you change the two locations where the number 3 is in the script above to a new number, you can allow up to that many response selections for your respondents.

6) Assign random values to your respondents

  • To set up this option, you must first add a Hidden Response and set it as Numeric/single. Within that Hidden Response, you also need to supply the possible random values that you would like respondents to receive (such as 1, 2, 3, and 4 depending on how many possibilities you would like for the randomization).
  • Once the Hidden Response is added, you then enter Script Editing and add a script to that Hidden Response.
  • Set the script to onInit, and copy the script below.

var randnumber = Math.ceil(Math.random() * 4);
thiselement.value = randnumber;
//alert(thiselement.value);

  • The value 4 above once again corresponds to the amount of random values you want possibly assigned based on the paths you have set up for randomization.
  • You can then use those random values to direct respondents down random paths within your survey.

 




Client Login | Terms of Use | FAQs | Privacy Policy | Support | Site Map
© 2017 InstantSurvey. All rights reserved.