Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Validate text answer against a list of words

  • airraidsiren1974
  • airraidsiren1974's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 4 months ago #122740 by airraidsiren1974
Validate text answer against a list of words was created by airraidsiren1974
Hi guys,

I have a kill list of words that I'm trying to use to validate a particular short text answer. The question asks the respondent to enter the company they work for. If they use words like: "refused", "none of your business", "nunya"... etc, I would like a warning to appear.

I searched the forum and googled for a number of days. I found some similar problems, but nothing helped. Then I found a JQuery Validator plugin, but I can't figure out how to implement it on to my survey. HELP!

I found this one being very promising:

stackoverflow.com/questions/5696222/jque...t-of-accepted-values

Can someone help me edit the JQuery code? Is this doable in Limesurvey?

Thanks in advance.
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team & Official Partner
  • LimeSurvey Community Team & Official Partner
More
9 years 3 months ago #122743 by DenisChenu
Replied by DenisChenu on topic Validate text answer against a list of words

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member. - Professional support - Plugins, theme and development .
I don't answer to private message.
The following user(s) said Thank You: airraidsiren1974
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 3 months ago #122746 by tpartner
Replied by tpartner on topic Validate text answer against a list of words
This could probably be rolled into a function or, better, a jQuery plugin but no time today...

Assuming the question is a short-text, add something like this to the question source. It will interrupt the Next/Submit function and search the text input for your disallowed strings. If found, an alert is popped up, a class is applied to the input and the Next/Submit function is aborted.

Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function() {  
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Define the disallowed strings and allert text
    var disallowed = ['refused', 'none of your business', 'nunya'];
    var alertText = 'You have entered an invalid string!';
 
    // Interrupt the Next/Submit click
    $('#movenextbtn, #movesubmitbtn').bind('click', function () {
      var thisInput = $('input.text', thisQuestion);
      var thisVal = thisInput.val();
      thisInput.removeClass('disallowed-string');
 
      // Loop through all disallowed strings
      $(disallowed).each(function(i) {
        // If disallowed string found in input value
        if(new RegExp('\\b'+this+'\\b', 'i').test(thisVal) == true) {
          thisInput.addClass('disallowed-string'); // Add a class
          alert(alertText); // Pop up alert
          return false; // Exit the loop
        }
      });
 
      // Abort the Next/Submit if we found a disallowed string
      if(thisInput.hasClass('disallowed-string')) {
        return false;
      }
    });
 
  });
</script>

You can add something like this to the end of template.css for the disallowed class:

Code:
input.disallowed-string {
  background: pink;
  border: 1px solid red;
}

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • airraidsiren1974
  • airraidsiren1974's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 3 months ago #122755 by airraidsiren1974
Replied by airraidsiren1974 on topic Validate text answer against a list of words
Thank you both for the quick reply! B) :laugh:
The topic has been locked.
  • airraidsiren1974
  • airraidsiren1974's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 3 months ago #122838 by airraidsiren1974
Replied by airraidsiren1974 on topic Validate text answer against a list of words
So I added Tony's code and it worked great. Then I tried to add a validation equation to require respondents to put more than 4 characters and Tony's code stopped working. Can I incorporate the 4 character validation to the code Tony provided? Thanks in advance for the help.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 3 months ago #122839 by tpartner
Replied by tpartner on topic Validate text answer against a list of words
Try this:

Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function() {  
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Define the disallowed strings and allert text
    var disallowed = ['refused', 'none of your business', 'nunya'];
    var alertText = 'You have entered an invalid string!';
    var alertText2 = 'You must enter more than 4 characters!';
 
    // Interrupt the Next/Submit click
    $('#movenextbtn, #movesubmitbtn').bind('click', function () {
      var thisInput = $('input.text', thisQuestion);
      var thisVal = thisInput.val();
      thisInput.removeClass('disallowed-string');
 
      if($.trim(thisVal).length < 5) {
        thisInput.addClass('disallowed-string'); // Add a class
        alert(alertText2); // Pop up alert
      }
      else { 
        // Loop through all disallowed strings
        $(disallowed).each(function(i) {
          // If disallowed string found in input value
          if(new RegExp('\\b'+this+'\\b', 'i').test(thisVal) == true) {
            thisInput.addClass('disallowed-string'); // Add a class
            alert(alertText); // Pop up alert
            return false; // Exit the loop
          }
        });
      }
 
      // Abort the Next/Submit if we found a disallowed string
      if(thisInput.hasClass('disallowed-string')) {
        return false;
      }
    });
 
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: airraidsiren1974
The topic has been locked.
  • airraidsiren1974
  • airraidsiren1974's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 3 months ago #122863 by airraidsiren1974
Replied by airraidsiren1974 on topic Validate text answer against a list of words
Hi Tony,

Sorry to be such a pest. I can't seem to get this to work consistently. It started working... I added more words to the disallowed strings, then it stopped working. When it stopped working I went back to your original code, with the 3 disallowed strings, and it still didn't work.

Could it be my Limesurvey install?

Let me know if you'd like to see the survey in question. Thanks in advance.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 3 months ago #122875 by tpartner
Replied by tpartner on topic Validate text answer against a list of words
Can you please post the exact code you have now?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • airraidsiren1974
  • airraidsiren1974's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 3 months ago - 9 years 3 months ago #122901 by airraidsiren1974
Replied by airraidsiren1974 on topic Validate text answer against a list of words
This is the exact code for that particular question:
Code:
<style type="text/css">
body {
background-color: #434343;
background-image: url('/upload/surveys/362842/images/buildingbkgd.png');
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: right; 
 
}</style>
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function() {  
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Define the disallowed strings and alert text
    var disallowed = ['refused', 'none of your business', 'nunya'];
    var alertText = 'Please enter the name of the company you work for';
 
    // Interrupt the Next/Submit click
    $('#movenextbtn, #movesubmitbtn').bind('click', function() {
      var thisInput = $('input.text', thisQuestion);
      var thisVal = thisInput.val();
      thisInput.removeClass('disallowed-string');
 
      // Loop through all disallowed strings
      $(disallowed).each(function(i) {
        // If disallowed string found in input value
        if(new RegExp('\\b'+this+'\\b', 'i').test(thisVal) === true) {
          thisInput.addClass('disallowed-string'); // Add a class
          alert(alertText); // Pop up alert
          return false; // Exit the loop
        }
      });
 
      // Abort the Next/Submit if we found a disallowed string
      if(thisInput.hasClass('disallowed-string')) {
        return false;
      }
    });
 
  });
</script>What is the name of the company where you currently work?
Last edit: 9 years 3 months ago by airraidsiren1974. Reason: adding the code tags
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 3 months ago #122902 by tpartner
Replied by tpartner on topic Validate text answer against a list of words
Your code works fine for me in Firefox, Chrome and IE 7-11. Clear your cache, restart the browser and test again.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • airraidsiren1974
  • airraidsiren1974's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 3 months ago #122903 by airraidsiren1974
Replied by airraidsiren1974 on topic Validate text answer against a list of words
I cleared cache (on Chrome... including all browsing history, passwords, downloads). Restarted the browser... same issue. I'll try it out of the work connection tonight when I get home. Could it possibly be my work network?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 3 months ago #122905 by tpartner
Replied by tpartner on topic Validate text answer against a list of words
Unlikely, do you have any errors in the console? (F12 --> Console)

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose