validate the value of the selected radio button
I would like to validate a quiz. Each question has 3 answers, I need the validation script to match the value of the selected radio button with the proper answer. I have been searching far and wide for an answer. I feel like I found one, the only issue is that it doesn't validate the selected value, it validate any of the values.
Here is my code:
- jQuery.validator.addMethod("correctAnswer", function(value, element, params) {
return this.optional(element) || value == params;
}, "Select the correct answer to move on.");
$(document).ready(function() {
// validate signup form on keyup and submit
$("#onlinetrainingquiz").validate({
rules: {
a1: {
required: true,
correctAnswer: "z"
}
}
},
messages: {
a1: {
required: "All questions must be answered.",
},
}
});
});
This correctAnswer method I have added checks the value of all of the radio buttons in the set. Let me explain, the radio buttons have 3 answers, 'a', 'b' & 'c'. When I set the correct answer to 'a', no matter which radio button is select, the script says the answer is correct. When I set that value to 'z', the script says the answer is incorrect. I need the method to check the value of the selected radio button.
Any ideas? Have I been clear? If you would like to see the page, you can view it here: http://bit.ly/9lQqAQ
Thanks for your assistance.