|
using System;
namespace CodeProject.ComponentModel
{
[AttributeUsage(AttributeTargets.Property)]
public sealed class StringLengthValidatorAttribute : ValidatorAttribute
{
int min;
int max = int.MaxValue;
public StringLengthValidatorAttribute()
{
FailureTemplate = Properties.Resources.StringLengthValidationFailure;
}
public int Min
{
get { return min; }
set { min = value; }
}
public int Max
{
get { return max; }
set { max = value; }
}
public override string Validate(object value)
{
string stringValue = (string)value;
int length = stringValue == null ? 0 : stringValue.Length;
if (length < Min || length > Max)
return this.FormatObject(FailureTemplate);
return string.Empty;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Windows developer with 10+ years experience working in the banking industry.