I'm working on a project in which there are several surveys. One of them contains few questions where users have to enter number inputs as their responses and they will get an error messsage for that questions, if sum of the responses exceeds 100% when pressing submit button. (The survey is consisting multiple questions in a webpage.).
I'm sharing the HTML of responses for such a question which do have number input fields
<div class="input_row cell_wrapper row col s12" question-input-
template="number" question-input-id="qid:258 group_id:1 input_value:1">
<div class="input-field col s12">
<input name="qid:258 group_id:1 input_value:1" type="number"
class="validate">
</div>
</div>
<div class="input_row cell_wrapper row col s12" question-input-
template="number" question-input-id="qid:258 group_id:1 input_value:2">
<div class="input-field col s12">
<input name="qid:258 group_id:1 input_value:2" type="number"
class="validate">
</div>
</div>
<div class="input_row cell_wrapper row col s12" question-input-
template="number" question-input-id="qid:258 group_id:1 input_value:3">
<div class="input-field col s12">
<input name="qid:258 group_id:1 input_value:3" type="number"
class="validate">
</div>
</div>
I am trying to validate the sum of the above text input fields to 100% (not more than that) using jQuery. Kindly note that the fields are not the part of a particular form. What I've researched and referred are almost based on forms, e.g., StackExchange and StackOverflow.
Any idea?
Will it work?
var sumOfValues = 0;
$(".validate").each(function(){
sumOfValues += $(this).val();
});
if(sumOfValues == 100)
{
//alert message
}
else
{
//alert message
}