Click here to Skip to main content
15,889,838 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,
My requirement is simple.
* I have a grid in my page with 3 columns, From Time and To Time.
* When I click a button above the grid, I have got to open a modal popup window.
* In the popup, I have 4 dropdowns - From Hours, From Minutes, To Hours and To Minutes.
* Consider, I select 08(From Hours), 40(From Minutes) and 09(To Hours), 20(To Minutes.)
* I click the add button. The popup closes, and the above selected values are populated in the grid as

FromTime ToTime
08-40 09-20

* Now, when I want to enter another set of values, I have to validate as follows.
The new time should be more than the ones already present in the grid. (The 2nd set of data can start with 09-20 to .. though.)
* Example, if I choose times 09-00 to 09-30, I should get an alert and the popup should stay as it is. When I enter correct values(say 09-20 to 10-00) the popup should close and the values should be added to the grid.

Can someone provide me a solution for this? It is urgent, my job is at stake ..

P.S. My code, which doesn't work the way I want it to.

JavaScript
function validateTime() {
            var table = document.getElementById('<%=gvTimeSlots.ClientID%>');
            var Row;
            var oldFromTime, oldToTime;
            var oldFT, oldTT;
            var oldFromHour, oldFromMin, oldToHour, oldToMin;
            var newFromHour, newFromMin, newToHour, newToMin;
            var newFromTime, newToTime;
                for (var i = 1; i < table.rows.length; i++) {
                Row = table.rows[i];
                oldFT = new Array();
                oldTT = new Array();
                oldFromTime = Row.cells[4].innerText;
                oldToTime = Row.cells[5].innerText;
                oldFT = oldFromTime.split("-");
                oldTT = oldToTime.split("-");
                oldFromHour = oldFT[0];
                oldFromMin = oldFT[1];
                oldToHour = oldTT[0];
                oldToMin = oldTT[1];
                oldFromTime = parseInt(oldFromHour * 60) + parseInt(oldFromMin);
                oldToTime = parseInt(oldToHour * 60) + parseInt(oldToMin);
                if(('<%= cboFromTimeHours.SelectedValue%>') >= 0 && (('<%= cboFromTimeHours.SelectedValue%>')) < 10){
                newFromHour = "0" + ('<%= cboFromTimeHours.SelectedValue%>');
                }
                else{
                newFromHour = ('<%= cboFromTimeHours.SelectedValue%>');
                }
                newFromMin = ('<%= cboFromTimeMins.SelectedValue%>');
                newFromTime = parseInt(newFromHour * 60) + parseInt(newFromMin);
                if (('<%= cboToTimeHours.SelectedValue%>') >= 0 && (('<%= cboToTimeHours.SelectedValue%>')) < 10) {
                newToHour = "0" + ('<%= cboToTimeHours.SelectedValue%>');
                }
                else {
                newToHour = ('<%= cboToTimeHours.SelectedValue%>');
                }
                newToMin = ('<%= cboToTimeMins.SelectedValue%>');
                newToTime = parseInt(newToHour * 60) + parseInt(newToMin);
                if (newFromHour < oldFromHour || newFromHour < oldToHour){
                    alert("do not add.");
                    return false;
                }
            }
        }
Posted

If clicking the button simply adds a new row, then why do you have to loop through the whole table every time to check all the values? Surely you can just store the last value entered in a global variable and check against that value every time a new one's entered.

Like:

JavaScript
var oldToTime = 0;

function validateTime() {
    var NFH = document.getElementById('<%=cboFromTimeHours.ClientID%>');
    var NFM = document.getElementById('<%=cboFromTimeMins.ClientID%>');
    var NTH = document.getElementById('<%=cboToTimeHours.ClientID%>');
    var NTM = document.getElementById('<%=cboToTimeMins.ClientID%>');

    var newFromHour, newFromMin, newToHour, newToMin, newFromTime, newToTime;

    // now get the new 'from' time
    if ((NFH.options[NFH.selectedIndex].text >= 0) && (NFH.options[NFH.selectedIndex].text) < 10) {
        newFromHour = "0" + NFH.options[NFH.selectedIndex].text;
    }
    else {
        newFromHour = NFH.options[NFH.selectedIndex].text;
    }

    // and 'from' minutes
    newFromMin = NFM.options[NFM.selectedIndex].text;

    // combine to get minutes past midnight
    newFromTime = parseInt(newFromHour * 60) + parseInt(newFromMin);


    // now get the new 'to' time
    if ((NTH.options[NTH.selectedIndex].text >= 0) && (NTH.options[NTH.selectedIndex].text) < 10) {
        newToHour = "0" + NTH.options[NTH.selectedIndex].text;
    }
    else {
        newToHour = NTH.options[NTH.selectedIndex].text;
    }

    // and 'to' minutes
    newToMin = NTM.options[NTM.selectedIndex].text;

    // combine to get minutes past midnight
    newToTime = parseInt(newToHour * 60) + parseInt(newToMin);

    alert('new from:' + newFromTime + ', new to:' + newToTime + ', old:' + oldToTime);

    // check new times are valid
    if (!((newToTime > newFromTime) && (newFromTime >= oldToTime))) {
        alert("do not add.");
        return false;
    }
    else {
        alert('ok, time is valid');
        oldToTime = newToTime;
        return true;
    }
}


Form code:
ASP.NET
<form runat="server">

    From H:<asp:DropDownList ID="cboFromTimeHours" DataValueField="TIME_ID" DataTextField="TIME_DESC" runat="server"></asp:DropDownList>
    <br />
    From M:<asp:DropDownList ID="cboFromTimeMins" DataValueField="TIME_ID" DataTextField="TIME_DESC" runat="server"></asp:DropDownList>
    <br />
    To H:<asp:DropDownList ID="cboToTimeHours" DataValueField="TIME_ID" DataTextField="TIME_DESC" runat="server"></asp:DropDownList>
    <br />
    To M:<asp:DropDownList ID="cboToTimeMins" DataValueField="TIME_ID" DataTextField="TIME_DESC" runat="server"></asp:DropDownList>
    <br />

    <asp:Button OnClientClick="validateTime(); return false;" Text="Click" runat="server"/>

</form>


Code behind:
C#
protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt;

    DataRow unSelected;

    dt = new DataTable();

    dt.Columns.Add("TIME_ID", typeof(String));
    dt.Columns.Add("TIME_DESC", typeof(String));

    for (int i = 1; i < 13; i++)
    {
        unSelected = dt.NewRow();
        unSelected["TIME_ID"] = i;
        unSelected["TIME_DESC"] = i;

        dt.Rows.InsertAt(unSelected, dt.Rows.Count);
    }

    cboFromTimeHours.DataSource = dt;
    cboFromTimeHours.DataBind();

    cboToTimeHours.DataSource = dt;
    cboToTimeHours.DataBind();

    dt = new DataTable();

    dt.Columns.Add("TIME_ID", typeof(String));
    dt.Columns.Add("TIME_DESC", typeof(String));

    for (int i = 0; i < 60; i++)
    {
        unSelected = dt.NewRow();
        unSelected["TIME_ID"] = i;
        unSelected["TIME_DESC"] = i;

        dt.Rows.InsertAt(unSelected, dt.Rows.Count);
    }

    cboFromTimeMins.DataSource = dt;
    cboFromTimeMins.DataBind();

    cboToTimeMins.DataSource = dt;
    cboToTimeMins.DataBind();
}
 
Share this answer
 
v6
Comments
Samresh.ss 12-Apr-13 8:31am    
Seems the best way to achieve it.
Aish P Karthi 12-Apr-13 8:36am    
Hi Nick Fisher..tried ur code .. But the function isn't getting called..strange.. any idea about it?
Nick Fisher (Consultant) 12-Apr-13 9:33am    
I didn't have time to syntax check it before, but I've made a couple of small changes now and it seems fine. You don't even need to reference the table now, as the only thing that matters is the last time value you entered. This code works fine for me - if I enter 9:10 to 9:20, the 'newFromTime' is 550 and 'newToTime' is 560 minutes and validates correctly. If I then enter 9:15 to 9:20, it shows the alert and returns false, but if I enter 9:20 - 9:25 it validates and returns true. That should be all you need.

Nick
Are you really sure you want to use the server value '<%= cboFromTimeHours.SelectedValue%>' here? Don't you want the actual value selected?

Good luck!
 
Share this answer
 
Hi Nijboer, it would be good that way too..! But somehow I am not able to get the validation right. It is a total failure.
 
Share this answer
 
All right, I have solved this at last. Here goes my code.

XML
function validateTime(source,arguments) {
            var fnSuccess = 0;
            var table = document.getElementById('<%= gvTimeSlots.ClientID%>');
            var gvRowCount = table.rows.length;
            var Row;
            var oldFromTime, oldToTime;
            var oldFT, oldTT;
            var oldFromHour, oldFromMin, oldToHour, oldToMin;
            var newFromHour, newFromMin, newToHour, newToMin;
            var newFromTime, newToTime;
            for (var i = 1; i < table.rows.length; i++) {
            //var i = table.rows.length - 1;
                Row = table.rows[i];
                oldFT = new Array();
                oldTT = new Array();
                oldFromTime = Row.cells[4].innerText;
                oldToTime = Row.cells[5].innerText;
                oldFT = oldFromTime.split("-");
                oldTT = oldToTime.split("-");
                oldFromHour = oldFT[0];
                oldFromMin = oldFT[1];
                oldToHour = oldTT[0];
                oldToMin = oldTT[1];

                oldFromTime = parseInt(oldFromHour * 60) + parseInt(oldFromMin);
                oldToTime = parseInt(oldToHour * 60) + parseInt(oldToMin);

                newFromHour = document.getElementById('<%= cboFromTimeHours.ClientID%>');
                newFromMin = document.getElementById('<%= cboFromTimeMins.ClientID%>');
                //if (newFromHour.value > 12) {
                //    newFromHour=((parseInt(newFromHour.value)) * 60) + parseInt(newFromMin.value);
                //    alert(newFromHour);
                //}

                newFromTime = parseInt(newFromHour.value * 60) + parseInt(newFromMin.value);

                newToHour = document.getElementById('<%= cboToTimeHours.ClientID%>');
                newToMin = document.getElementById('<%= cboToTimeMins.ClientID%>');
                newToTime = parseInt(newToHour.value * 60) + parseInt(newToMin.value);


                //if ((oldFromTime <= newFromTime) && (oldToTime >= newFromTime) && ((oldToTime <= newToTime) && (oldFromTime >= newToTime))) {
                if ((oldFromTime >= newFromTime) && (oldToTime > newFromTime)) {
                    arguments.IsValid = false;

                }
                else {
                    arguments.IsValid = true;

                }
            }


        }

I had a custom validator to avoid postback on unsuucessful validation.
 
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