How to correctly validate in jquery for a empty text input

akram1

New Member
This is my jQuery code to validate a html table:\[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 < '0') { alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) + " Marks"; } else if (marks > '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\]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:Higher than 0:\[code\]You have errors on Question Number: 1Your Total Marks Remaining does not equal 0 ? You Have 7 Marks Remaining\[/code\]Less than 0:\[code\]You have errors on Question Number: 1Your Total Marks Remaining does not equal 0 ? You Need To Remove 3 Marks\[/code\]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:\[code\]You have errors on Question Number: NaN? You have not entered in a value for all the Indivdiaul Marks textbox\[/code\]Why can't it display the question number for this validation?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?Below is an example html table:\[code\]<table border='1' id='markstbl'><thead><tr><th class='questionth'>Question No.</th><th class='answermarksth'>Marks per Answer</th><th class='noofmarksth'>Marks Remaining</th></tr></thead><tbody><tr class="questiontd"><td class="questionnumtd q<?php echo$questionId?>_qnum" name="numQuestion" rowspan="1">1 <input type="hidden" name="q1_ans_org" class="q1_ans_org" value="http://stackoverflow.com/questions/13715987/5"><input type="hidden" name="q1_ans" class="q1_ans" value="http://stackoverflow.com/questions/13715987/5"></td><td class="answermarkstd"><input class="individualMarks q1_mark" q_group="1" name="answerMarks[]" class="individualtext" type="text" onkeypress="return isNumberKey(event)" maxlength="3" /></td><td class="noofmarkstd q1_ans_text" q_group="1" rowspan="1"><strong>5</strong></td></tr> </tbody></table>\[/code\]
 
Back
Top