public void IsValidCreditCardNumberTest()
        {
            ICardNumberGenerator cardNumberGenerator = CardNumberGenerator.Instance;

            string numDash    = "4111-1111-1111-1111";
            string numSpace   = "4111 1111 1111 1111";
            string numNoSpace = "4111111111111111";
            string numBadSep  = "4111.1111.1111.1111";
            string numBadLen  = "4111-1111-1111-111";

            Assert.IsTrue(cardNumberGenerator.IsValidCreditCardNumber(numDash), "IsValidNumber should allow numbers with dashes");
            Assert.IsTrue(cardNumberGenerator.IsValidCreditCardNumber(numSpace), "IsValidNumber should allow numbers with spaces");
            Assert.IsTrue(cardNumberGenerator.IsValidCreditCardNumber(numNoSpace), "IsValidNumber should allow numbers with no spaces");
            Assert.IsFalse(cardNumberGenerator.IsValidCreditCardNumber(numBadLen), "IsValidNumber should not allow numbers with too few numbers");
            Assert.IsFalse(cardNumberGenerator.IsValidCreditCardNumber(numBadSep), "IsValidNumber should not allow numbers with dot separators");
        }
Exemple #2
0
    /// <summary>
    /// Action to Validate Credit Card Number.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnValidate_Click(object sender, EventArgs e)
    {
        ICardNumberGenerator cardNumberGenerator = CardNumberGenerator.Instance;

        if (txtCard.Text.Length > 0)
        {
            string cardNum = txtCard.Text;

            if (cardNumberGenerator.IsValidCreditCardNumber(cardNum))
            {
                Constants.CardIssuer?cardType = cardNumberGenerator.GetCardTypeFromNumber(cardNum);
                string strCardType            = (cardType == null) ? "Unknown" : cardType.ToString();

                showMessage(String.Format("You have entered a valid card number. The card type is {0}.", strCardType), true);
            }
            else
            {
                showMessage("Card failed test. Please enter a valid card number.", false);
            }

            if (cardNumberGenerator.IsRewardsCardNumber(cardNum))
            {
                Constants.CardIssuer?cardType = cardNumberGenerator.GetCardTypeFromNumber(cardNum);
                string strCardType            = (cardType == null) ? "Unknown" : cardType.ToString();

                showMessage(String.Format("You have entered a reward card number. The card type is {0}.", strCardType), true);
            }
        }
        else
        {
            showMessage("Please enter a card number first.", false);
        }
    }