[英]Required Field Validation only when field is visible in asp.net mvc
我有一个复选框,您可以在其中隐藏/显示日期选择器字段。
但是当 datepicker 字段被隐藏时,就不必进行验证。
这是复选框:
@Html.CheckBox("showEindDatum", (Request.Form["showEindDatum"] ?? string.Empty).Contains("true"))
这是日期选择器:
@FormGroupHelper.CreateFormGroup(Html, m => m.EindDatum, Html.Kendo().DatePickerFor(m => m.EindDatum).Min(Model.EindDatum.HasValue ? Model.EindDatum.Value : DateTime.Today).Format("dd-MM-yyyy").ParseFormats(new string[] { "ddMMyyyy" }))
此字段的属性如下所示:
[Display(Name = "Einddatum wijziging")]
[Required(ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "Global_Validatie_VeldVerplicht")]
public DateTime? EindDatum { get; set; }
public bool showEindDatum { get; set; }
这是它的jquery:
if ($('#showEindDatum').prop('checked')) {
$(".EinddatumDatepicker").show();
$("#meldingEindDatumCheck").show();
}
else {
$(".EinddatumDatepicker").hide();
$("#meldingEindDatumCheck").hide();
}
$("#showEindDatum").on("click", function ()
{
$(".EinddatumDatepicker").toggle(this.checked);
$("#meldingEindDatumCheck").toggle(this.checked);
});
那么我必须改变的是当日期选择器字段隐藏时没有验证?
谢谢
所以你的意思是:
$.validator.setDefaults({ ignore: null });
if ($('#showEindDatum').prop('checked')) {
$(".EinddatumDatepicker").show();
$("#meldingEindDatumCheck").show();
$("#geenEinddatumIngevuld").hide();
}
else {
$(".EinddatumDatepicker").hide();
$("#meldingEindDatumCheck").hide();
$("#geenEinddatumIngevuld").show();
}
$("#showEindDatum").on("click", function () {
$(".EinddatumDatepicker").toggle(this.checked);
$("#meldingEindDatumCheck").toggle(this.checked);
$("#geenEinddatumIngevuld").toggle(this.checked);
});
这不起作用。
但是表单也有一个 post 方法,像这样:
using (Html.BeginForm("Formulier", "PersoneelsDossier", FormMethod.Post, new { @id = "PDForm", name = "PDForm" }))
}
你的意思是这样的:
$(document).ready(function () {
//$("#PDForm").data("validator").settings.ignore = ".ignore, :hidden";
if ($('#showEindDatum').prop('checked')) {
$(".EinddatumDatepicker").show();
$("#meldingEindDatumCheck").show();
$("#geenEinddatumIngevuld").hide();
}
else {
$(".EinddatumDatepicker").hide();
$("#meldingEindDatumCheck").hide();
$("#geenEinddatumIngevuld").show();
}
$("#showEindDatum").on("click", function () {
$(".EinddatumDatepicker").toggle(this.checked);
$("#meldingEindDatumCheck").toggle(this.checked);
$("#geenEinddatumIngevuld").toggle(this.checked);
});
$.validator.setDefaults({ ignore: null });
});
你的意思是这样的:
$.validator.setDefaults({ ignore: null });
if ($('#showEindDatum').prop('checked')) {
$(".EinddatumDatepicker").show();
$("#meldingEindDatumCheck").show();
$("#geenEinddatumIngevuld").hide();
}
else {
$(".EinddatumDatepicker").hide();
$("#meldingEindDatumCheck").hide();
$("#geenEinddatumIngevuld").show();
}
$("#showEindDatum").on("click", function () {
$(".EinddatumDatepicker").toggle(this.checked);
$("#meldingEindDatumCheck").toggle(this.checked);
$("#geenEinddatumIngevuld").toggle(this.checked);
});
不起作用
默认情况下,jQuery 验证会忽略隐藏字段、宽度和高度为零的元素、css display:none 和任何具有不可见父元素的元素(使用相同的标准)。
然而,可以通过添加以下脚本来覆盖忽略设置
// By default validator ignores hidden fields.
// change the setting here to ignore nothing
$.validator.setDefaults({ ignore: null });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.