Welcome, Guest
Username: Password: Remember me

TOPIC: Checking answers to two different questions before proceeding

Checking answers to two different questions before proceeding 3 years 1 month ago #58076

  • paulfiner
  • paulfiner's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
Hi

I have a group with a long free text question and a multiple option with just one checkbox.
The user should check this button if the text question above is not relevant.

I'm trying to stop the user from going to the next page if they didn't enter any text AND they didn't check the checkbox.

Here's my javascript which I patched together from various other solutions suggested by TPartner.
My variable i always seems to return 0 even if I enter text in the field.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 		var qOpen = 3329;
		var cb = 3487;
		var i=0;
 
	$('#question3329 input.text').change(function() {
		if ($(this).val() != '') {
			i=1;} 
		else {
			i=0;}
 
	});
 
	// Interrupt next/submit
	$('form#limesurvey').submit(function(){
 
		if ($('#question'+cb+' input.checkbox:eq(0)').attr('checked') == false && i == 0) {
				alert("Please enter either a comment or tick the checkbox");
				return false;
			} else {
 
			return true;
		}
	});
 });
</script>

Can anyone help?

Cheers

Paul
The administrator has disabled public write access.

Re: Checking answers to two different questions before proceeding 3 years 1 month ago #58093

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
Long-text questions use textareas instead of inputs. Try this:

Change
$('#question3329 input.text').change(function() {

To
$('#question3329 textarea').change(function() {
Cheers,
Tony Partner

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

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 3 years 1 month ago by tpartner.
The administrator has disabled public write access.

Re: Checking answers to two different questions before proceeding 3 years 1 month ago #58095

  • paulfiner
  • paulfiner's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
Hi Tony

I knew there had to be a simple reason why it wasn't working! I must have spent about 2 hours this morning trying to get this to work.
I think my method is probably a rather long-winded way of doing this and I originally tried to use the 'length' function instead. Probably would have worked if I had had textarea!

Thanks for the heads up...I'll remember for next time!

Cheers

Paul
The administrator has disabled public write access.

Re: Checking answers to two different questions before proceeding 3 years 1 month ago #58097

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
Hehe - it's the little things that take the most time.

I think you could shorten it a bit by dropping the onchange function and doing it all in the submit function:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 		var qOpen = 3329;
		var cb = 3487;
 
		// Interrupt next/submit
		$('form#limesurvey').submit(function(){
 
			if ($('#question'+cb+' input.checkbox:eq(0)').attr('checked') == false && $('#question'+qOpen+' textarea').val() == '') {
				alert("Please enter either a comment or tick the checkbox");
				return false;
			} 
			else {
				return true;
			}
		});
	});
</script>
Cheers,
Tony Partner

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

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.120 seconds
Donation Image