信用卡号 验证

using System;

namespace CreditCards
{
    /// <summary>
    /// Summary description for CreditCardValidator.
    /// </summary>
    public class CreditCardValidator
    {
        public CreditCardValidator()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public bool ValidateCC(string creditNo)
        {
            int indicator = 1;        //-- will be indicator for every other number
            int firstNumToAdd = 0;  //-- will be used to store sum of first set of numbers
            int secondNumToAdd = 0; //-- will be used to store second set of numbers
            string num1;            //-- will be used if every other number added is greater than 10, store the left-most integer here
            string num2;            //-- will be used if ever yother number added is greater than 10, store the right-most integer here

            //-- Convert our creditNo string to a char array
            char[] ccArr = creditNo.ToCharArray();

            for (int i=ccArr.Length-1;i>=0;i--)
            {
                char ccNoAdd = ccArr[i];
                int ccAdd = Int32.Parse(ccNoAdd.ToString());
                if (indicator == 1)
                {
                    //-- If we are on the odd number of numbers, add that number to our total
                    firstNumToAdd += ccAdd;
                    //-- set our indicator to 0 so that our code will know to skip to the next piece
                    indicator = 0;
                }
                else
                {
                    //-- if the current integer doubled is greater than 10
                    //-- split the sum in to two integers and add them together
                    //-- we then add it to our total here
                    if ((ccAdd + ccAdd) >= 10)
                    {
                        int temporary = (ccAdd + ccAdd);
                        num1 = temporary.ToString().Substring(0,1);
                        num2 = temporary.ToString().Substring(1,1);
                        secondNumToAdd += (Convert.ToInt32(num1) + Convert.ToInt32(num2));
                    }
                    else
                    {
                        //-- otherwise, just add them together and add them to our total
                        secondNumToAdd += ccAdd + ccAdd;
                    }
                    //-- set our indicator to 1 so for the next integer we will perform a different set of code
                    indicator = 1;
                }
            }
            //-- If the sum of our 2 numbers is divisible by 10, then the card is valid. Otherwise, it is not
            bool isValid = false;
            if ((firstNumToAdd + secondNumToAdd) % 10 == 0)
            {
                isValid = true;
            }
            else
            {
                isValid = false;
            }
            return isValid;
        }

        /// <summary>
        /// Get and check to make sure that the chosen credit card type matches the 
        /// card number
        /// </summary>
        /// <param name="CCNum">string</param>
        /// <returns>bool</returns>
        public bool RetrieveCCType(string CCNum, int intChosenType)
        {
            // return value
            bool bResult = false;

            if (Convert.ToInt32(CCNum.Substring(0,2)) >= 51 && Convert.ToInt32(CCNum.Substring(0,2)) <= 55)
            {
                // Check to see that the type of card that the user selected
                // matches the 
                if(intChosenType == 3)
                {
                    bResult = true;
                }
                else
                {
                    bResult = false;
                }
            }
            else if (Convert.ToString(CCNum.Substring(0,1)) == "4")
            {
                // Visa

                if(intChosenType == 4)
                {
                    bResult = true;
                }
                else
                {
                    bResult = false;
                }
            }
            else if (Convert.ToString(CCNum.Substring(0,2)) == "34" || Convert.ToString(CCNum.Substring(0,2)) == "37")
            {
                // american express
                // check to see that the card matches the type they chose
                if(intChosenType == 1)
                {
                    bResult = true;
                }
                else
                {
                    bResult = false;
                }
            }
            else if(Convert.ToString(CCNum.Substring(0,4)) == "6011")
            {
                // Discover card
                if(intChosenType == 2)
                {
                    bResult = true;
                }
                else
                {
                    bResult = false;
                }
            }
            else
            {
                bResult = false;
            }

            return bResult;
        }
    }
}

  

posted on 2016-08-01 15:56  风雨潜行  阅读(236)  评论(0编辑  收藏  举报

导航