14

I have such attribute which is checking for minimum and maximum string length:

    [StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)]
public string PropertyName {get; set;}

Basically it is how it is declared in default asp.net mvc 3.0 template(they just have Maximum 100).

So if string contain less than 3 characters the display error message will be

"The PropertyName  must be at least 3 characters long."

So I don't see any problems wit that in such case

But if string length will be more than 10 the display message still will be

"The PropertyName  must be at least 3 characters long."

So it is getting not correct now

But if i change the message template to something like that:

    [StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long and maximum {1} characters long", MinimumLength = 3)]

The error message for both cases will be like that:

"The PropertyName must be at least 3 characters long and maximum 10 characters long"

And it is not so good as well

1) The message is to long

2) It would be better to display separate message for each case so and question is how could I do hat?

Is there any way I could display two different messages, one message if length is less than minimum and another message is length is greater than maximum?

I could see only one way to fix that is to declare two separate StringLength attributes one would check for minimum an another for maximum but it is impossible to add two same attributes on method, also I would go with MinLength and MaxLength attributes but they not generating unobtrusive attributes. So looks like I should write my custom validation attribute

But it would be great to display two different messages on each case (min and max) for one StringLength validation attribute if it would be possible, what do you think?

Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
Joper
  • 8,019
  • 21
  • 53
  • 80
  • 2
    Although a bit late, perhaps using a shorter error message would work. `The {0} must be between {1} and {2} characters long.` – saluce Nov 16 '12 at 19:29
  • I think displaying a message `The field must be between 3 and 100 characters` is completely sufficient for any use case. I think that if you make your own attribute to show different messages you're spending way too much time in the wrong areas. – The Muffin Man Oct 27 '13 at 23:47
  • How can I do same thing but without numbers and special string, in your case you can enter three numbers and there will be no errors, but i need just words to count. – mikrimouse Mar 19 '14 at 16:43

1 Answers1

24

You can use MinLength, MaxLength attributes. It is clearer than two StringLength attributes

[MinLength(3, ErrorMessage="The {0} must be at least {2} characters long")]
[MaxLength(10, ErrorMessage="The {0} must be maximum {2} characters long")]        
public string PropertyName { get; set; }
dohaivu
  • 2,038
  • 15
  • 17
  • 4
    MinLength and MaxLength does not generating unobtrusive validation attributes – Joper Aug 29 '11 at 05:32
  • 1
    For ASP.NET MVC 5.1: Client-side validation for string and array types will now be supported for properties decorated with the MinLength and MaxLength attributes. [Read this](http://www.asp.net/mvc/overview/releases/mvc51-release-notes#Unobtrusive). – Daniel Dušek Nov 30 '15 at 16:02