Validate text answer against a list of words

More
5 years 8 months ago #122740 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.
LimeSurvey Partners
More
5 years 8 months ago #122743 by DenisChenu

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development . I don't answer to private message.
The following user(s) said Thank You: airraidsiren1974
The topic has been locked.
More
5 years 8 months ago #122746 by tpartner
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.

<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:

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.
Official LimeSurvey Partner - partnersurveys.com
The topic has been locked.
More
5 years 8 months ago #122755 by airraidsiren1974
Thank you both for the quick reply! B) :laugh:
The topic has been locked.
More
5 years 8 months ago #122838 by airraidsiren1974
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.
More
5 years 8 months ago #122839 by tpartner
Try this:

<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.
Official LimeSurvey Partner - partnersurveys.com
The following user(s) said Thank You: airraidsiren1974
The topic has been locked.
More
5 years 8 months ago #122863 by airraidsiren1974
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.
More
5 years 8 months ago #122875 by tpartner
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.
Official LimeSurvey Partner - partnersurveys.com
The topic has been locked.
More
5 years 8 months ago - 5 years 8 months ago #122901 by airraidsiren1974
This is the exact code for that particular question:
<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: 5 years 8 months ago by airraidsiren1974. Reason: adding the code tags
The topic has been locked.
More
5 years 8 months ago #122902 by tpartner
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.
Official LimeSurvey Partner - partnersurveys.com
The topic has been locked.
More
5 years 8 months ago #122903 by airraidsiren1974
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.
More
5 years 8 months ago #122905 by tpartner
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.
Official LimeSurvey Partner - partnersurveys.com
The topic has been locked.
More
5 years 8 months ago #122908 by airraidsiren1974
No errors on the console
Attachments:
The topic has been locked.
More
5 years 8 months ago #122909 by airraidsiren1974
OK. The plot thickens. I tried it on my MacBookPro (running Yosemite) using Safari and it worked :blink:
The topic has been locked.
More
5 years 8 months ago #123176 by airraidsiren1974
What would make the same exact code work in Safari and IE, but not in Firefox or Chrome?

Can someone tried to get this plugin to work?

jqueryvalidation.org/

Thanks in advance :). Will talk to my boss about making a donation on behalf of the company.
The topic has been locked.

Start now!

Just create your account and start using Limesurvey today.

Register now