Example #1
0
        private bool Validate()
        {
            try
            {
                if (CardNumber.Length < 12 || CardNumber.Length > 19)
                {
                    return(false);
                }
                var IIN = CardNumber.Substring(0, 6);
                CardType = eCardType.Unknown;
                if (IIN.StartsWith("34") || IIN.StartsWith("37"))
                {
                    CardType = eCardType.AmericanExpress;
                }
                else if (IIN.StartsWith("4"))
                {
                    if (IIN.StartsWith("4026") || IIN.StartsWith("417500") || IIN.StartsWith("4405") || IIN.StartsWith("4508") ||
                        IIN.StartsWith("4844") || IIN.StartsWith("4913") || IIN.StartsWith("4917"))
                    {
                        CardType = eCardType.VisaElectron;
                    }
                    else
                    {
                        CardType = eCardType.Visa;
                    }
                }
                else if (IIN.StartsWith("51") || IIN.StartsWith("52") || IIN.StartsWith("53") || IIN.StartsWith("54") ||
                         IIN.StartsWith("55"))
                {
                    CardType = eCardType.MasterCard;
                }
                else if (IIN.StartsWith("6011") || IIN.StartsWith("644") || IIN.StartsWith("65"))
                {
                    CardType = eCardType.Discover;
                }
                else if (IIN.StartsWith("36") || IIN.StartsWith("38") || IIN.StartsWith("39"))
                {
                    CardType = eCardType.DinersClubInternational;
                }
                else if (IIN.StartsWith("3"))
                {
                    var jcbCheck = Convert.ToInt32(IIN.Substring(0, 4));
                    if (jcbCheck >= 3528 && jcbCheck <= 3589)
                    {
                        CardType = eCardType.JCB;
                    }
                    else
                    {
                        var dinersCheck = Convert.ToInt32(IIN.Substring(0, 3));
                        if (dinersCheck < 306)
                        {
                            CardType = eCardType.DinersClubCarteBlanche;
                        }
                        else if (dinersCheck == 309)
                        {
                            CardType = eCardType.DinersClubInternational;
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }
                else
                {
                    return(false);
                }

                //// 1.	Starting with the check digit double the value of every other digit
                //// 2.	If doubling of a number results in a two digits number, add up
                ///   the digits to get a single digit number. This will results in eight single digit numbers
                //// 3. Get the sum of the digits
                int sumOfDigits = CardNumber.Where((e) => e >= '0' && e <= '9')
                                  .Reverse()
                                  .Select((e, i) => ((int)e - 48) * (i % 2 == 0 ? 1 : 2))
                                  .Sum((e) => e / 10 + e % 10);


                //// If the final sum is divisible by 10, then the credit card number
                //   is valid. If it is not divisible by 10, the number is invalid.
                return(sumOfDigits % 10 == 0);
            }
            catch
            {
                return(false);
            }
        }