C#验证用户输入,如信用卡号

时间:2015-10-05 23:07:50

标签: c# regex validation

这是作业。我需要为三明治店创建一个程序。部分原因是验证用户的付款信息。这项任务的指导原则是

  

信用卡号必须是16位数;前4位必须是以下之一:                  1298,1267,4512,4567,8901,8933

     

到期日期必须是月份的2位数,年份的4位数。月份必须是01 - 12.年份必须以" 20"开头。日期必须大于当前的实际月份和年份,并且距当前实际月份和年份不到6年。

我有一个文本框,每个数字,txtCardNumber,txtSecuritycode和txtExpiration。我有什么作品,但我认为它有点混乱。什么是更好的方式?是否可以只有一个Validate()方法而不是每个方法单独一个?

public bool IsValidCard()
{
    if (txtCardNumber.Text.StartsWith("1298") ||
        txtCardNumber.Text.StartsWith("1267") ||
        txtCardNumber.Text.StartsWith("4512") ||
        txtCardNumber.Text.StartsWith("4567") ||
        txtCardNumber.Text.StartsWith("8901") ||
        txtCardNumber.Text.StartsWith("8933"))
    {
        if (Regex.Replace(txtCardNumber.Text, @"\s+", "").Length == 16)
        {
            return true;
        }
    }
    return false;
}//end IsValidCard()

public bool IsValidSecurityCode()
{
    bool isValid = Regex.Match(txtSecurityCode.Text, @"^\d{3}$").Success;
    return isValid;
}//end IsValidSecurityCode()

public bool IsValidExpiration()
{
    string[] date = Regex.Split(txtExpiration.Text, "/");
    string[] currentDate = Regex.Split(DateTime.Now.ToString("MM/yyyy"), "/");
    int compareYears = string.Compare(date[1], currentDate[1]);
    int compareMonths = string.Compare(date[0], currentDate[0]);

    //if expiration date is in MM/YYYY format
    if (Regex.Match(txtExpiration.Text, @"^\d{2}/\d{4}$").Success)
    {
        //if month is "01-12" and year starts with "20"
        if (Regex.Match(date[0], @"^[0][1-9]|[1][0-2]$").Success)
        {
            //if expiration date is after current date
            if ((compareYears == 1) || (compareYears == 0 && (compareMonths == 1)))
            {
                return true;
            }
        }
    }
    return false;
}//end IsValidExpiration

注意:我还没有确认六年的要求。

1 个答案:

答案 0 :(得分:3)

You can use the following method, which will validate all the credit card info as per your requirements.

<1> Credit card number must be 16 digits; the first 4 digits must be one of these: 1298, 1267, 4512, 4567, 8901, 8933

<2> Security code must be 3 digit numeric (assumed)

<3> Expiration date must be 2 digits for the month, <4> 4 digits for the year. <5> The month must be 01 - 12. <6> The year must begin with "20". <7> The date must be greater than the current actual month and year, and <8> less than 6 years from the current actual month and year.

public static bool IsCreditCardInfoValid(string cardNo, string expiryDate, string cvv)
{
    var cardCheck = new Regex(@"^(1298|1267|4512|4567|8901|8933)([\-\s]?[0-9]{4}){3}$");
    var monthCheck = new Regex(@"^(0[1-9]|1[0-2])$");
    var yearCheck = new Regex(@"^20[0-9]{2}$");
    var cvvCheck = new Regex(@"^\d{3}$");

    if (!cardCheck.IsMatch(cardNo)) // <1>check card number is valid
        return false;
    if (!cvvCheck.IsMatch(cvv)) // <2>check cvv is valid as "999"
        return false;

    var dateParts = expiryDate.Split('/'); //expiry date in from MM/yyyy            
    if (!monthCheck.IsMatch(dateParts[0]) || !yearCheck.IsMatch(dateParts[1])) // <3 - 6>
        return false; // ^ check date format is valid as "MM/yyyy"

    var year = int.Parse(dateParts[1]);
    var month = int.Parse(dateParts[0]);            
    var lastDateOfExpiryMonth = DateTime.DaysInMonth(year, month); //get actual expiry date
    var cardExpiry = new DateTime(year, month, lastDateOfExpiryMonth, 23, 59, 59);

    //check expiry greater than today & within next 6 years <7, 8>>
    return (cardExpiry > DateTime.Now && cardExpiry < DateTime.Now.AddYears(6));
}

Though this works fine for all the conditions, please check properly before using in production.