Mandatory rows in array

More
5 years 4 months ago #81556 by paulfiner
paulfiner created the topic: Mandatory rows in array
I have an array question using radio buttons and I'd like to make all rows mandatory except the last one.
I found this script in the workarounds section but I think this is for an array of text fields. I tried adapting it for radio buttons but had no luck. Can anyone help?
<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
 
  // Interrupt the submit function
  $('form#limesurvey').submit(function () {
    // Override the built-in "disable navigation buttons" feature
    $('#moveprevbtn, #movenextbtn, #movesubmitbtn').attr('disabled', '');
 
    var empty = 0;
 
    var warningText = 'Please complete the highlighted inputs.';
 
    // Call the mandatory row function with question IDs and row numbers
    mandatoryRow(QQ, 1);
    mandatoryRow(QQ, 3);
 
    // A function to render rows of an array mandatory
 
 
    function mandatoryRow(qID, rowNum) {
 
      $('div#question' + qID + ' table.question tbody[id^="javatbd"]:eq(' + Number(rowNum - 1) + ') input[type="text"]').each(function (i) {
        if ($(this).val() == '') {
          $(this).css('background-color', 'pink');
          empty = 1;
        } else {
          $(this).css('background-color', '#FFFFFF');
        }
      });
    }
 
    if (empty == 1) {
      alert(warningText);
      return false;
    } else {
      return true;
    }
  });
});
</script>

Please Log in to join the conversation.

More
5 years 4 months ago - 5 years 4 months ago #81569 by tpartner
tpartner replied the topic: Mandatory rows in array
Try this. Replace "QQ" with the question ID.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function () {
 
		// Call the mandatory row function with question ID
		mandatoryRows(QQ);
 
		// A function to render all but the last rows of an array mandatory
		function mandatoryRows(qID) {
 
			var warningText = 'Please complete the highlighted rows.';
 
			// Add a mandatory class to all rows except the last one
			$('div#question'+qID+' table.question tbody[id^="javatbd"]:not(:last) tr').addClass('mandatory');
 
			// Interrupt the submit function
			$('#movenextbtn, #movesubmitbtn').click(function(){
				var fail = '';
 
				// Loop through all mandatory rows and check for answers
				$('tr.mandatory').each(function(i){
					if($('input.radio:checked', this).length < 1) {
						 $('th, td', this).css({ 'background':'pink' });
						 fail = 1;
					}
					else {
						 $('th, td', this).css({ 'background':'' });
					}
				});
 
				if(fail == 1) {
					alert (warningText);
					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.
Last Edit: 5 years 4 months ago by tpartner.

Please Log in to join the conversation.

More
5 years 4 months ago #81572 by paulfiner
paulfiner replied the topic: Mandatory rows in array
Hi Tony

Hope you are well!

Thanks for the script, which works fine. I had actually just muddled something together which seemed to work but wasn't as neat as your solution.
I have one small thing to add in that there is a short textfield just after my array question and I need to check if somebody selected an answer on the last row of my array but didn't fill in the textbox question I need to warn them.

I think I have most of the script setup to do this but how do I assign the value in the textbox to a variable?
I can't use INSERTANS because it's before the submit button is pressed.

Cheers

Paul

Please Log in to join the conversation.

More
5 years 4 months ago #81574 by tpartner
tpartner replied the topic: Mandatory rows in array
If it's a single-short-text (where QQ is the question ID):
var textInput = $('#questionQQ input.text').val();

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
5 years 4 months ago #81575 by paulfiner
paulfiner replied the topic: Mandatory rows in array
Great, thanks Tony....I knew it was something simple!

Please Log in to join the conversation.

More
5 years 4 months ago #81583 by Bourbon
Bourbon replied the topic: Mandatory rows in array
How do you get them all to be mandatory?
I can't get the script in workaround section to work, but this did.

Please Log in to join the conversation.

More
5 years 4 months ago #81584 by paulfiner
paulfiner replied the topic: Mandatory rows in array
To get them all to be mandatory, you don't need javascript at all....just set the option at the bottom of the page when you edit the question.

Please Log in to join the conversation.

More
5 years 4 months ago #81590 by tpartner
tpartner replied the topic: Mandatory rows in array
If you want to make them all mandatory and use this custom behaviour (turning the offending rows pink), change this:
$('div#question'+qID+' table.question tbody[id^="javatbd"]:not(:last) tr').addClass('mandatory');

To this:
$('div#question'+qID+' table.question tbody[id^="javatbd"] tr').addClass('mandatory');

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
5 years 4 months ago - 5 years 4 months ago #81756 by Bourbon
Bourbon replied the topic: Mandatory rows in array

To get them all to be mandatory, you don't need javascript at all....just set the option at the bottom of the page when you edit the question.

Sorry I was a bit vague. I ment that all rows are mandatory and the rows turn pink in unasnwered.


One problem occurs now.
"Repeating headings in array questions" is now turned pink, and I cant get past the array question, because the repeating heading row can't be answered.
Last Edit: 5 years 4 months ago by Bourbon. Reason: problem found

Please Log in to join the conversation.

More
5 years 4 months ago #81765 by tpartner
tpartner replied the topic: Mandatory rows in array
Please supply a sample question or survey so I can test.

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
5 years 4 months ago #81767 by Bourbon
Bourbon replied the topic: Mandatory rows in array

tpartner wrote: Please supply a sample question or survey so I can test.



File Attachment:

File Name: limesurvey...7544.lss
File Size:24 KB


Here you go
Attachments:

Please Log in to join the conversation.

More
5 years 4 months ago - 5 years 4 months ago #81770 by tpartner
tpartner replied the topic: Mandatory rows in array
If making all but the last row mandatory, replace this:
$('div#question'+qID+' table.question tbody[id^="javatbd"]:not(:last) tr').addClass('mandatory');
With this:
$('div#question'+qID+' table.question tbody[id^="javatbd"]:not(:last) tr:not(.headings)').addClass('mandatory');


If making all rows mandatory, replace this:
$('div#question'+qID+' table.question tbody[id^="javatbd"] tr').addClass('mandatory');
With this:
$('div#question'+qID+' table.question tbody[id^="javatbd"] tr:not(.headings)').addClass('mandatory');

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 5 years 4 months ago by tpartner.
The following user(s) said Thank You: Bourbon

Please Log in to join the conversation.

More
5 years 4 months ago #81771 by Bourbon
Bourbon replied the topic: Mandatory rows in array
Thank you tpartner !!!

It works great!

Please Log in to join the conversation.

More
5 years 4 months ago #81811 by Mazi
Mazi replied the topic: Mandatory rows in array

Bourbon wrote: Thank you tpartner !!!

It works great!

You're welcome :-)

If our hints have been helpful and you enjoy limesurvey please consider a donation to give Limesurvey a future .
We do all this in our free time and you don't have to pay a penny for this software.

Without your help we can't keep this project alive.


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now