Here is my problem.
I have a razor generated form that has multiple fields but I want email to only be required if the System Admin radio button is clicked. It is defaulted to system user.
<div class="">@Html.LabelFor(m => m.Email)</div>
<div class="">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control required", placeholder = "Email" })
@Html.ValidationMessageFor(m => m.Email)
</div>
I even tried removing the "required" after form control and taking out the validation.
I have 2 radio buttons that are generated by razor that have the same name but different values.
<div class="inline-editor create-user-roles">
<input id="UserRole" name="UserRole" type="radio" value="System Administrator">
<input id="UserRole" name="UserRole" type="radio" value="System User">
</div>
Here is the JQuery function that I am trying to use.
$('input[name="UserRole"]').on('change', function () {
if ($('input[name="UserRole"]:checked').val() == "System Administrator") {
$('#Email').rules('add', {
required: true, messages: { required: "This field is required" }
});
}
else {
$('#Email').rules('add', {
required: false
});
}
});