仅当前一个字段为0.01美元或更多时,我才需要验证字段。我正在使用asp.net mvc来编写代码。我的页面如下所示:
<div class="form-group">
@Html.LabelFor(m => m.AdditionalIncomeAmt, new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
<div class="col-sm-4">
@Html.TextBoxFor(m => m.AdditionalIncomeAmt, new { @class = "form-control", placeholder = "Additional Income Amount", id = "additionalIncome" })
@Html.ValidationMessageFor(m => m.AdditionalIncomeAmt)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.AdditionalIncomeSource, new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
<div class="col-sm-4">
@Html.TextBoxFor(m => m.AdditionalIncomeSource, new { @class = "form-control", placeholder = "Additional Income Source", id = "additionalIncomeSource" })
@Html.ValidationMessageFor(m => m.AdditionalIncomeSource)
</div>
</div>
</div>
我在两个字段的模型中都需要标记。
[Required(ErrorMessage = "Additional income amount is required")]
[Display(Name = "Additional Income Amount")]
public string AdditionalIncomeAmt { get; set; }
[Required(ErrorMessage = "Additional income source is required")]
[Display(Name = "Additional Income Source (If any)")]
public string AdditionalIncomeSource { get; set; }
我需要在HomeController.cs中编写什么类型的代码才能在金额大于.01时验证该字段,或者如果金额等于0.00则删除验证?
我试过这个没有成功
if (model.AdditionalIncomeAmt == null)
{
this.ModelState.Remove("AdditionalIncomeSource");
}
答案 0 :(得分:0)
在javascript中编写自己的验证函数将是最简单的方法。
$('#btnSubmit').click(function(){
if(your condition true)
{
$('form').submit()
}
else
{
// show validation
}
});