using javascript to validate text entry responses | Experience Community
Skip to main content
Solved

using javascript to validate text entry responses

  • Jun 14, 2023 5:40 PM UTC
  • 10 replies
  • 1022 views

Forum|alt.badge.img+2

I tried using the following javascript, to only allow certain responses to a text entry question. However, it is not working. Any suggestions?

 

Thanks a lot in advance.

 

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
// Target the text entry question by its QuestionID
var questionId = 'QID1';

// Specify the pre-specified options
var validOptions = ['Option 1', 'Option 2', 'Option 3'];

// Get the entered value from the text entry question
var enteredValue = this.getChoiceValue(questionId);

// Check if the entered value is not one of the valid options
if (!validOptions.includes(enteredValue)) {
this.setChoiceError(questionId, 'Please enter a valid option.');
this.disableNextButton(); // Disable the next button
} else {
this.clearChoiceError(questionId);
this.enableNextButton(); // Enable the next button
}
});

 

Best answer by TomG

Two more options:

  1. Use an empty select and load the options from a JS object using select2.
  2. If you hit a limitation, load them from a server using select2 ajax option.
View original

10 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Jun 14, 2023 6:11 PM UTC

You should use Custom Validation instead of JavaScript.  Without going into details on your JS, you’ve misunderstood how a number of things work.


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • Jun 14, 2023 7:05 PM UTC

Thanks Tom. However, I have hundreds of text options to put in, instead of 3 in the attempted code. Could you provide some hints to correcting the code?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Jun 14, 2023 7:18 PM UTC

In that case, I recommend using a multiple choice question with a dropdown instead of a text entry question. You could use select2 to make it searchable.

Another option might be Supplemental Data if you have that feature on your Qualtrics account.


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • Jun 14, 2023 8:53 PM UTC

Thanks Tom. I will give select2 a try and report back.


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • Jun 17, 2023 3:23 PM UTC

Dropdown + select2 worked nicely!


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • Jun 17, 2023 4:12 PM UTC

I concluded too early. I ran into a problem with the 20,000 character limit when I input all the options. Any other idea to solve this?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Jun 17, 2023 5:08 PM UTC

You can use a text entry question. Add you own select in the question text html. Use JS to hide the text input, add select2, and copy the select value to the text entry on update or submit.


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • Jun 17, 2023 6:27 PM UTC

The idea is brilliant, but I hit the 20,000 character limit again when I added my select in html.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Answer
  • Jun 17, 2023 6:44 PM UTC

Two more options:

  1. Use an empty select and load the options from a JS object using select2.
  2. If you hit a limitation, load them from a server using select2 ajax option.

Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • Jun 17, 2023 7:45 PM UTC

Empty select worked. Grateful for your advice, Tom!

P.S. In case it is useful for future visitors to this page -- if there is ‘ in one of the options, need to escape it to make the code work. This took me the longest time to figure out...