Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to correctly validate in jquery for a empty text input
    primarykey
    data
    text
    <p>This is my jQuery code to validate a html table:</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"; } else 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>Now what I find strange is that if the validation for when the number of marks remaining is higher than 0 and the validation for when the number of marks remainig is lower than 0 works perfectly when the alert is outputted as it displays this below:</p> <p><strong>Higher than 0:</strong></p> <pre><code>You have errors on Question Number: 1 Your Total Marks Remaining does not equal 0 • You Have 7 Marks Remaining </code></pre> <p><strong>Less than 0:</strong></p> <pre><code>You have errors on Question Number: 1 Your Total Marks Remaining does not equal 0 • You Need To Remove 3 Marks </code></pre> <p>But when I try to validate to see if a text input under the "Marks per Answer" section is empty, then it displays this message below:</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>Why can't it display the question number for this validation?</p> <p>Also another slight problem I have is that even if I have entered in a value in all the text inputs under "Marks per Answer", it still displays the above validation message alert when it shouldn't do. Why does it do this as well?</p> <p>Below is an example html 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;th class='noofmarksth'&gt;Marks Remaining&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr class="questiontd"&gt; &lt;td class="questionnumtd q&lt;?php echo$questionId?&gt;_qnum" name="numQuestion" rowspan="1"&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" value="5"&gt;&lt;/td&gt; &lt;td class="answermarkstd"&gt; &lt;input class="individualMarks q1_mark" q_group="1" name="answerMarks[]" class="individualtext" type="text" onkeypress="return isNumberKey(event)" maxlength="3" /&gt; &lt;/td&gt; &lt;td class="noofmarkstd q1_ans_text" q_group="1" rowspan="1"&gt;&lt;strong&gt;5&lt;/strong&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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