Beispiel #1
0
        public static CardType ParseFromNumber(string number)
        {
            var validationExpression = new CreditCardValidationExpression();

            return(new[]
            {
                Amex,
                Visa,
                MasterCard,
                Discover
            }
                   .Where(card => Regex.IsMatch(number, validationExpression.GetExpression(card)))
                   .FirstOrDefault());
        }
        public bool ValidateCVV(string number, string cvv)
        {
            var validationExpression = new CreditCardValidationExpression();

            // American Express requires 4 digit cvv
            if (Regex.IsMatch(number, validationExpression.GetExpression(CardType.Amex)) &&
                Regex.IsMatch(cvv, "^[0-9]{4}$"))
            {
                return(true);
            }

            // while the rest require 3 digit cvv
            return((Regex.IsMatch(number, validationExpression.GetExpression(CardType.Visa)) ||
                    Regex.IsMatch(number, validationExpression.GetExpression(CardType.Discover)) ||
                    Regex.IsMatch(number, validationExpression.GetExpression(CardType.MasterCard))) &&
                   Regex.IsMatch(cvv, "^[0-9]{3}$"));
        }