我有一个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;
}
答案 0 :(得分:6)
尝试
txtCardExpiry.Text.Compare(DateTime.Now.ToString("MMyyyy"))
Custom Date and Time Format Strings 必须说,MSDN非常有帮助。