c# - HTML Razor 中 Step 属性的自定义错误验证

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

当用户输入使 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");
        }
    }

结果: enter image description here

关于c# - HTML Razor 中 Step 属性的自定义错误验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65328864/

相关文章:

C# 如何定义新的指​​针结构?

html - 如何垂直对齐div中的元素?

html - 需要 css 大小修复,请

authentication - 重定向到 ASP.NET Core 的自定义 "access denied"页面

c# - 如何修复 Azure Web 应用程序上的 ASP.NET MVC 中的缓慢加载操作

c# - 将 IEnumerable<dynamic> 转换为 JsonArray

c# - 使用 .Net 调用 Servicenow Web 服务

php - 如何选择单个或多个复选框按钮?

c# - ASP.NET核心: How to get original path in error page by 404 status code

asp.net-core - ASP.NET JWT : Signature validation failed. 未提供安全 key 来验证签名