Date的CompareValidator [英] CompareValidator for Date
问题描述
我想使用比较验证器比较两个日期(两个文本框).
我使用的代码是:
Hi,
I want to compare two dates (two textboxes) using compare validator.
I use the code:
<asp:comparevalidator id="valCmpDate" runat="server" controltocompare="txtExpiryDate" controltovalidate="txtDateOfIssue" ErrorMessage='Please enter a date lessthan exipry date' Operator='LessThan' Type='Date' />
但是它只检查日期部分,而不检查月份和年份.
例如:
如果txtExpiryDate = 2010-06-23和txtDateOfIssue = 2009-06-25
我得到的结果txtDateOfIssue大于txtExpiryDate
请帮助
在此先感谢....:)
But it checks only the date portion, not month and year.
For example:
If txtExpiryDate=2010-06-23 and txtDateOfIssue=2009-06-25
I got the result txtDateOfIssue is greaterthan txtExpiryDate
Please Help
Thanks in advance.... :)
推荐答案
您可以使用自定义验证器代替比较验证器.
hi u can use custom validator instead of compare validator.
<div>
expiry date: <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
issue Date :<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cus" runat="server" ErrorMessage="Please enter a date lessthan exipry date" OnServerValidate="validateExpiryDate" ></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
日期格式应为:mm/dd/yyyy或mm-dd-yyyy
前任. 2010年6月25日或2010年6月25日
后面的代码是:
受保护的void Button1_Click(对象发送者,EventArgs e)
{
}
受保护的void validateExpiryDate(对象源,ServerValidateEventArgs args)
{
DateTime发布;
DateTime到期日期;
DateTime.TryParseExact(TextBox1.Text.Trim(),新字符串[] {"MM/dd/yyyy","MM-dd-yyyy"},null,System.Globalization.DateTimeStyles.AllowWhiteSpaces,过期时间);
DateTime.TryParseExact(TextBox2.Text.Trim(),新字符串[] {"MM/dd/yyyy","MM-dd-yyyy"},null,System.Globalization.DateTimeStyles.AllowWhiteSpaces,已发布);
args.IsValid =已发布<到期日;
}
the date format should be : mm/dd/yyyy or mm-dd-yyyy
ex. 06/25/2010 or 06-25-2010
the code behind is :
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void validateExpiryDate(object source, ServerValidateEventArgs args)
{
DateTime issuedate;
DateTime expirydate;
DateTime.TryParseExact(TextBox1.Text.Trim(), new string[] { "MM/dd/yyyy", "MM-dd-yyyy" }, null, System.Globalization.DateTimeStyles.AllowWhiteSpaces, out expirydate);
DateTime.TryParseExact(TextBox2.Text.Trim(), new string[] { "MM/dd/yyyy", "MM-dd-yyyy" }, null, System.Globalization.DateTimeStyles.AllowWhiteSpaces, out issuedate);
args.IsValid = issuedate < expirydate;
}
这篇关于Date的CompareValidator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!