Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to validate empty text input and stop the display of NaN?
    primarykey
    data
    text
    <p>I'm using the following HTML code to display a table: </p> <pre><code>&lt;table border='1' id='markstbl'&gt; &lt;thead&gt; &lt;tr&gt; &lt;th class='questionth'&gt;Question No.&lt;/th&gt; &lt;th class='answermarksth'&gt;Marks per Answer&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr class="questiontd"&gt; &lt;td class="questionnumtd" name="numQuestion" rowspan="2"&gt;1 &lt;input type="hidden" name="q1_ans_org" class="q1_ans_org" value="5"&gt;&lt;input type="hidden" name="q1_ans" class="q1_ans"&gt;&lt;/td&gt; &lt;td class="answermarkstd"&gt; &lt;input class="individualMarks q1_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr class="questiontd"&gt; &lt;td class="answermarkstd"&gt; &lt;input class="individualMarks q1_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr class="questiontd"&gt; &lt;td class="questionnumtd" name="numQuestion" rowspan="1"&gt;2 &lt;input type="hidden" name="q2_ans_org" class="q2_ans_org" value="5"&gt;&lt;input type="hidden" name="q2_ans" class="q2_ans"&gt;&lt;/td&gt; &lt;td class="answermarkstd"&gt; &lt;input class="individualMarks q2_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>Below is what the table looks like:</p> <pre><code>Question No. Marks Per Answer 1 (blank text input) (blank text input) 2 (blank text input) </code></pre> <p>As you can see question 1 contains 2 text inputs while question 2 contains 1 text input.</p> <p>Now what I want to do with the code below is validate for any empty text inputs.</p> <pre><code>function validation() { var alertValidation = ""; var _qid = ""; var _msg = ""; $("[class*='q']").each(function(i) { var questions = parseInt($("[class*=q" + i + "_qnum]").text()); var marks = parseInt($("[class*=q" + i + "_ans_text]").text()); var txtinput = $("[class*=q" + i + "_mark]").val(); _qid = questions; _msg = "You have errors on Question Number: " + _qid + "\n"; if (txtinput == '' || txtinput == null) { alertValidation += "\n\u2022 You have not entered in a value for all the Indivdiaul Marks textbox\n"; } if (alertValidation != "") { return false; //Stop the each loop } }); if (alertValidation != "") { alert(_msg + alertValidation); return false; } return true; } </code></pre> <p>The problems is that in the alert it does not display the question number <code>_qid</code> as that it displays it as NaN and also even though no text input is blank, it still displays an alert stating that not all inputs contains a value.</p> <p>My quqestion is how do I display the question number and how do I properly check for a empty text input correct so that if text input is empty, then it displays the alert with the correct question number and if all text inputs contains a value, then it does not display a validation?</p> <p>I like to say that the <code>_qid</code> does work for when I include the code below for another validation check in the <code>validation()</code> function which is placed exactly below the if statement checking for empty text input:</p> <pre><code>if(marks &lt; '0') { alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) + " Marks"; } else if(marks &gt; '0') { alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Have " + marks + " Marks Remaining"; } </code></pre> <p>When the alert appears for the above validations, it displays the question number in the alert. It just doesn't display it when I validate empty text input? Is it because I may have multiple text inputs within a question so I might need to include another foreach() loop in the code for empty text input?</p> <p>BELOW IS WHAT THE FULL <code>Validation()</code> CODE LOOKS LIKE:</p> <pre><code>function validation() { var alertValidation = ""; var _qid = ""; var _msg = ""; $("[class*='q']").each(function(i) { var questions = parseInt($("[class*=q" + i + "_qnum]").text()); var marks = parseInt($("[class*=q" + i + "_ans_text]").text()); var txtinput = $("[class*=q" + i + "_mark]").val(); _qid = questions; _msg = "You have errors on Question Number: " + _qid + "\n"; if (txtinput == '' || txtinput == null) { alertValidation += "\n\u2022 You have not entered in a value for 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"; } else if(marks &gt; '0') { alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Have " + marks + " Marks Remaining"; } if (alertValidation != "") { return false; //Stop the each loop } }); if (alertValidation != "") { alert(_msg + alertValidation); return false; } return true; } </code></pre> <p>Below are examples of what the validation messages look like:</p> <p><strong>Marks Higher than 0 validation:</strong></p> <pre><code>You have errors on Question Number: 1 Your Total Marks Remaining does not equal 0 • You Have NaN Marks Remaining </code></pre> <p><strong>Marks Less than 0 validation:</strong></p> <pre><code>You have errors on Question Number: 2 Your Total Marks Remaining does not equal 0 • You Need To Remove NaN Marks </code></pre> <p><strong>Empty Text input validation:</strong></p> <pre><code>You have errors on Question Number: NaN • You have not entered in a value for all the Indivdiaul Marks textbox </code></pre> <p>As you can see above alert is the only one which doesn't display question number</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload