当用户输入使 step = ".01"
无效时,我需要提供自定义验证消息
目前,验证消息默认为“请输入 0.01 的倍数”。我该如何改变这个?
@Html.TextBoxFor(x => x.CostChanges[i].MyVariable, new { @class = "form-control", step = ".01" })
<span asp-validation-for="@Model.CostChanges[i].MyVariable" class="text-danger"></span>
最佳答案
这是一个演示(删除代码中的 <span></span>
,并使用新的范围来放置错误消息):
html:
@Html.TextBoxFor(x => x.CostChanges[i].MyVariable, new { @class = "form-control", step = ".01", onblur = "check(this)",type="number" })
<span id="CostChanges_@(i)__MyVariable-error" class="text-danger field-validation-valid"></span>
js:
function check(input) {
if ($(input).attr("aria-invalid")) {
var id = $(input).attr("id") + "-error";
//put your custom error message to the text
$("#"+id).text("custom error message");
}
}
关于c# - HTML Razor 中 Step 属性的自定义错误验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65328864/