简体   繁体   English

更改MVC中的验证摘要文本以进行密码验证

[英]Change Validation Summary Text in MVC for a password validation

How do I change the default message on the validation summary when a password does not meet the requirements. 当密码不符合要求时,如何更改验证摘要上的默认消息。 The current message the appears is 当前出现的消息是

"Passwords must have at least one digit ('0'-'9'). Passwords must have at least one uppercase ('A'-'Z')." “密码必须至少包含一位数字('0'-'9')。密码必须至少具有一位大写字母('A'-'Z')。”

I want to change that text to something else. 我想将文本更改为其他内容。

You can also use DataAnnotations 您也可以使用DataAnnotations

In your example, you can use : 在您的示例中,您可以使用:

// ~YourModelFile.cs

[RegularExpression(@"^[A-Z0-9]{6,}$", ErrorMessage = "Password must be at least 6 characters long")]
public string Password { get; set; }

Interesting point is the ErrorMessage may be placed in a Resources files, so you can display it in multiple languages. 有趣的是, ErrorMessage可能放置在Resources文件中,因此您可以用多种语言显示它。
Plus, you do not have to write a custom AddError method anymore. 另外,您不必再编写自定义的AddError方法。

I figured it out.. I hope it is the right way to do this. 我想通了..我希望这是正确的方法。 but here is my code. 但是这是我的代码。 I commented out one line and replaced with the one right under it. 我注释掉了一行,并用下面的一行代替。

private void AddErrors(IdentityResult result)
        {
            foreach (var error in result.Errors)
            {
                //ModelState.AddModelError("", error);
                ModelState.AddModelError("", "PASSWORDS MUST BE AT LEAST 6 CHARACTERS LONG, WITH AT LEAST ONE NUMBER, AND ONE NON LETTER OR DIGIT SUCH AS %, #, @, !, AND *. AN EXAMPLE: PASSWORD1$.");
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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