Exemplo n.º 1
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            if (!IsCreditCardValid(CreditCardNumber))
            {
                yield return(new ValidationResult("Credit card number invalid"));
            }

            if (!string.IsNullOrEmpty(ExpiryDate))
            {
                var dateDigits = ExpiryDate.Replace("/", "");
                if (dateDigits.Length == 4 && ValidationHelper.IsNumeric(dateDigits))
                {
                    var month = int.Parse(dateDigits.Substring(0, 2));
                    var year  = int.Parse("20" + dateDigits.Substring(2, 2));

                    if (month >= 1 && month <= 12 && year >= 2000 && year <= 2099)
                    {
                        // Set the exp date to the last day of month
                        var expDate = new DateTime(year, month, DateTime.DaysInMonth(year, month));
                        if (expDate < DateTime.Today)
                        {
                            yield return(new ValidationResult("Credit card expired"));
                        }
                    }
                    else
                    {
                        yield return(new ValidationResult("Expiry date invalid"));
                    }
                }
                else
                {
                    yield return(new ValidationResult("Expiry date invalid"));
                }
            }

            yield return(ValidationResult.Success);
        }