FadoBagi
FadoBagi

Reputation: 191

How to change validation message from string length ASP.NET Core

I want to validate input in a form field that takes string input. ASP.Net Core already checks if it is empty or not because it is required.

enter image description here

I have put max and min length attributes on the module and when the input is not valid I want to change this message.

enter image description here

Upvotes: 0

Views: 868

Answers (2)

Ibrahim Timimi
Ibrahim Timimi

Reputation: 3750

For complex validation requirements, you can check out FluentValiation. It is a great tool to help make your validation easy to create and easy to maintain.

https://docs.fluentvalidation.net/en/latest/aspnet.html

Upvotes: 1

FadoBagi
FadoBagi

Reputation: 191

The solution is really simple. All I had to do was add this attribute to the model property

[StringLength(8, ErrorMessage = "{0} length must be between {2} and {1}.", MinimumLength = 6)]

Where the first parameter is the maximum length and the last parameter is the minimum length

More information about this model validation here

Upvotes: 1

Related Questions