Custom error validation for Step property in HTML Razor

Viewed 118

I need to provide a custom validation message for when user input invalidates the step = ".01"

Currently, the validation message is the default "Please enter a multiple of 0.01". How can I change this?

@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>
1 Answers

Here is a demo(remove <span></span> in your code,and use a new span to put your error message):

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

result: enter image description here

Related