How to validate empty text input and stop the display of NaN?

DDeadRights

New Member
I'm using the following HTML code to display a table: \[code\]<table border='1' id='markstbl'> <thead> <tr> <th class='questionth'>Question No.</th> <th class='answermarksth'>Marks per Answer</th> </tr> </thead> <tbody> <tr class="questiontd"> <td class="questionnumtd" name="numQuestion" rowspan="2">1 <input type="hidden" name="q1_ans_org" class="q1_ans_org" value="http://stackoverflow.com/questions/13734028/5"><input type="hidden" name="q1_ans" class="q1_ans"></td> <td class="answermarkstd"> <input class="individualMarks q1_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" /> </td> </tr> <tr class="questiontd"> <td class="answermarkstd"> <input class="individualMarks q1_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" /> </td> </tr> <tr class="questiontd"> <td class="questionnumtd" name="numQuestion" rowspan="1">2 <input type="hidden" name="q2_ans_org" class="q2_ans_org" value="http://stackoverflow.com/questions/13734028/5"><input type="hidden" name="q2_ans" class="q2_ans"></td> <td class="answermarkstd"> <input class="individualMarks q2_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" /> </td> </tr> </tbody> </table>\[/code\]Below is what the table looks like:\[code\]Question No. Marks Per Answer 1 (blank text input) (blank text input) 2 (blank text input) \[/code\]As you can see question 1 contains 2 text inputs while question 2 contains 1 text input.Now what I want to do with the code below is validate for any empty text inputs.\[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\]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.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?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:\[code\]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"; }\[/code\]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?BELOW IS WHAT THE FULL \[code\]Validation()\[/code\] CODE LOOKS LIKE:\[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 < '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\]Below are examples of what the validation messages look like:Marks Higher than 0 validation:\[code\]You have errors on Question Number: 1Your Total Marks Remaining does not equal 0 ? You Have NaN Marks Remaining\[/code\]Marks Less than 0 validation:\[code\]You have errors on Question Number: 2Your Total Marks Remaining does not equal 0 ? You Need To Remove NaN Marks\[/code\]Empty Text input validation:\[code\]You have errors on Question Number: NaN? You have not entered in a value for all the Indivdiaul Marks textbox\[/code\]As you can see above alert is the only one which doesn't display question numberThanks
 
Back
Top