【问题标题】:Custom error validation for Step property in HTML RazorHTML Razor 中 Step 属性的自定义错误验证
【发布时间】:2021-03-27 10:40:14
【问题描述】:

当用户输入使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>

【问题讨论】:

    标签: c# html asp.net-core dom razor


    【解决方案1】:

    这是一个演示(删除代码中的&lt;span&gt;&lt;/span&gt;,并使用新的跨度来放置您的错误消息):

    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");
            }
        }
    

    结果:

    【讨论】:

      猜你喜欢
      • 2015-11-17
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多