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

            var valueAsString = value as string;
            return valueAsString != null && _regex.Match(valueAsString).Length > 0;
        }

Usage Example

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

            Assert.IsTrue(attribute.IsValid(null));
            Assert.IsTrue(attribute.IsValid("20245597151"));
            Assert.IsTrue(attribute.IsValid("20-24559715-1"));
            Assert.IsTrue(attribute.IsValid("27-23840320-6"));
            Assert.IsTrue(attribute.IsValid("27238403206"));

            Assert.IsFalse(attribute.IsValid("20 24559715 1"));
            Assert.IsFalse(attribute.IsValid(""));
            Assert.IsFalse(attribute.IsValid("123456789"));
            Assert.IsFalse(attribute.IsValid("aa-aaaaaaaa-a"));
            Assert.IsFalse(attribute.IsValid("4408 0412 3456 7890"));
            Assert.IsFalse(attribute.IsValid(0));
            Assert.IsFalse(attribute.IsValid(20245597151));
        }