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!
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>