- Posts: 88
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Validate fields in a table
- eniisula
- Topic Author
- Offline
- Premium Member
Less
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
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
Attachments:
Please Log in to join the conversation.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10109
- Thank you received: 3595
2 years 1 week ago #227346
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
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.
Sample survey attached:
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:
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
- Topic Author
- Offline
- Premium Member
Less
More
- Posts: 88
- Thank you received: 0
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
Highly appreciated!
All the best
Please Log in to join the conversation.