Custom validation, any combination of n strings | XM Community
Skip to main content
Hi there,



I am new to the platform and I apologize if the question is easy or a repeat. I was hoping I can get help with my custom validation quest. What I would like to do is make sure that a text entry response is within any combination of couple of strings. I tried the 'Match regex' option but was unable to do what I want. I tried adding javascript code but got lost with using the API. I hope someone can provide some guidance. Thanks in advance!
> @Clare said:

>What I would like to do is make sure that a text entry response is within any combination of couple of strings.



Can you more specific than your statement above?
It would also help if you mentioned your familiarity with JavaScript and Regex.
Hi TomG,



Thanks for your response. I intend to give participants couple of worlds with which they would have to build a sentence/response. I would like to accept as a valid response any combination of these words, as long as only these words are used to compose a response, and all of them are spelled correctly. Ideally, I would like to make sure no numbers, punctuation or any other symbol is included in the response. For instance, if I provide participants with the words:



I

love

hate

swimming

is

what

better

...



A valid response would be "I like swimming better", "what I like is swimming" etc.



I hope this clarifies it a bit.



Here is an update on my progress: Using the javascript add-on functionality, I was able to capture the response and figure out whether it is a combination of the words provided. I am unable to print out a Qualtrics error message though. In addition, qualtrics still submits the response as valid and continues on to the next question. I would like to give people the option of re-typing a response. I would use some help on dealing with this part.



Thanks in advance!
Hi Mattyb513,



I am don't have prior experience with JavaScript but have worked with other programming languages. It is important to mention that my skills in web development are non existent. I have some experience with Regex, primarily with java and python. I hope that helps.
@Clare,



First, you want to start your JavaScript by disabling and/or hiding the Next button. Then, when you know you have a valid response enable and/or show the Next button.



For a validation error message, I generally just update the html of the element Qualtrics uses for its validation errors (class of ValidationError).



You probably want to use a 'blur' handler on the input field to check the input then take appropriate action...show the validation error or enable/show the Next button.
Tom is right on the money.



Here's a working example I spun up real quick: https://qualtricssfi.az1.qualtrics.com/jfe/preview/SV_80RXr3Vax0mgHkh?Q_SurveyVersionID=current&Q_CHL=preview



Attached is the qsf. I've also made a gist that removes all the extra JavaScript made for pulling the answers from the previous page: https://gist.github.com/mattbloomfield/4fd526d58cd336210a0b623f7017ebb0
Hi Mattyb513,



Thank you so much for your help! You saved me so much time and effort!

Leave a Reply