I have a form with an input that collects a dollar amount (ex: $5,000
)
How can I allow an input of $5,000
to not trigger (!ModelState.IsValid)
remove comma (,
) and $
for POSTing?
ViewModel
public class FormViewModel
{
[Required, RegularExpression("[0-9,]")]
public int? Amount { get; set; }
}
View
<form>
<input asp-for="Amount" />
<span asp-validation-for="Amount"></span>
<button type="submit">Submit</button>
</form>
Controller
[HttpPost]
public IActionResult PostForm(FormViewModel viewModel)
{
if (!ModelState.IsValid)
{
return View(viewModel)
}
else
{
//Post Form
}
}