[英]C# Validating User Input like a Credit Card number
This is for an assignment. 这是一项任务。 I need to create a program for a sandwich shop. 我需要为三明治店创建一个程序。 Part of it is to validate a user's payment info. 其中一部分是验证用户的付款信息。 The guidelines for this assignment are 此作业的准则是
Credit card number must be 16 digits; 信用卡号必须为16位数字; the first 4 digits must be one of these: 1298, 1267, 4512, 4567, 8901, 8933 前4位必须是以下其中一个:1298、1267、4512、4567、8901、8933
Expiration date must be 2 digits for the month, 4 digits for the year. 到期日期必须是月份的2位数字,年份的4位数字。 The month must be 01 - 12. The year must begin with "20". 月份必须为01-12。年份必须以“ 20”开头。 The date must be greater than the current actual month and year, and less than 6 years from the current actual month and year. 日期必须大于当前的实际月份和年份,并且距当前的实际月份和年份小于6年。
I have a text box for each number, txtCardNumber, txtSecuritycode, and txtExpiration. 每个数字,txtCardNumber,txtSecuritycode和txtExpiration都有一个文本框。 What I have works, but I think it's kind of messy. 我所拥有的有效,但我认为这有点混乱。 What would be a better way? 有什么更好的方法? And would it be possible to have only one Validate()
method instead of a separate one for each? 并且有可能只有一个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
Note: I haven't worked on validating the six year requirement. 注意:我尚未验证六年的要求。
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; <1>信用卡号必须为16位数字; the first 4 digits must be one of these: 1298, 1267, 4512, 4567, 8901, 8933 前4位必须是以下其中一个:1298、1267、4512、4567、8901、8933
<2> Security code must be 3 digit numeric (assumed) <2>密码必须是3位数字(假定)
<3> Expiration date must be 2 digits for the month, <4> 4 digits for the year. <3>到期日期必须是月份的2位数字,<4>是年份的4位数字。 <5> The month must be 01 - 12. <6> The year must begin with "20". <5>月份必须为01-12。<6>年份必须以“ 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. <7>日期必须大于当前实际的月份和年份,并且<8>必须小于当前实际的月份和年份的6年。
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. 尽管这在所有条件下都能正常工作,但在生产中使用前请先进行正确检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.