public class MaxDigitsAttribute : ValidationAttribute
{
private int Max,
Min;
public MaxDigitsAttribute(int max, int min = 0)
{
Max = max;
Min = min;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (!IsValid(value))
{
return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
}
return null;
}
public override bool IsValid(object value)
{
// you could do any custom validation you like
if (value is int)
{
var stringValue = "" + (int)value;
var length = stringValue.Length;
if (length >= Min && length <= Max)
return true;
}
return false;
}
}
Validation Attribute to check range of digits for Integer Type
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.