How to correctly concatenate messages correctly in an alert
<p>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:</p>

<blockquote>
  <p>You have errors on Question Number: 1</p>
  
  <p>• You have not entered in a value in all the Indivdiaul Marks textbox</p>
  
  <p>• You have not entered in a value in all the Indivdiaul Marks textbox
  Your Total Marks Remaining does not equal 0</p>
</blockquote>

<p>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:</p>

<blockquote>
  <p>You have errors on Question Number: 3</p>
  
  <p>• You have not entered in a value in all the Indivdiaul Marks textbox</p>
  
  <p>• You have not entered in a value in all the Indivdiaul Marks textbox
  Your Total Marks Remaining does not equal 0</p>
</blockquote>

<p>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:</p>

<blockquote>
  <p>You have errors on Question Number: 1</p>
  
  <p>• You have not entered in a value in all the Indivdiaul Marks textbox</p>
  
  <p>• You have not entered in a value in all the Indivdiaul Marks textbox
  Your Total Marks Remaining does not equal 0 </p>
  
  <p>• You have not entered in a value in all the Indivdiaul Marks textbox</p>
  
  <p>• You have not entered in a value in all the Indivdiaul Marks textbox
  Your Total Marks Remaining does not equal 0</p>
</blockquote>

<p>My question is then how can I get the alert working so it matches the way I want it to work?</p>

<p>Below is the jquery code:</p>

<pre><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 &lt; '0') {
            alertValidation += "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) +  " Marks";   
        }

        if(marks &gt; '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;
}
</code></pre>

<p>In the above code where it says <code>//comment</code>, if I include this code:</p>

<pre><code>if (alertValidation != "") {
    return false; //Stop the each loop 
}
</code></pre>

<p>Then what it does is that it displays only one message per alert per question e.g</p>

<p>except alerting:</p>

<blockquote>
  <p>You have errors on Question Number: 1</p>
  
  <p>• You have not entered in a value in all the Indivdiaul Marks textbox</p>
  
  <p>• You have not entered in a value in all the Indivdiaul Marks textbox
  Your Total Marks Remaining does not equal 0</p>
</blockquote>

<p>It will just alert:</p>

<blockquote>
  <p>You have errors on Question Number: 1</p>
  
  <p>• You have not entered in a value in all the Indivdiaul Marks textbox</p>
</blockquote>

<pre><code>   UPDATE:
</code></pre>

<p>Below is an update of the code:</p>

<p>function validation() {</p>

<pre><code>    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 &lt; '0')
{

 alertValidation += "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) +  " Marks";   
}

if(marks &gt; '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;
}
</code></pre>

<p>But the problem is that below is what it alerts:</p>

<blockquote>
  <p>• You have errors on Question Number: 1</p>
  
  <p>• You have not entered in a value in all the Indivdiaul Marks textbox
  You have errors on Question Number: 1</p>
  
  <p>• You have not entered in a value in all the Indivdiaul Marks textbox
  Your Total Marks Remaining does not equal 0 </p>
  
  <p>• You Have 5 Marks RemainingYou have errors on Question Number: 2</p>
  
  <p>• You have errors on Question Number: 3</p>
  
  <p>• You have not entered in a value in all the Indivdiaul Marks textbox
  Your Total Marks Remaining does not equal 0 </p>
  
  <p>• You Have 5 Marks RemainingYou have errors on Question Number: 3</p>
  
  <p>• You have not entered in a value in all the Indivdiaul Marks textbox
  You have errors on Question Number: 3</p>
  
  <p>• You have not entered in a value in all the Indivdiaul Marks textbox</p>
</blockquote>
