I have to validate a field only if a previous field is $0.01 or more. I am using asp.net mvc to code this. My page looks like this:
<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>
I have required tags in my model on both fields.
[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; }
What type of code would I need to write in the HomeController.cs to only validate that field if the amount is greater than .01 or remove validation if the amount is equal to 0.00?
I've tried this to no success
if (model.AdditionalIncomeAmt == null)
{
this.ModelState.Remove("AdditionalIncomeSource");
}