信用卡信息验证

  要验证的信息:credit card number, expiration date, and CVV numbers.

  假设只接收这四种信用卡: Visa, MasterCard, Discover Card, and American Express.

  信用卡长度:16位(有13位的已经不用了),American Express 15位。

  American Express:以“34”或“37”开头。

  Visa:以“4”开头。

  MasterCard:以“51-54”开头。

  Discover Card:以“6011”开头。

  CVV:由卡号、有效期和服务约束代码生成的3位(Visa, MasterCard, and Discover Card)或4位数字(American Express)  

  信用卡号验证    

public bool ValidatingCreditCard(string credit_card_number)
                {
                    //获取首字母
                    int firstnumber = int.Parse(credit_card_number.Substring(0, 1));

                    bool result = true;

                    #region 位数验证
                    switch (firstnumber)
                    {
                        case 3:
                            if (!Regex.Match(credit_card_number, @"^3\d{3}[ \-]?\d{6}[ \-]?\d{5}$").Success)
                                result = false;                               
                            break;
                        case 4:
                            if (!Regex.Match(credit_card_number, @"^4\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$").Success)
                                result = false;
                            break;
                        case 5:
                            if (!Regex.Match(credit_card_number, @"^5\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$").Success)
                                result = false;
                            break;
                        case 6:
                            if (!Regex.Match(credit_card_number, @"^6011[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$").Success)
                                result = false;
                            break;

                        default: result = false; break;
                    }
                    #endregion


                    if (!result)
                        return result;

                    #region Luhn Algorithm
                    credit_card_number = credit_card_number.Replace("-", "");
                    int sum = 0;

                    for (int i = 0; i < credit_card_number.Length; i++)
                    {
                        int n = int.Parse(credit_card_number[i].ToString());
                        
                        if (i % 2 == 0 && i != credit_card_number.Length - 1)
                            sum += n * 2;
                        else
                            sum += n;
                    }

                    if (sum % 10 != 0)
                    {
                        result = false; 
                    }
                    #endregion

                    return result;

                }

  信用卡日期验证

      public bool ValidatingExpirationDate(int month, int year)
                {
                    bool result = true;

                    if (!Regex.Match(month.ToString(), @"^\d{1,2}$").Success)
                        result = false;
                    else if (!Regex.Match(year.ToString(), @"^\d{4}$").Success)
                        result = false;

                    else if (year < DateTime.Now.Year)
                        result = false;
                    else if (month < DateTime.Now.Month && year == DateTime.Now.Year)
                        result = false;
                    return result;
                }

  信用卡CCV验证

    

      public bool ValidatingCVV(string credit_card_number,string cvv)
                {
                    bool result = true;

                    //获取首字母
                    int firstnumber = int.Parse(credit_card_number.Substring(0, 1));

                    if (firstnumber == 3)
                    {
                        if (!Regex.Match(cvv, @"^\d{4}$").Success)
                            result = false;
                    }
                    else if (!Regex.Match(cvv, @"^\d{3}$").Success)
                        result = false;

                    return result;
                }

 

posted @ 2013-05-29 12:22  小飞的DD  阅读(656)  评论(0编辑  收藏  举报