StringLengthValidator Behavior

Topics: Validation Application Block
May 23, 2007 at 7:54 PM
I'm curious about the behavior for the StringLengthValidator. When a value is null the result is invalid. Is this the expected behavior? Is this what is required?

If so, it seems a bit verbose. Also if this is the case the documentation shows the above sample minus the negated portion as an example to use the Or Composite Validator. I guess that's a bad example if not null is already enforced by string length.
May 24, 2007 at 3:02 PM
I agree with you.

In your case, you are requiring that the length of the string be 10 characters or less. If the value is null, that should return true in my opinion. The length is indeed <= 10.

If you were to add a lower limit and say the string length has to be 1 - 10 characters, I would expect a null value to be invalid, because the length is indeed not between 1 and 10.

There is definitely some inconsistency with how the StringLengthValidator handles nulls in this case. Your example seems to be the best way around it.




David Hayden
Microsoft MVP C#