System.ComponentModel.DataAnnotations.MinAttribute.IsValid C# (CSharp) Method

IsValid() public method

Determines whether the specified value of the object is valid.
public IsValid ( object value ) : bool
value object The value of the object to validate.
return bool
        public override bool IsValid(object value)
        {
            if (value == null) return true;

            double valueAsDouble;

            var isDouble = double.TryParse(Convert.ToString(value), out valueAsDouble);

            return isDouble && valueAsDouble >= _min;
        }

Usage Example

        public void IsValidIntegerTests()
        {
            const int min = 42;
            var attribute = new MinAttribute(min);

            Assert.IsTrue(attribute.IsValid(null));  // Optional values are always valid
            Assert.IsTrue(attribute.IsValid("42"));
            Assert.IsTrue(attribute.IsValid(42.5));
            Assert.IsTrue(attribute.IsValid("50"));
            Assert.IsTrue(attribute.IsValid(100));
            Assert.IsTrue(attribute.IsValid("10000000000"));
            Assert.IsFalse(attribute.IsValid("41"));
            Assert.IsFalse(attribute.IsValid("10"));
            Assert.IsFalse(attribute.IsValid(0));
            Assert.IsFalse(attribute.IsValid("-1"));
            Assert.IsFalse(attribute.IsValid(-50));
            Assert.IsFalse(attribute.IsValid("fifty"));
        }
All Usage Examples Of System.ComponentModel.DataAnnotations.MinAttribute::IsValid