我有一个 TextBox 和 Extender AjaxCalander。选择日期时,它的工作正常,仅在 TextBox 中显示月份和年份。我的代码如下
<asp:TextBox ID="txtCardExpiry" runat="server" Width="200px" /><span> MMYY</span>
<ajaxToolkit:CalendarExtender runat="server" Format="MMyy" TargetControlID="txtCardExpiry" />
我想验证信用卡是否未过期
现在我如何txtCardExpiry.Text
与今天的月份和年份进行比较
现在我正在使用
int dtNowYear = Convert.ToInt16(DateTime.Now.Year.ToString().Substring(2, 2));
int dtNowMonth = Convert.ToInt16(DateTime.Now.Month.ToString());
int dtExpYear = Convert.ToInt16(txtCardExpiry.Text.Substring(2, 2));
int dtExpMonth = Convert.ToInt16(txtCardExpiry.Text.Substring(0, 2));
if (dtNowYear == dtExpYear && dtNowMonth > dtExpMonth)
{
lblMessage.Attributes.Add("class", "lblMessageError");
lblMessage.Text =
"Credit Card expiry month and year cannot be less than todays month and year or your Credit Card has expired";
return false;
}
else if (dtNowYear > dtExpYear)
{
if (dtNowMonth > dtExpMonth)
{
lblMessage.Attributes.Add("class", "lblMessageError");
lblMessage.Text =
"Credit Card expiry month and year cannot be less than todays month and year or your Credit Card has expired";
return false;
}
return false;
}