I'm using MVC 3 with jQuery 1.7.2 and I've implemented some custom validation on client and server side to validate that a certain textarea has text if a checkbox is checked.
My client side code to implement unobstrusive validation looks like this:
$.validator.addMethod("requiredif", function (value, element, params) {
if (value) {
var id = '#' + params["otherproperty"];
var control = $(id);
if (control.val() == '') {
return false;
}
}
return $.validator.methods.required.call(this, value, element, params);
});
$.validator.unobtrusive.adapters.add("requiredif", ["otherproperty"], function(options) {
options.rules["requiredif"] = options.params;
options.messages["requiredif"] = options.message;
});
My problem is that even though the validation fires if I subsequently go back and untick the checkbox (i.e. text is no longer required), the validation message stays put and validation is not cleared.
I have tried the following to clear the validation:
$('#Dashboard').on('change', '#Damaged', null, function () {
var validator = $('#Gridform').validate();
validator.resetForm();
});
But this doesn't make any difference.
What I would like is for the validation to work the same as the standard validation does i.e. when I untick the checkbox or enter content into the textarea the validation is cleared/reset.
requiredifadapter is associated with the textarea, not with the checkbox.