System.ComponentModel.DataAnnotations.DateAttribute.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;

            DateTime retDate;

            return DateTime.TryParse(Convert.ToString(value), out retDate);
        }
    }

Usage Example

        public void IsValidTests()
        {
            var attribute = new DateAttribute();

            Assert.IsTrue(attribute.IsValid(null)); // Don't check for required
            Assert.IsTrue(attribute.IsValid(DateTime.Now));
            Assert.IsTrue(attribute.IsValid(DateTime.Now.ToShortDateString()));
            Assert.IsTrue(attribute.IsValid("12/31/2010"));
            Assert.IsFalse(attribute.IsValid(12));
            Assert.IsFalse(attribute.IsValid("12.90"));
            Assert.IsFalse(attribute.IsValid("February 24th"));
            Assert.IsFalse(attribute.IsValid("yesterday"));
            Assert.IsFalse(attribute.IsValid("80/80/2011"));
        }
All Usage Examples Of System.ComponentModel.DataAnnotations.DateAttribute::IsValid