Change Validation Summary Text in MVC for a password validation

1.2k Views Asked by At

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')."

I want to change that text to something else.

2

There are 2 best solutions below

0
AlexB On BEST ANSWER

You can also use 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.
Plus, you do not have to write a custom AddError method anymore.

2
Chiefster On

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$.");
            }