Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to validate from date should not be greater than to date.
plz help me to do it.
Posted

You can use following javascript function :

JavaScript
function CompareDate()
{
    var From = document.getElementById("TextBoxofFrom").value;
    var To = document.getElementById("TextBoxofTo").value;
    var strSplitFrom = From.split('/')
    var myDateFrom = new Date();
    myDateFrom.setFullYear(strSplitFrom[2], strSplitFrom[1] - 1, strSplitFrom[0]);
    var strSplitTo = To.split('/')
    var myDateTo = new Date();
    myDateTo.setFullYear(strSplitTo[2], strSplitTo[1] - 1, strSplitTo[0]);
    if (myDateFrom > myDateTo) {
        alert("Start date can not greater than End date.");        
        return false;
    }
    else {
        return true;
    }
}
 
Share this answer
 
<asp:textbox id="TextBox1" runat="server" xmlns:asp="#unknown"></asp:textbox>
    <asp:calendar id="cal1" runat="server" backcolor="White" bordercolor="Black" xmlns:asp="#unknown">
        BorderStyle="Solid" CellSpacing="1" Font-Names="Verdana" Font-Size="9pt" 
        ForeColor="Black" Height="250px" NextPrevFormat="ShortMonth" 
        onselectionchanged="Calendar1_SelectionChanged" Width="330px">
        <dayheaderstyle font-bold="True" font-size="8pt" forecolor="#333333">
            Height="8pt" />
        <daystyle backcolor="#CCCCCC" />
        <nextprevstyle font-bold="True" font-size="8pt" forecolor="White" />
        <othermonthdaystyle forecolor="#999999" />
        <selecteddaystyle backcolor="#333399" forecolor="White" />
        <titlestyle backcolor="#333399" borderstyle="Solid" font-bold="True">
            Font-Size="12pt" ForeColor="White" Height="12pt" />
        <todaydaystyle backcolor="#999999" forecolor="White" />
    </titlestyle></dayheaderstyle></asp:calendar>
    <asp:textbox id="TextBox2" runat="server" xmlns:asp="#unknown"></asp:textbox>
    <br />
    <asp:calendar id="cal2" runat="server" backcolor="White" bordercolor="Black" xmlns:asp="#unknown">
        BorderStyle="Solid" CellSpacing="1" Font-Names="Verdana" Font-Size="9pt" 
        ForeColor="Black" Height="250px" NextPrevFormat="ShortMonth" 
        onselectionchanged="Calendar2_SelectionChanged" Width="330px">
        <dayheaderstyle font-bold="True" font-size="8pt" forecolor="#333333">
            Height="8pt" />
        <daystyle backcolor="#CCCCCC" />
        <nextprevstyle font-bold="True" font-size="8pt" forecolor="White" />
        <othermonthdaystyle forecolor="#999999" />
        <selecteddaystyle backcolor="#333399" forecolor="White" />
        <titlestyle backcolor="#333399" borderstyle="Solid" font-bold="True">
            Font-Size="12pt" ForeColor="White" Height="12pt" />
        <todaydaystyle backcolor="#999999" forecolor="White" />  </titlestyle></dayheaderstyle></asp:calendar>



protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
DateTime t1 = cal1.SelectedDate;
TextBox1.Text = t1.ToString("dd/MM/yyyy");
}

protected void Calendar2_SelectionChanged(object sender, EventArgs e)
{
Calendar c = new Calendar();
c.SelectedDate = cal1.SelectedDate;
DateTime t2 =cal2.SelectedDate;
if (c.SelectedDate > t2)
{
Page.ClientScript.RegisterStartupScript(GetType(), "popup", "alert('Invalid Date..Please Select date after"+c.SelectedDate+"');", true);
}
else
{
TextBox2.Text = t2.ToString("dd/MM/yyyy");
}
}
 
Share this answer
 

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