【问题标题】:How to use client-side validation to work on your asp.net mvc in c#?如何使用客户端验证在 c# 中处理您的 asp.net mvc?
【发布时间】:2020-11-18 22:46:02
【问题描述】:

我使用 jQuery 进行客户端验证,以验证我的 @EditorFor() 控件何时有空白空间。但是,当我过滤任何数据时,错误不会消失。

我的逻辑中还需要一些示例,因为当错误弹出时,必须在中心显示图像,目前它在侧面。

// Model
     [Required]
        public int ZipCode { get; set; }
        public string Country { get; set; }

        [Required]
        public string Email { get; set; }

        [Required]
        public string CellNumber { get; set; }

// View

<div class="form-group row">
                                <label for="ZipCode" class="col-sm-2 col-form-label"></label>
                                <div class="col-sm-3">
                                    @Html.EditorFor(model => model.RegForm.ZipCode, new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus", placeholder = "ZipCode", id = "textZipCode" } })
                                    @Html.ValidationMessageFor(model => model.RegForm.ZipCode, "", new { @class = "text-danger" })
                                </div>
                                <label id="labelMessage_zip" class="text-danger" style="display:none">This field is required</label>
                            </div>

                            <div class="form-group row">
                                <label for="Email" class="col-sm-2 col-form-label">Email:</label>

                                <div class="col-sm-4">
                                    @Html.EditorFor(model => model.RegForm.Email, new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus", placeholder = "Email", id = "textEmail" } })
                                    @Html.ValidationMessageFor(model => model.RegForm.Email, "", new { @class = "text-danger" })
                                </div>
                                <label id="labelMessage_email" class="text-danger" style="display:none">This field is required</label>
                            </div>

                            <div class="form-group row">
                                <label for="Attendee" class="col-sm-2 col-form-label">Attendee Cell Number*</label>
                                <div class="col-sm-3">
                                    @Html.EditorFor(model => model.RegForm.CellNumber, new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus", placeholder = "Cell Number", id ="textCellNumber" } })
                                    @Html.ValidationMessageFor(model => model.RegForm.CellNumber)
                                </div>
                                <label id="labelMessage_cell" class="text-danger" style="display:none">This field is required</label>
                            </div>
            <script type="text/javascript" src="~/Content/dist/js/jquery.validate.unobtrusive.min.js"></script>

            <!--Handling form-validation when empty-->
            <script type='text/javascript'>
                $(function () {
                    //When the blur event occurs from your Textbox (you lose focus)
                    $('#textEmail').blur(function () {
                        var email = document.getElementById("textEmail").value;
                        var expr = /[a-z0-9._%+-]+[a-z0-9.-]+\.[a-z]{2,}$/;
                        if (!expr.test(email)) {
                            document.getElementById("labelMessage_email").style.display = "inline";
                        }
                        else {
                            document.getElementById("labelMessage_email").style.display = "none";
                        }
                    });
                });

                // Error message for cell-phone.
                $(function () {
                    $('#textCellNumber').blur(function () {
                        var cell = document.getElementById("textCellNumber").value;
                        var expr = /[a-z0-9._%+-]+[a-z0-9.-]+\.[a-z]{2,}$/;
                        if (!expr.test(cell)) {
                            document.getElementById("labelMessage_cell").style.display = "inline";
                        } else {
                            document.getElementById("labelMessage_cell").style.display = "none";
                        }
                    });

                });


                // Error message for ZipCode.
                $(function () {
                    $('#textZipCode').blur(function () {
                        var zipcode = document.getElementById("textZipCode").value;
                        var expr = /[a-z0-9._%+-]+[a-z0-9.-]+\.[a-z]{2,}$/;
                        if (!expr.test(zipcode)) {
                            document.getElementById("labelMessage_zip").style.display = "inline";
                        } else {
                            document.getElementById("labelMessage_zip").style.display = "none";
                        }
                    });
                });
            </script>

【问题讨论】:

  • 只需使用 MVC 提供的非侵入式验证即可。为不显眼的验证添加 jaquery 参考。
  • 有什么我可以用的例子吗?
  • 答案中提供了进一步的帮助

标签: javascript c# jquery asp.net-mvc bootstrap-4


【解决方案1】:

使用这些库:

"~/Content/assets/js/jquery.validate.min.js"
"~/Content/assets/js/jquery.validate.unobtrusive.min.js"

并在您的模型中将属性上面的属性写为:

[Required]
public string Email { get; set; }

为属性使用库:using System.ComponentModel.DataAnnotations

【讨论】:

  • 好的,Imran 让我现在开始工作,如有必要,请提供反馈,谢谢。
  • 你看到有一个小问题,电子邮件只有一个工作。这意味着当有一封电子邮件时,错误就会消失,这与 zipCode 和 CellNumber 不同。他们似乎没有工作。让我向您展示我在这个主题中的逻辑。
  • 这里是应用非侵入式验证的详细示例c-sharpcorner.com/article/asp-net-mvc5-jquery-form-validator
猜你喜欢
  • 2010-09-14
  • 2014-07-21
  • 1970-01-01
  • 1970-01-01
  • 2013-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多