This month's Badge of the Month 💬 discussion is about your favorite teachers! Tell us about your favorite teacher to earn your badge now | EARN BADGE >

Custom validation to avoid certain word in free text field.

Subhransu
Subhransu India
edited August 2021 in Custom Code

Hello,

Can someone please help.

I have few hundreds of words to be avoided being entered in the free text field while user answering the survey.

Only option I see is to add one condition for each word.

Can someone help with the java script code if its possible.

Best Answers

  • cbhavsar
    cbhavsar Mumbai, India Level 2 ●●
    Accepted Answer

    Hi,

    Here is the script you are looking for. Try modifying it based on your requirement.

    var substrings = ["Monday", "Tuesday", "Wednesday"];
    var str;
    str = "Hey, today is Monday!";
    if (new RegExp(substrings.join("|")).test(str)) {
        console.log("Text have a word from array!");
    } else {
        console.log("Text don't have any word from array!");
    }
    

    All the very best for your project!

  • cbhavsar
    cbhavsar Mumbai, India Level 2 ●●
    Accepted Answer

    Using Jquery or Javascript you can fetch the text and store it in str variable of my script earlier. I suggest you should execute this script before page unload.

    In case you are looking where you can place JS, then click on the question - the last option in the left pane would be Javascript and there you should add script under Qualtrics.SurveyEngine.addOnUnload

Answers