簡體   English   中英

ASP.NET 雙重驗證,僅限數字的自定義消息

[英]ASP.NET Double validation, custom message for numbers only

我對 Fueling 屬性進行了雙重驗證。

[Display(Name = "Tankolás")]
    [DisplayFormat(DataFormatString = "{0} l", ApplyFormatInEditMode = false)]
    public double? Fueling { get; set; }

如果我在輸入中輸入字母而不是數字,則會收到錯誤消息:值“測試”對 Tankolás 無效。

我該如何更改錯誤消息?

這是視圖:

<div class="form-group">
                <label asp-for="Fueling" class="control-label"></label>
                <input id="Fueling" asp-for="Fueling" class="form-control" placeholder="Tankolás" />
                <span asp-validation-for="Fueling" class="text-danger"></span>
            </div>

編輯 - 完整查看代碼

@model MainProject.Models.RoadRecord


@{
    ViewData["Title"] = "Hozzáadás";
}
<head>
    <link rel="stylesheet" href="~/css/RoadRecords/create.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>

<div class="container">
    <div class="text-center">
        <h2>Új Menetlevél hozzáadása</h2>
    </div>
    <div class="row justify-content-center">
        <div class="col-8">
            <form asp-action="Create">
                <input type="hidden" asp-for="UserId" />
                <input type="hidden" asp-for="LicencePlate" />

                <div class="form-group">
                    <label asp-for="Mileage" class="control-label"></label>
                    <div class="input-group-sm">
                        <input asp-for="Mileage" class="form-control" placeholder="Kilométer" />
                        <div id="mileageContainer" class="input-group-append">
                            <partial name="_ShowMileagePartialView.cshtml" model="@Model" />
                        </div>
                    </div>
                    <span asp-validation-for="Mileage" class="text-danger"></span>
                    @if (!string.IsNullOrEmpty(ViewBag.Message))
                    {
                        <span class="text-danger">
                            @ViewBag.Message
                        </span>
                    }
                </div>

                @*
                    <div class="form-group">
                        <label asp-for="TimeStamp" class="control-label"></label>
                        <input type="datetime-local" asp-for="TimeStamp" class="form-control" value="@DateTime.Now.ToString("yyyy-MM-ddTHH:mm")" />
                        <span asp-validation-for="TimeStamp" class="text-danger"></span>
                    </div>
                *@

                <div class="form-group">
                    <label asp-for="Location" class="control-label"></label>
                    <div class="input-group">
                        <input id="varos" type="text" asp-for="Location" class="form-control" aria-describedby="getLocationButton" />
                        <div class="input-group-append">
                            <button type="button" class="btn btn-primary" id="getLocationButton">Pozícióm</button>
                        </div>
                    </div>
                    <span asp-validation-for="Location" class="text-danger"></span>
                </div>

                <div class="form-group">
                    <label asp-for="Type" class="control-label"></label>
                    <select id="selectType" asp-for="Type" class="form-control">
                        <option selected value="C">Céges</option>
                        <option value="M">Magán</option>
                    </select>
                </div>

                <div class="form-group">
                    <label asp-for="Fueling" class="control-label"></label>
                    <input id="Fueling" asp-for="Fueling" class="form-control" placeholder="Tankolás" />
                    <span asp-validation-for="Fueling" class="text-danger"></span>
                </div>

                <div class="form-group">
                    <label asp-for="Note" class="control-label"></label>
                    <input asp-for="Note" class="form-control" placeholder="Megjegyzés" />
                    <span asp-validation-for="Note" class="text-danger"></span>
                </div>

                <div class="form-group">
                    @Html.AntiForgeryToken()<input onclick="ConvertDouble()" type="submit" value="Hozzáadás" class="btn btn-primary w-100" />
                </div>
            </form>

            <a class="add" asp-action="Index" asp-route-licencePlate="@ViewBag.LicencePlate">
                <button type="button" class="btn btn-secondary w-100">
                    Vissza
                </button>
            </a>
        </div>
    </div>
</div>

@section Scripts {

    <!--GEO Location-->
    <script>
        $(document).ready(function () {
            $('#getLocationButton').click(function () {
                $.getJSON('https://geolocation-db.com/json/')
                    .done(function (location) {
                        $('#country').html(location.country_name);
                        $('#state').html(location.state);
                        $('#city').html(location.city);
                        $('#latitude').html(location.latitude);
                        $('#longitude').html(location.longitude);
                        $('#ip').html(location.IPv4);
                        document.getElementById('varos').value = location.city;
                    });
            });
        });

        $.getJSON('https://geolocation-db.com/json/')
            .done(function (location) {
                console.log("kint");
                if (document.getElementById('varos').value == '') {
                    console.log("bent");
                    $('#country').html(location.country_name);
                    $('#state').html(location.state);
                    $('#city').html(location.city);
                    $('#latitude').html(location.latitude);
                    $('#longitude').html(location.longitude);
                    $('#ip').html(location.IPv4);
                    document.getElementById('varos').value = location.city;
                }
            });
    </script>

    <!--Get Milage-->
    <script>
        $(function () {
            $.ajax({
                type: "Get",
                url: "../RoadRecords/ShowMileage?licencePlate=" + "@ViewBag.LicencePlate",
                success: function (data) {
                    $("#mileageContainer").html(data);
                },
                error: function (response) {
                    console.log(response.responseText);
                }
            });
        });
    </script>
    <script>
        function ConvertDouble() {
            var param = $("#Fueling").val();
            if (param.indexOf(".") != -1) {
                param = param.replace(".", ",");
            }
            $("#Fueling").val(param);
        }
    </script>

}

這是我的完整視圖代碼。 我不得不刪除驗證 js,因為燃料輸入,如果我輸入例如 12,5 或 12.5,我會收到錯誤消息,所以我刪除了它,並且由於文化的原因,它可以使用逗號(我來自匈牙利),並且在如果用戶鍵入點而不是逗號,我將用逗號替換點的 javascript。

您可以在屬性中添加RegularExpressionErrorMessage以添加規則和自定義 ErrorMessage

    [Display(Name = "Tankolás")]
    [RegularExpression(@"^[0-9]*$",ErrorMessage = "You can use numbers only")]
    public double? Fueling { get; set; }

看法

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM