user1861818
user1861818

Reputation: 83

Validation function in jQuery is not working

I'm using the following HTML code to display a table:

<table border="1" id="markstbl">
   <thead>
      <tr>
         <th class="answermarksth">Marks per Answer</th>
         <th class="totalmarksth">Total Marks</th>
         <th class="noofmarksth">Marks Remaining</th>
      </tr>
   </thead>
   <tbody>
      <tr class="questiontd">
         <td class="answermarkstd">
            <input class="individualMarks q1_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" />
         </td>
         <td class="totalmarkstd" rowspan="2">4</td>
         <td class="noofmarkstd q1_ans_text" q_group="1" rowspan="2"><strong>4</strong></td>
      </tr>
      <tr class="questiontd">
         <td class="answermarkstd">
            <input class="individualMarks q2_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" />
         </td>
         <td class="totalmarkstd" rowspan="1">6</td>
         <td class="noofmarkstd q2_ans_text" q_group="1" rowspan="1"><strong>0</strong></td>
      </tr>
   </tbody>
</table>

Below is what the table looks like:

Marks Per Answer                Total Marks    Marks Remaining
(blank text input)                   4              4
(value=6 text input)                 6              0

I have tried to validate the table so that if a text input is blank, then it displays a message. If the 'Marks Remaining' is not equal to 0, then it displays a validation message (one validation message if greater than 0 and a different validation message if less than 0).

The problem is that it is not outputting any validation messages. Why is it not performing any validation?

This is my jQuery code:

function validation() {
    var alertValidation = "";
    var _qid = "";
    var _msg = "";
    var questions = $('#markstbl td[class*="_ans"]').length;

    $("[class*='q'").each(function (i) {
        var marks = parseInt($("[class*=q" + i + "_mark]").text());
        _qid = $(".q" + i + "_ans_org").text();
        _msg = "You have errors on Question Number: " + _qid + "\n";


        if (!this.value) {
            alertValidation += "\n\u2022 You have not entered in a value in the Indivdiaul Marks textbox all your Answers\n";
        }

        if ($("[class*=q" + i + "_mark]").text() < '0') {
            alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) + " Marks";
        } else if ($("[class*=q" + i + "_mark]").text() > '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;
}​

Upvotes: 0

Views: 85

Answers (1)

Cymen
Cymen

Reputation: 14419

You do have an error in your jQuery code. This selector $("[class*='q'") is invalid. It needs to be like this: $("[class*='q']").

Here is a jsfiddle with your code running: http://jsfiddle.net/jYZSX/

I'm still reading the question to figure out how the validation should work.

Upvotes: 1

Related Questions