Click here to Skip to main content
15,904,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,i am new to javascript, my problem is i have a simple date check JS function as follow
JavaScript
function CompareDates(str1, str2)
   {
          var dt1 = parseInt(str1.substring(0, 2), 10);
          var mon1 = parseInt(str1.substring(3, 5), 10);
          var yr1 = parseInt(str1.substring(6, 10), 10);
          var dt2 = parseInt(str2.substring(0, 2), 10);
          var mon2 = parseInt(str2.substring(3, 5), 10);
          var yr2 = parseInt(str2.substring(6, 10), 10);
          var date1 = new Date(yr1, mon1, dt1);
          var date2 = new Date(yr2, mon2, dt2);

          if (date2 < date1) {
                alert("To date cannot be greater than from date");
                return false;
          }
          else
          {
                return true;
          }

      }

In Gridview
JavaScript
<asp:TemplateField HeaderText="Start Dtae">
                           <ItemTemplate>
                                        <asp:TextBox ID="txtStartDate" runat="server" Text='<%# Bind("StartDate") %>'></asp:TextBox>
                                        <asp:CalendarExtender ID="txtStartDate_CalendarExtender" runat="server" Format="dd/MM/yyyy"                 Enabled="True" TargetControlID="txtStartDate"></asp:CalendarExtender>
                            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="End Dtae">
                           <ItemTemplate>
                                        <asp:TextBox ID="txtEndDate" runat="server" Text='<%# Bind("EndDate") %>' **onchange="CompareDates(txtStartDate.Text,this.Text)**;" ></asp:TextBox>
                                        <asp:CalendarExtender ID="txtEndDate_CalendarExtender" runat="server" Format="dd/MM/yyyy" Enabled="True" TargetControlID="txtEndDate">**strong text**</asp:CalendarExtender>
                           </ItemTemplate>
         </asp:TemplateField>

grid is dynamic and user can add any number of rows to it. my need is to check the date without looping the entire gridview rows on submit button click. onchange of the txtenddate i want to pass the values of both text boxes..

can anybody help me..

Thank You..
Posted
Updated 17-Sep-12 21:30pm
v2

1 solution

Hi
I think it will be better to pass the id of both control to your method and then get the values from that and continue with your validation code.

for example....
modify the js method as...
function CompareDates(ctrlStartID, ctrlEndID)
{
    var str1 = document.getElementByID(ctrlStartID);
    var str2 = document.getElementByID(ctrlEndID);
    // continue with your previous code.
}


and instead of assigning the onchange attribute on html, use the "rowdatabound" event of grid view and the both the control there and assign the onchange attribute and pass the client id of both the controls there.

like....

C#
protected void yourgrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TextBox txtStart = (TextBox)e.Row.FindControl("txtStartDate");
        TextBox txtEnd = (TextBox)e.Row.FindControl("txtEndDate");
        txtStart.Attribute["onchange"] = "CompareDates('"+ txtStart.ClientID +"','"+ txtEnd.ClientID +"')";
txtEnd.Attribute["onchange"] = "CompareDates('"+ txtStart.ClientID +"','"+ txtEnd.ClientID +"')";
    }
}


I will also prefer to use "onblur" event instead of "onchange"

Hope this will help you.
 
Share this answer
 
Comments
anish.karunakaran 19-Sep-12 4:54am    
@TanvirRaihan,
i did as you suggested but nothing happens.. function is invoked there after no response.

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