简体   繁体   中英

ASP.NET CORE RegularExpression data annotation ignoring error message

I have a model called CreateStaffViewModel which has a data annotation for the password which checks the user has entered an upper, lower, numeric and special character and at least 8 characters long. Along with a user friendly Error Message.

My problem is that on the view the Error Message is being ignored and the regular expression is being showed.

public class CreateStaffViewModel : StaffViewModel
        {
            [RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*([^a-zA-Z\d\s])).{8,}$",
                ErrorMessage = "Must include upper, lower, numeric and special characters")]
            public string Password { get; set; }

            [Compare(nameof(Password), ErrorMessage = "Passwords do not match")]
            public string ConfirmPassword { get; set; }
        }

在此处输入图片说明

How can I make the regular expression on the view display the user friendly error message.

PS. I have already viewed the answer Regular expression error message and implemented the answers. Which NONE have worked for me.

UPDATE: View Code

    <form asp-action="Register">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="Email" class="control-label"></label>
                    <input asp-for="Email" class="form-control" />
                    <span asp-validation-for="Email" class="text-danger"></span>
                </div>


                <div class="form-group">
                    <label asp-for="Password" class="control-label"></label>
                    <input asp-for="Password" type="password" class="form-control" />
                    @*<span  asp-validation-for="Password" class="text-danger"></span>*@
                    @Html.ValidationMessageFor(x => x.Password)
                </div>

                <div class="form-group">
                    <label asp-for="ConfirmPassword" class="control-label"></label>
                    <input asp-for="ConfirmPassword" type="password" class="form-control" />
                    <span asp-validation-for="ConfirmPassword" class="text-danger"></span>
                </div>

在剃刀视图中:

@Html.ValidationMessageForm(model => model.Password)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM