Steve B
Steve B

Reputation: 37660

Validation a string length : either X or Y, not between X and Y?

I often use the validation framework with something like :

[StringLength(10, MinimumLength=5)]
public string MyString{ get; set; }

This allows me specify the MyString length must be between 5 and 10 characters long.

Now I have to check if the string is either 5 long, or 10 long. No other length are allowed. I'm quite sure the StringLength attribute is inadequate, so how to do that ? Have I to extend the validation framework and how ?

thx

Upvotes: 0

Views: 1250

Answers (2)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391406

If you can (I'm not familiar with the validation library in question) you can use a regular expression.

The regular expression to validate any character, length 5 or 10, would be:

^.{5}(.{5})?$

If you only have digits, you can use:

^\d{5}(\d{5})?$

Upvotes: 1

user156888
user156888

Reputation:

using the fluent validation api i'd just use:

RuleFor(x => x.Property).Length(10);

Upvotes: 0

Related Questions