Welcome to the LimeSurvey Community Forum

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

Validate fields in a table

  • eniisula
  • eniisula's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
2 years 1 week ago #227340 by eniisula
Validate fields in a table was created by eniisula
Please help us help you and fill where relevant:
Your LimeSurvey version: Version 5.2.13+220207
Own server or LimeSurvey hosting: own
==================
Here I have attached my question. I want to validate the first 3 fields so it only takes numbers and on the 4th field I want to show the sum of these fields.
Can anyone help me achieve this?

Thank you in advance for taking your time to help me with this!

E

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 1 week ago #227346 by tpartner
Replied by tpartner on topic Validate fields in a table
This script will render the text input in the last row readonly, enforce numerics in the first column and load the total for the first column in the last row.

Code:
<script type="text/javascript" data-author="Tony Partner">  
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // Identify this question
    var q1ID = '{QID}';
    var thisQuestion = $('#question'+q1ID);
 
    //Identify the inputs
    var activeInputs = $('tr[id^="javatbd"]:not(:last) .answer-item:nth-child(2) input:text.form-control', thisQuestion);
    var totalInput = $('tr[id^="javatbd"]:last .answer-item:nth-child(2) input:text.form-control', thisQuestion);
 
    // Render the text input in the last row readonly
    $('tr[id^="javatbd"]:last .answer-item:nth-child(2) input:text', thisQuestion).prop('readonly', true);
 
    // Listener on the text inputs in first coloumn, except last row
    $(activeInputs).on('keyup change', function(e) {
      var thisValue = $.trim($(this).val());
 
      // Strip out non-numerics characters
      newValue = thisValue.replace(/\D/g,'').replace(/,/g,'').replace(/\./g,'');
      $(this).val(newValue);
 
      // Load the total into the last row
      var total = 0;
      $(activeInputs).each(function(i) {
        total = total + Number($(this).val());
      });
      $(totalInput).val(total).trigger('change')
    });
  });
</script>



Sample survey attached: 

File Attachment:

File Name: limesurvey...5(1).lss
File Size:31 KB

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

Please Log in to join the conversation.

  • eniisula
  • eniisula's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
2 years 1 week ago #227347 by eniisula
Replied by eniisula on topic Validate fields in a table
That works perfectly! Thank you for your time and devotion you took into this!

Highly appreciated!

All the best

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose