I been trying to do some jquery validation where a textfield or dropdown is validated only if a checkbox is checked.
Here is what I have:
@using (Ajax.BeginForm("SomeAction", "SomeController", new { id = Model.Id }, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "somePanel", InsertionMode = InsertionMode.Replace }, new { @id = "testForm" }))
{
@Html.DropDownListFor(x => Model.SelectedValue, Model.YesNoList, " ", new { @id ="drpYesNo" })
@Html.TextAreaFor(x => x.CriminalText, new { @id = "txtSomeField" })
<input type="submit" value="Save" />
}
In the javascript I have this:
<script type="text/javascript">
jQuery.validator.messages.required = "";
$("#testForm").validate(
{
rules: {
txtSomeField: {
required: function (element) {
return $("input:checkbox[name='drpYesNo']:checked").val() == 'Yes';
}
}
}
});
$("#testForm").on("submit", function () {
if (!$(this).valid()) {
return false;
}
});
</script>
I am following some of the example that are online, but can't seem to work. Any help is much appreciated. Happy Fourth of July!