Check out the LimeSurvey source code on GitHub!

Auto-suggest text field

More
3 years 8 months ago #97270 by tpartner
Yeah, but when would you want this alert to pop up? Every time the respondent types something or every time the list appears?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
3 years 8 months ago - 3 years 8 months ago #97272 by arielmosto
Tony:

Alert pop up should appear when the respondent writes an option out of the list.

The list contains the 200 largest firms in an industry (complex and lengthy names with many words)*. Not much chance of entering new, but the possibility of ranking must be free, it should be possible to choose one from the list or a new company.

Thanks for your time and all your ideas and experience are welcome. Ariel

*The names on the list are very similar to each other, such as:
  • Red Hot Chili Peppers Inc.
  • Blue Hot Chili Peppers Corp.
  • Red cold Chili brothers
Last Edit: 3 years 8 months ago by arielmosto. Reason: clarification

Please Log in to join the conversation.

More
3 years 8 months ago #97282 by tpartner
Try this code in the question source:
<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
 
		// Identify the question
		var q1ID = '{QID}';
		var q1 = $('#question'+q1ID+'');
 
		var url = 'http://localhost/limesurvey2/upload/templates/test_csv_2/countries.csv';
		//var url = '{TEMPLATEURL}companies.csv';
		var countries = new Array();
 
		// Grab the CSV contents
		$.get(url, function(data) {
 
			// Convert CSV contents to an array of arrays
			tmp = $.csv.toArrays(data);
 
			// Load the countries array
			$(tmp).each(function(i, item) {
				countries.push(item[0]);
			});
 
			var msg = 'You cannot have duplicate entries.';
			var msg2 = 'If your option is in the list, please use the mouse to select it. If not, write it.';
 
			$('input.text', q1).autocomplete({
				source: countries,
				minLength: 0,
				change: function(event, ui) {
					var thisInput = $(this);
					var duplicateError = false;
					if($(thisInput).val() != '') {
						// Check for duplicates
						$('input.text', q1).not($(thisInput)).each(function(){
							if($(this).val().toLowerCase() == $(thisInput).val().toLowerCase()) {
								alert (msg);
								duplicateError = true;
								$(thisInput).val('').focus();
								$('input.text', q1).autocomplete('close');
								return false;
							}
						});
						if(duplicateError == true) {
							return;
						}
						// Check if entered value is in the list
						var inList = false;
						$(countries).each(function(){
							if(this.toLowerCase() == $(thisInput).val().toLowerCase()) {
								inList = true;
							}
						});
						if(inList == false) {
							alert (msg2);
						}
					}
				},
				select: function(event, ui) {
					// Check for duplicates
					var thisInput = $(this);
					var okay = 1;
					$('input.text', q1).not($(thisInput)).each(function(){
						if($(this).val().toLowerCase() == ui.item.value.toLowerCase()) {
							okay = 0;
						}
					});
					if(okay == 0) {
						alert (msg);
						return false;
					}
				} 			
			});	
		});	
	});
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
3 years 8 months ago #97290 by arielmosto
Thanks Tony.

The msg2 not displayed when you write an option out of the list.

Below this
if(inList == false) {
							alert (msg2);
I add
return false;

And it works, when changing cell with the mouse, but not work with the keyboard, with "Tab"

Perhaps "return false" is not sufficiently good to work properly in this case.

Thanks ! Ariel

Please Log in to join the conversation.

More
3 years 8 months ago #97294 by arielmosto
Tony;

I just checked again and the code works correctly.

"msg2" is displayed by clicking with the mouse but not with Tab.

Is it possible to do that with Tab works well?

Regards ! Ariel

Please Log in to join the conversation.

More
3 years 8 months ago #97319 by arielmosto
Tony:

We are planning to leave the possibility of advancing with the keyboard button "Tab" for those not listed.

On the other hand, we tried the script in the question type "Array (Texts)" and fail to make it work.

You can make it work for "Array (texts)"?

Here its the image from question Array:


Attachment Pantallazo-1.png not found



Thanks !
Attachments:

Please Log in to join the conversation.

More
3 years 7 months ago #97338 by tpartner
<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
 
		// Identify the question
		var q1ID = '{QID}';
		var q1 = $('#question'+q1ID+'');
 
		var url = 'http://localhost/limesurvey2/upload/templates/test_csv_2/countries.csv';
		//var url = '{TEMPLATEURL}companies.csv';
		var countries = new Array();
 
		// Grab the CSV contents
		$.get(url, function(data) {
 
			// Convert CSV contents to an array of arrays
			tmp = $.csv.toArrays(data);
 
			// Load the countries array
			$(tmp).each(function(i, item) {
				countries.push(item[0]);
			});
 
			var msg = 'You cannot have duplicate entries.';
			var msg2 = 'If your option is in the list, please use the mouse to select it. If not, write it.';
 
			$('input[type="text"]', q1).autocomplete({
				source: countries,
				minLength: 0,
				change: function(event, ui) {
					var thisInput = $(this);
					var duplicateError = false;
					if($(thisInput).val() != '') {
						// Check for duplicates
						$('input[type="text"]', q1).not($(thisInput)).each(function(){
							if($(this).val().toLowerCase() == $(thisInput).val().toLowerCase()) {
								alert (msg);
								duplicateError = true;
								$(thisInput).val('').focus();
								$('input[type="text"]', q1).autocomplete('close');
								return false;
							}
						});
						if(duplicateError == true) {
							return;
						}
						// Check if entered value is in the list
						var inList = false;
						$(countries).each(function(){
							if(this.toLowerCase() == $(thisInput).val().toLowerCase()) {
								inList = true;
							}
						});
						if(inList == false) {
							alert (msg2);
						}
					}
				},
				select: function(event, ui) {
					// Check for duplicates
					var thisInput = $(this);
					var okay = 1;
					$('input[type="text"]', q1).not($(thisInput)).each(function(){
						if($(this).val().toLowerCase() == ui.item.value.toLowerCase()) {
							okay = 0;
						}
					});
					if(okay == 0) {
						alert (msg);
						return false;
					}
				} 			
			});	
		});	
	});
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
3 years 7 months ago #97347 by arielmosto
Dear Tony, thank you very much.

It works but it was a small detail pending; duplicate validation should work in this type of question, per row (first, second and third ranking)

No per column, it does not allow ranking, the same company in two different sectors.

Regards! Ariel

Please Log in to join the conversation.

More
3 years 7 months ago #97348 by tpartner
Sorry, I've already spent WAY too much time on this post. Perhaps someone els can help you.

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

Please Log in to join the conversation.

More
3 years 7 months ago #97349 by arielmosto
Dear Tony, I understand perfectly.

Thank you very much for your time, we have made a new donation to support the project.

Regards Ariel

Please Log in to join the conversation.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form