I want to concatenate messages in my alert so that it show all error messages for the first question it encounters errors on. For example if I have 3 questions and there are errors in question 1 and question 3, when I click on the submit button, the validation alert should display an example alert like so:
You have errors on Question Number: 1
• You have not entered in a value in all the Indivdiaul Marks textbox
• You have not entered in a value in all the Indivdiaul Marks textbox Your Total Marks Remaining does not equal 0
It is only after I have solved errors in question 1 that when I submit the page again then it shows the alert for question number 3 as an example below:
You have errors on Question Number: 3
• You have not entered in a value in all the Indivdiaul Marks textbox
• You have not entered in a value in all the Indivdiaul Marks textbox Your Total Marks Remaining does not equal 0
The problem I am having is that it is only showing 1 question number at a time which is fine but it is showing all of the errors from question 1 and 3 so it looks like this below:
You have errors on Question Number: 1
• You have not entered in a value in all the Indivdiaul Marks textbox
• You have not entered in a value in all the Indivdiaul Marks textbox Your Total Marks Remaining does not equal 0
• You have not entered in a value in all the Indivdiaul Marks textbox
• You have not entered in a value in all the Indivdiaul Marks textbox Your Total Marks Remaining does not equal 0
My question is then how can I get the alert working so it matches the way I want it to work?
Below is the jquery code:
function validation() {
var alertValidation = "";
var _qid = "";
var _msg = "";
$("input[data-type='qmark']").each(function(i) {
var questions = $(this).attr("data-qnum");
var marks = parseInt($("[class*=q" + i + "_ans_text]").text());
var txtinput = $(this).val();
_qid = questions;
_msg = "You have errors on Question Number: " + _qid + "\n";
if (txtinput == '') {
alertValidation += "\n\u2022 You have not entered in a value in all the Indivdiaul Marks textbox\n";
}
if(marks < '0') {
alertValidation += "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) + " Marks";
}
if(marks > '0') {
alertValidation += "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Have " + marks + " Marks Remaining";
}
});
//comment
if (alertValidation != "") {
alert(_msg + alertValidation);
return false;
}
return true;
}
In the above code where it says //comment
, if I include this code:
if (alertValidation != "") {
return false; //Stop the each loop
}
Then what it does is that it displays only one message per alert per question e.g
except alerting:
You have errors on Question Number: 1
• You have not entered in a value in all the Indivdiaul Marks textbox
• You have not entered in a value in all the Indivdiaul Marks textbox Your Total Marks Remaining does not equal 0
It will just alert:
You have errors on Question Number: 1
• You have not entered in a value in all the Indivdiaul Marks textbox
UPDATE:
Below is an update of the code:
function validation() {
var alertValidation = "";
var _qid = "";
$("input[data-type='qmark']").each(function(i) {
var questions = $(this).attr("data-qnum");
var marks = parseInt($("[class*=q" + i + "_ans_text]").text());
var txtinput = $(this).val();
_qid = questions;
alertValidation += "\nYou have errors on Question Number: " + _qid + "\n";
if (txtinput == '') {
alertValidation += "\n\u2022 You have not entered in a value in all the Indivdiaul Marks textbox\n";
}
if(marks < '0')
{
alertValidation += "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) + " Marks";
}
if(marks > '0')
{
alertValidation += "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Have " + marks + " Marks Remaining";
}
});
if (alertValidation != "") {
alert(alertValidation.substr(1));
return false;
}
return true;
}
But the problem is that below is what it alerts:
• You have errors on Question Number: 1
• You have not entered in a value in all the Indivdiaul Marks textbox You have errors on Question Number: 1
• You have not entered in a value in all the Indivdiaul Marks textbox Your Total Marks Remaining does not equal 0
• You Have 5 Marks RemainingYou have errors on Question Number: 2
• You have errors on Question Number: 3
• You have not entered in a value in all the Indivdiaul Marks textbox Your Total Marks Remaining does not equal 0
• You Have 5 Marks RemainingYou have errors on Question Number: 3
• You have not entered in a value in all the Indivdiaul Marks textbox You have errors on Question Number: 3
• You have not entered in a value in all the Indivdiaul Marks textbox