1

我正在创建一个使用剑道验证的 MVC 页面。

我有以下模型:

public class ForgotPasswordModel
{
    [Required(ErrorMessage = " ")]
    public string UserName { get; set; }

    [Required(ErrorMessage = " ")]
    [EmailAddress(ErrorMessage = " ")]
    public string Email { get; set; }

    public string PhoneNumber { get; set; }

    [Required(ErrorMessage = " ")]
    public string Code { get; set; }
}

我不希望我的视图显示电子邮件字段的错误消息。我希望它在验证失败时只显示感叹号。

目前我的视图中有以下代码:

@model Site.Models.ForgotPasswordModel

@{
    ViewBag.Title = "Forgot Password";
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta name="viewport" content="width=device-width" />
    <title>Login</title>

    <link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />

    <script src="~/Scripts/jquery-3.1.1.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>

    <link href="~/Content/kendo/kendo.common-material.min.css" rel="stylesheet" />
    <link href="~/Content/kendo/kendo.custom.css" rel="stylesheet" />
    <link href="~/Content/kendo/custom.css" rel="stylesheet" />

    <script src="~/Scripts/kendo/kendo.all.min.js"></script>
    <script src="~/Scripts/kendo/kendo.aspnetmvc.min.js"></script>


    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

    <style>
        input[type=text]::-ms-clear {
            display: none;
        }

        input[type=password]::-ms-reveal {
            display: none;
        }

        .errMsg {
            font-weight: bolder;
            color: red;
        }

        span#UserName_validationMessage, span#Email_validationMessage,span#Code_validationMessage  {
            display: inline-block;
            width: 160px;
            text-align: left;
            border: 0;
            padding: 0;
            margin: -20px;
            background: none;
            box-shadow: none;
            color: red;
        }

        .k-invalid-msg {
            display: none;
        }
    </style>

</head>
<body>
    <div> 
    </div>
    <div style="margin-top: 20px;">
        <div style="margin: 0 auto; width: 940px; height: 409px; background-image: url('../../Content/images/finance-globe.jpg');"/>
            @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "ForgotPasswordForm" }))
            {
                <div style="margin: 0 auto; margin-top: 20px;">
                    <table id="ForgotPassword" style="margin: 0" auto;">
                        <tr style="height:40px;">
                            <td>@Html.EditorFor(x => x.UserName, new { htmlAttributes = new { @class = "form-control k-textbox checkError", placeholder = "username" } })</td>
                        </tr>
                        <tr style="height:40px;">
                            <td>@Html.EditorFor(x => x.Email, new { htmlAttributes = new { @class = "form-control k-textbox checkError", placeholder = "email" } })</td>
                        </tr>
                        <tr style="height:40px;">
                            <td>@Html.EditorFor(x => x.PhoneNumber, new { htmlAttributes = new { @class = "form-control k-textbox", placeholder = "phone number" } })</td>
                        </tr>
                        <tr style="height:40px;">
                            <td>@Html.EditorFor(x => x.Code, new { htmlAttributes = new { @class = "form-control k-textbox checkError", placeholder = "code" } })</td>
                        </tr>
                    </table>
                </div>
            }
    </div>
</body>
</html>

<script>
    $(document).ready(function () {
        $("#ForgotPasswordForm").kendoValidator();
    });
</script>

模型和视图产生以下内容:

在此处输入图像描述

我不希望在正则表达式验证失败的情况下显示“电子邮件无效”消息。

我只需要一个感叹号。

我怎样才能做到这一点?

4

1 回答 1

0

对于您的电子邮件字段,您可以尝试添加 data-email-msg属性,这是为错误的电子邮件字段值自定义验证消息的方法。

例如:如果您想为错误的电子邮件 ID 值显示空白消息,您可以执行以下操作:

<input type="email" name="email" id="email" name="email" data-email-msg="">

如果您也想自定义required field validation message,那么您可以添加另一个属性data-required-msg,如下所示:

<input type="email" name="email" id="email" name="email" data-email-msg="" data-required-msg="">
于 2017-09-27T06:19:13.457 回答