asp.net-mvcvalidationvalidationsummary

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

I want to change that text to something else.


Solution

  • 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.