Click here to Skip to main content
16,023,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to compare two dates (two textboxes) using compare validator.
I use the code:

XML
<asp:comparevalidator id="valCmpDate" runat="server" controltocompare="txtExpiryDate" controltovalidate="txtDateOfIssue" ErrorMessage='Please enter a date lessthan exipry date' Operator='LessThan' Type='Date' />


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.... :)
Posted
Updated 25-Jun-10 3:25am
v4

1 solution

hi u can use custom validator instead of compare validator.

XML
<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>


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;
}
 
Share this answer
 
Comments
rohinideepoo 28-Jun-10 2:00am    
Thank you....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900