2

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.

Chiefster
  • 61
  • 1
  • 10
  • 1
    the framework uses the data annotations on the password property of your view model to generate the error message. Start there. – Shyju Nov 15 '17 at 22:28
  • I do have that, and it shows right above the password field. what i am trying to change is the error message in the validation summary. when using an HTML.ValidationSummary helper. I am unable to find that text above by doing a find all in the entire solution – Chiefster Nov 16 '17 at 00:15
  • The text you're looking for is the default `ErrorMessage` property of your `DataAnnotation`. You can custom it easily, see my answer. – AlexB Nov 17 '17 at 09:32

2 Answers2

2

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.

AlexB
  • 7,302
  • 12
  • 56
  • 74
0

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$.");
            }
Chiefster
  • 61
  • 1
  • 10
  • 1
    so this kinda worked.. however now it is using that same error message for different types of errors. I a m going to try what the AlexB suggested – Chiefster Nov 18 '17 at 16:58
  • Please feel free to accept one of the proposed answers if you solved your problem – AlexB Jan 03 '19 at 10:23