예제 #1
0
        // http://www.regular-expressions.info/creditcard.html
        public CardType GetCCType(string proposedNumber)
        {
            Regex reg = new Regex("");

            if (proposedNumber.Length < Card.CC_LEN_FOR_TYPE)
            {
                return(CardType.UNKNOWN);
            }

            CardType lastcardtype = System.Enum.GetValues(typeof(CardType)).Cast <CardType> ().Last();

            for (int idx = 0; idx < (int)lastcardtype; ++idx)
            {
                switch (idx)
                {
                case  (int)CardType.VISA:
                    reg = visaTypeReg;
                    break;

                case  (int)CardType.MASTERCARD:
                    reg = mcTypeReg;
                    break;

                case  (int)CardType.AMEX:
                    reg = amexTypeReg;
                    break;

                case (int)CardType.MAESTRO:
                    reg = maestroTypeReg;
                    break;
                }


                CSRange range = new CSRange(0, Card.CC_LEN_FOR_TYPE);


                var matches = reg.Matches(proposedNumber.Substring(range.Location, range.Length));
                if (matches != null)
                {
                    if (matches.Count == 1)
                    {
                        return((CardType)idx);
                    }
                }
            }
            return(CardType.UNKNOWN);
        }
예제 #2
0
        internal string FormatForViewing(string enteredNumber)
        {
            string cleaned = CleanNumber(enteredNumber);
            int    len     = cleaned.Length;

            if (len <= Card.CC_LEN_FOR_TYPE)
            {
                return(cleaned);
            }

            CSRange r2 = new CSRange();

            r2.Location = 0;
            CSRange r3 = new CSRange();

            r3.Location = 0;
            CSRange r4 = new CSRange();

            r4.Location = 0;
            string[] gaps = new string[] { @"", @"", @"" };

            int[] segmentLengths = new int[3] {
                0, 0, 0
            };

            switch (GetCCType(enteredNumber))
            {
            case CardType.VISA:
            case CardType.MASTERCARD:
            case CardType.DISCOVER:             // { 4-4-4-4}
            case CardType.MAESTRO:
                segmentLengths [0] = 4;
                segmentLengths [1] = 4;
                segmentLengths [2] = 4;
                break;

            case CardType.AMEX:                 // {4-6-5}
                segmentLengths [0] = 6;
                segmentLengths [1] = 5;
                break;

            case CardType.DINERS_CLUB:  // {4-6-4}
                segmentLengths [0] = 6;
                segmentLengths [1] = 4;
                break;

            default:
                return(enteredNumber);
            }

            len -= Card.CC_LEN_FOR_TYPE;
            CSRange[] r = new CSRange[3] {
                r2, r3, r4
            };
            int totalLen = Card.CC_LEN_FOR_TYPE;

            for (int idx = 0; idx < 3; ++idx)
            {
                int segLen = segmentLengths [idx];


                r [idx].Location = totalLen;
                r [idx].Length   = len >= segLen ? segLen : len;
                totalLen        += segLen;
                len       -= segLen;
                gaps [idx] = @" ";


                if (len <= 0)
                {
                    break;
                }
            }

            string segment1 = enteredNumber.Substring(0, Card.CC_LEN_FOR_TYPE);
            string segment2 = r2.Location == 0 ? @"" : enteredNumber.Substring(r2.Location, r2.Length);
            string segment3 = r3.Location == 0 ? @"" : enteredNumber.Substring(r3.Location, r3.Length);

            ;
            string segment4 = r4.Location == 0 ? @"" : enteredNumber.Substring(r4.Location, r4.Length);

            ;

            string ret = string.Format(@"{0}{1}{2}{3}{4}{5}{6}",
                                       segment1, gaps [0],
                                       segment2, gaps [1],
                                       segment3, gaps [2],
                                       segment4);

            return(ret);
        }