I have a form with an email field and a "get newsletter" checkbox.
<form method="post" class="form-horizontal" action="">
<input class="form-control" type="text" id="Email" name="Email" value="[email protected]" />
<span class="text-danger field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>
<input data-val="true" id="GetNewsletter" name="GetNewsletter" type="checkbox" value="true" />
</form>
The email field is initially optional, but required if user checks the get newsletter checkbox. I added this on client side using jquery validation.
$("#GetNewsletter").change(function () {
var checked = $(this).prop("checked");
if (checked) {
$('#Email').rules('add', { //set required rule
required: true,
messages: {
required: "Email is required."
}
});
}
else {
$('#Email').rules('remove', 'required'); //remove rule and call valid()
$('#Email').valid();
}
});
If the checkbox is unchecked, I remove the rule and call valid() to reset the status of the input. Unfortunately, this does not remove the validation message.
Is there a built-in way to clear the validation message? I want to avoid manipulating the markup, e.g. remove/add validation classes.
.valid()after removing the rule should remove the message.