locked
How to validate shift timing from time and to time RRS feed

  • Question

  • User-2028591720 posted

    I am looking for a solutions to validate the shift timing in jquery

    Condition 1: Shift start time should be less than of end time

    Condition 2: Shift break times such as break1(start and end time), break2(start and end time), break3(start and end time).

    Condition 3: Break1 start time should be less than of shift end time

    Condition 4: Break2 start time should be less than of break 1 end time

    Condition 5: Break3 start time should be less than of break 2 end time. Without break2 break3 timing will be considered as invalid.

    I made the jsfiddle example here: https://jsfiddle.net/nmtx4pa8/2/

    i made the logic...it was not working propertly, can you advice to solve the issue

     
    <head> 
        <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
    </head>
     
            <table border="1" style="border-collapse:collapse;" width="100%">
                <thead class="btn-primary">
                    <tr class="table-prima1ry">
                        <th>Day</th>
    
    
                        <th>Start Time</th>
    
                        <th>End Time</th>
    
                        <th colspan="2" class="text-center">Break 1</th>
    
                        <th colspan="2" class="text-center">Break 2</th>
    
                        <th colspan="2" class="text-center">Break 3</th>
    
                        <th>Total Shift Hrs</th>
                        <th>Total Shift Excl Break</th>
    
                    </tr>
                </thead>
                <tbody>
    
                    <tr dayno="0">
                        <td class="shift-day-name">
                            <span>
                                Sunday
                            </span>
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time shift_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time shift_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break1_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break1_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break2_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break2_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break3_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break3_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" data-accept-number="true" class="form-control validate-time" maxlength="5" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" data-accept-number="true" class="form-control validate-time" maxlength="5" />
                        </td>
                    </tr>
                    <tr dayno="1">
                        <td class="shift-day-name">
    
                            <span>
                                Monday
                            </span>
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time shift_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time shift_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break1_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break1_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break2_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break2_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break3_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break3_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" data-accept-number="true" class="form-control validate-time" maxlength="5" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" data-accept-number="true" class="form-control validate-time" maxlength="5" />
                        </td>
                    </tr>
                    <tr dayno="2">
                        <td class="shift-day-name">
    
                            <span>
                                Tuesday
                            </span>
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time shift_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time shift_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break1_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break1_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break2_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break2_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break3_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break3_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" data-accept-number="true" class="form-control validate-time" maxlength="5" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" data-accept-number="true" class="form-control validate-time" maxlength="5" />
                        </td>
                    </tr>
                    <tr dayno="3">
                        <td class="shift-day-name">
    
                            <span>
                                Wednesday
                            </span>
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time shift_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time shift_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break1_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break1_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break2_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break2_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break3_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break3_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" data-accept-number="true" class="form-control validate-time" maxlength="5" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" data-accept-number="true" class="form-control validate-time" maxlength="5" />
                        </td>
                    </tr>
                    <tr dayno="4">
                        <td class="shift-day-name">
    
                            <span>
                                Thursday
                            </span>
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time shift_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time shift_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break1_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break1_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break2_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break2_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break3_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break3_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" data-accept-number="true" class="form-control validate-time" maxlength="5" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" data-accept-number="true" class="form-control validate-time" maxlength="5" />
                        </td>
                    </tr>
                    <tr dayno="5">
                        <td class="shift-day-name">
    
                            <span>
                                Friday
                            </span>
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time shift_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time shift_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break1_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break1_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break2_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break2_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break3_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break3_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" data-accept-number="true" class="form-control validate-time" maxlength="5" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" data-accept-number="true" class="form-control validate-time" maxlength="5" />
                        </td>
                    </tr>
                    <tr dayno="6">
                        <td class="shift-day-name">
    
                            <span>
                                Saturday
                            </span>
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time shift_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time shift_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break1_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break1_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break2_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break2_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break3_starttime" data-validate-time="true" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" class="form-control validate-time break3_endtime" data-validate-time="true" />
                        </td>
    
                        <td class="form-group mb-0">
                            <input placeholder="00:00" data-accept-number="true" class="form-control validate-time" maxlength="5" />
                        </td>
                        <td class="form-group mb-0">
                            <input placeholder="00:00" data-accept-number="true" class="form-control validate-time" maxlength="5" />
                        </td>
                    </tr>
                </tbody>
            </table>
            <hr />
            <button id="btnvalidateshifttiming" name="btnvalidateshifttiming">Validate time</button>
     
    <style type="text/css">
        .validate-time-invalid {
            border: 2px solid #f46a6a;
        }
    
        .in_validate_break_time {
            border: 2px solid yellow !important;
        }
    </style>
    <script type="text/javascript">
        $(function () {
            shift.init();
        });
        let shift = {
            init: function () { 
                shift.commonevent();
            },
            commonevent: function () {
    
                //validatetime
                $("[data-validate-time=true]").focusout(function (event) {
                    let current = $(this);
                    let value = current.val();
                    let isValid = shift.isValidate24HourTime(value);
                    current.removeClass("validate-time-invalid");
                    if (isValid == false) {
                        current.val("");
                        current.addClass("validate-time-invalid");
                    }
                    //console.log("The time is ", isValid, ", the value entered:", value);
                });
                  
                $("#btnvalidateshifttiming").click(function (e) {
                    e.preventDefault();                
                    let validateTime = shift.validateShiftDetails();
                });
            },
            validateStartAndEndTime: function (fromTimeElement, toTimeElement) {
    
                let timeStatus = false;
    
                //shift start and end time
                let startTimeEl = fromTimeElement;
                let endTimeEl = toTimeElement;
    
                startTimeEl.removeClass("validate-time-invalid");
                endTimeEl.removeClass("validate-time-invalid");
    
                //validate start time
                if (shift.isValidate24HourTime(startTimeEl.val()) == false) {
                    startTimeEl.addClass("validate-time-invalid");
                }
                //validate end time
                if (shift.isValidate24HourTime(endTimeEl.val()) == false) {
                    endTimeEl.addClass("validate-time-invalid");
                }
                //   console.log("Start time status:", shift.isValidate24HourTime(startTimeEl.val()), startTimeEl.val(), ", End time status:", endTimeEl.val(), shift.isValidate24HourTime(endTimeEl.val()));
                if (shift.isValidate24HourTime(startTimeEl.val()) == true && shift.isValidate24HourTime(endTimeEl.val()) == true) {
                    let startTimeV = new Date().setHours(shift.GetHours(startTimeEl.val()), shift.GetMinutes(startTimeEl.val()), 0);
                    let endTimeV = new Date(startTimeV).setHours(shift.GetHours(endTimeEl.val()), shift.GetMinutes(endTimeEl.val()), 0);
    
                    if (startTimeV > endTimeV) {
                        endTimeEl.addClass("validate-time-invalid");
                    }
                    else {
                        timeStatus = true;
                    }
                    //   console.log("start time:", startTimeV);
                    //  console.log("end time:", endTimeV);
                }
                return timeStatus;
            },
            ValidateBreak1Timing: function (shiftfromTimeElement, shifttoTimeElement, break1FromTimeElement, break1ToTimeElement) {
    
                let timeStatus = false;
    
                //shift start and end time
                let shiftStartTimeV = new Date().setHours(shift.GetHours(shiftfromTimeElement.val()), shift.GetMinutes(shiftfromTimeElement.val()), 0);
                let shiftEndTimeV = new Date(shiftStartTimeV).setHours(shift.GetHours(shifttoTimeElement.val()), shift.GetMinutes(shifttoTimeElement.val()), 0);
    
                let break1StartTimeV = new Date().setHours(shift.GetHours(break1FromTimeElement.val()), shift.GetMinutes(break1FromTimeElement.val()), 0);
                let break1EndTimeV = new Date(break1StartTimeV).setHours(shift.GetHours(break1ToTimeElement.val()), shift.GetMinutes(break1ToTimeElement.val()), 0);
    
                break1FromTimeElement.addClass("in_validate_break_time");
                break1ToTimeElement.addClass("in_validate_break_time");
    
                // console.log("---Validate Break1 Time ------------");
                /// console.log("Break1 Start time: ", break1FromTimeElement.val(), ":", break1StartTimeV, ", End:", break1ToTimeElement.val(), ":", break1EndTimeV);
    
                if (shiftStartTimeV < break1StartTimeV && shiftEndTimeV > break1EndTimeV) {
    
                    // console.log("You have a valid break 1 time");
    
                    break1FromTimeElement.removeClass("in_validate_break_time");
                    break1ToTimeElement.removeClass("in_validate_break_time");
    
                    timeStatus = true;
                }
                else {
                    // console.log("You dont have a valid break1 time");
                }
                // console.log("---End Validate Break 1 Time ------------");
                return timeStatus;
            },
    
            ValidateBreak2Timing: function (
                shiftfromTimeElement, shifttoTimeElement,
                break1FromTimeElement, break1ToTimeElement,
                break2FromTimeElement, break2ToTimeElement) {
    
                let timeStatus = false;
    
                //shift start and end time
                let shiftStartTimeV = new Date().setHours(shift.GetHours(shiftfromTimeElement.val()), shift.GetMinutes(shiftfromTimeElement.val()), 0);
                let shiftEndTimeV = new Date(shiftStartTimeV).setHours(shift.GetHours(shifttoTimeElement.val()), shift.GetMinutes(shifttoTimeElement.val()), 0);
    
                //break 1
                let break1StartTimeV = new Date().setHours(shift.GetHours(break1FromTimeElement.val()), shift.GetMinutes(break1FromTimeElement.val()), 0);
                let break1EndTimeV = new Date(break1StartTimeV).setHours(shift.GetHours(break1ToTimeElement.val()), shift.GetMinutes(break1ToTimeElement.val()), 0);
    
                break1FromTimeElement.addClass("in_validate_break_time");
                break1ToTimeElement.addClass("in_validate_break_time");
    
                //console.log("break2 end time id:", break2ToTimeElement);
                //break 2
                let break2StartTimeV = new Date().setHours(shift.GetHours(break2FromTimeElement.val()), shift.GetMinutes(break2FromTimeElement.val()), 0);
                let break2EndTimeV = new Date(break2StartTimeV).setHours(shift.GetHours(break2ToTimeElement.val()), shift.GetMinutes(break2ToTimeElement.val()), 0);
    
                break2FromTimeElement.addClass("in_validate_break_time");
                break2ToTimeElement.addClass("in_validate_break_time");
    
                // console.log("---Validate Break2 Time ------------");
                //  console.log("Shift Start time: ", shiftfromTimeElement.val(), ":", shiftStartTimeV, ", End:", shifttoTimeElement.val(), ":", break1EndTimeV);
                //  console.log("Break1 Start time: ", break1FromTimeElement.val(), ":", break1StartTimeV, ", End:", break1ToTimeElement.val(), ":", break1EndTimeV);
                //  console.log("Break2 Start time: ", break2FromTimeElement.val(), ":", break2StartTimeV, ", End:", break2ToTimeElement.val(), ":", break2EndTimeV);
    
                if (shiftStartTimeV < break1StartTimeV && shiftEndTimeV > break1EndTimeV) {
    
                    //validated break 1 successfully
                    break1FromTimeElement.removeClass("in_validate_break_time");
                    break1ToTimeElement.removeClass("in_validate_break_time");
                    //console.log("---------removed in valid class from break1---------------");
    
                    if (break1EndTimeV < break2StartTimeV && shiftEndTimeV > break2EndTimeV) {
    
                        //   console.log("You have a valid break 2 time");
    
                        break2FromTimeElement.removeClass("in_validate_break_time");
                        break2ToTimeElement.removeClass("in_validate_break_time");
    
                        timeStatus = true;
                    }
                    else {
                        //  console.log("You dont have a valid break2 time");
                    }
                }
                else {
                    // console.log("You dont have a valid break1 time");
                }
                // console.log("---End Validate Break 2 Time ------------", timeStatus);
                return timeStatus;
            },
    
            ValidateBreak3Timing: function (
                shiftfromTimeElement, shifttoTimeElement,
                break1FromTimeElement, break1ToTimeElement,
                break2FromTimeElement, break2ToTimeElement,
                break3FromTimeElement, break3ToTimeElement) {
    
                let timeStatus = false;
    
                //shift start and end time
                let shiftStartTimeV = new Date().setHours(shift.GetHours(shiftfromTimeElement.val()), shift.GetMinutes(shiftfromTimeElement.val()), 0);
                let shiftEndTimeV = new Date(shiftStartTimeV).setHours(shift.GetHours(shifttoTimeElement.val()), shift.GetMinutes(shifttoTimeElement.val()), 0);
    
                //break 1
                let break1StartTimeV = new Date().setHours(shift.GetHours(break1FromTimeElement.val()), shift.GetMinutes(break1FromTimeElement.val()), 0);
                let break1EndTimeV = new Date(break1StartTimeV).setHours(shift.GetHours(break1ToTimeElement.val()), shift.GetMinutes(break1ToTimeElement.val()), 0);
    
                break1FromTimeElement.addClass("in_validate_break_time");
                break1ToTimeElement.addClass("in_validate_break_time");
    
                //console.log("break2 end time id:", break2ToTimeElement);
                //break 2
                let break2StartTimeV = new Date().setHours(shift.GetHours(break2FromTimeElement.val()), shift.GetMinutes(break2FromTimeElement.val()), 0);
                let break2EndTimeV = new Date(break2StartTimeV).setHours(shift.GetHours(break2ToTimeElement.val()), shift.GetMinutes(break2ToTimeElement.val()), 0);
    
                break2FromTimeElement.addClass("in_validate_break_time");
                break2ToTimeElement.addClass("in_validate_break_time");
    
                //break 3
                let break3StartTimeV = new Date().setHours(shift.GetHours(break3FromTimeElement.val()), shift.GetMinutes(break3FromTimeElement.val()), 0);
                let break3EndTimeV = new Date(break3StartTimeV).setHours(shift.GetHours(break3ToTimeElement.val()), shift.GetMinutes(break3ToTimeElement.val()), 0);
    
                break3FromTimeElement.addClass("in_validate_break_time");
                break3ToTimeElement.addClass("in_validate_break_time");
    
                console.log("---Validate Break2 Time ------------");
                console.log("Shift Start time: ", shiftfromTimeElement.val(), ":", shiftStartTimeV, ", End:", shifttoTimeElement.val(), ":", break1EndTimeV);
                console.log("Break1 Start time: ", break1FromTimeElement.val(), ":", break1StartTimeV, ", End:", break1ToTimeElement.val(), ":", break1EndTimeV);
                console.log("Break2 Start time: ", break2FromTimeElement.val(), ":", break2StartTimeV, ", End:", break2ToTimeElement.val(), ":", break2EndTimeV);
                console.log("Break3 Start time: ", break3FromTimeElement.val(), ":", break3StartTimeV, ", End:", break3ToTimeElement.val(), ":", break3EndTimeV);
    
                //validate break 1
                if (shift.ValidateBreak1Timing(shiftfromTimeElement, shifttoTimeElement, break1FromTimeElement, break1ToTimeElement) == true) {
    
                    //validated break 1 successfully
                    break1FromTimeElement.removeClass("in_validate_break_time");
                    break1ToTimeElement.removeClass("in_validate_break_time");
                    //console.log("---------removed in valid class from break1---------------");
    
                    //validate break 2
                    if (shift.ValidateBreak2Timing(
                        shiftfromTimeElement, shifttoTimeElement,
                        break1FromTimeElement, break1ToTimeElement,
                        break2FromTimeElement, break2ToTimeElement) == true) {
    
                        //validated break 2 successfully
                        break2FromTimeElement.removeClass("in_validate_break_time");
                        break2ToTimeElement.removeClass("in_validate_break_time");
    
                        //validate break 3
                        if (break2EndTimeV < break3StartTimeV && shiftEndTimeV > break3EndTimeV) {
    
                            console.log("You have a valid break 3 time");
    
                            break3FromTimeElement.removeClass("in_validate_break_time");
                            break3ToTimeElement.removeClass("in_validate_break_time");
    
                            timeStatus = true;
                        }
                        else {
                            console.log("You dont have a valid break3 time");
                        }
                    }
                    else {
                        console.log("You dont have a valid break2 time");
                    }
                }
                else {
                    console.log("You dont have a valid break1 time");
                }
                console.log("---End Validate Break 2 Time ------------", timeStatus);
                return timeStatus;
            },
            validateShiftDetails: function () {
                let status = false;
                var shifttable = $("table tr[dayno]");
                if (shifttable.length > 0) {
                    console.log("I am here");
                    status = false;
                    shifttable.each(function (i, index) {
                        console.log(i);
    
                        //shift start and end time validation [24 time validation]
                        let startTimeEl = $(this).find(".shift_starttime");
                        let endTimeEl = $(this).find(".shift_endtime");
                        var shiftTimeStatus = shift.validateStartAndEndTime(startTimeEl, endTimeEl);
    
                        //break1 start and end time [24 time validation]
                        let break1_starttimeEl = $(this).find(".break1_starttime");
                        let break1_endtimeEl = $(this).find(".break1_endtime");
                        var break1TimeStatus = shift.validateStartAndEndTime(break1_starttimeEl, break1_endtimeEl);
    
                        //break2 start and end time [24 time validation]
                        let break2_starttimeEl = $(this).find(".break2_starttime");
                        let break2_endtimeEl = $(this).find(".break2_endtime");
                        var break2TimeStatus = shift.validateStartAndEndTime(break2_starttimeEl, break2_endtimeEl);
    
                        //break3 start and end time [24 time validation]
                        let break3_starttimeEl = $(this).find(".break3_starttime");
                        let break3_endtimeEl = $(this).find(".break3_endtime");
                        var break3TimeStatus = shift.validateStartAndEndTime(break3_starttimeEl, break3_endtimeEl);
    
                        //validate break 1, break 2, break 3
                        if (shiftTimeStatus == true) {
    
                            //validate break 1
                            let break1TimeValidationStatus = false;
                            if (break1TimeStatus == true) {
                                //console.log("Start validating the break 1");
                                break1TimeValidationStatus = shift.ValidateBreak1Timing(startTimeEl, endTimeEl, break1_starttimeEl, break1_endtimeEl);
                            }
    
                            //validate break 2
                            let break2TimeValidationStatus = false;
                            if (break2TimeStatus == true) {
                                // console.log("Start validating the break 2");
                                break2TimeValidationStatus = shift.ValidateBreak2Timing(
                                    startTimeEl, endTimeEl,
                                    break1_starttimeEl, break1_endtimeEl,
                                    break2_starttimeEl, break2_endtimeEl);
                            }
    
                            //validate break 3
                            let break3TimeValidationStatus = false;
                            if (break3TimeStatus == true) {
                                console.log("Start validating the break 3");
                                break3TimeValidationStatus = shift.ValidateBreak3Timing(
                                    startTimeEl, endTimeEl,
                                    break1_starttimeEl, break1_endtimeEl,
                                    break2_starttimeEl, break2_endtimeEl,
                                    break3_starttimeEl, break3_endtimeEl);
                            }
                        }
                         
                    });
                }
                else {
                    status = true;
                }
                return status;
            },
            isValidate24HourTime: function (s) {
                var t = s.split(':');
                return /^\d\d:\d\d$/.test(s) &&
                    t[0] >= 0 && t[0] <= 24 &&
                    t[1] >= 0 && t[1] <= 60
            },
            GetHours: function (d) {
                var h = parseInt(d.split(':')[0]);
                if (d.split(':')[1].split(' ')[1] == "PM") {
                    h = h + 12;
                }
                return h;
            },
            GetMinutes: function (d) {
                return parseInt(d.split(':')[1].split(' ')[0]);
            }
        };
    
       
    </script>

    Friday, January 22, 2021 12:48 PM

All replies

  • User303363814 posted

    it was not working propertly[/quoe]Is there any more detail available?

    Your conditions seem very strange 

    "Shift start time should be greater than of end time" - they have to end before they start?????

    What does Condition 2 mean?

    Condition 3 - The break must be after the shift finishes????

    Can you tell us what does not work and what these conditions mean?

    Saturday, January 23, 2021 12:04 AM
  • User-2028591720 posted

    Thanks for your reply,

    I have updated the question.

    Condition 3 - The break must be after the shift finishes????

    The break time should be less than of shift end time.

    Saturday, January 23, 2021 3:00 AM
  • User-1330468790 posted

    Hi prasadr07, 

     

    I think you should tell us the conditions in details so that we could help you check the codes following the rules you defined.

    Could you please specify the conditions so that we could understand the rules correctly? 

    Condition 1: Shift start time should be less than of end time. 

    Shift Start Time: 11: 00, Shift End Time should be later than 11:00, e.g. 15:00, correct?

     

    Condition 2: Shift break times such as break1(start and end time), break2(start and end time), break3(start and end time).

    start and end time should be following the same rule as Shift Time, correct?

     

    Condition 3: Break1 start time should be less than of shift end time

    Since you use the 'less', which is similar to the Condition 1, do you mean that the Break1 end time should be also less than the shift end time?

     

    Condition 4: Break2 start time should be less than of break 1 end time

    Shift Start Time: 11:00, Shift End Time: 15:00

    Break1 Start Time: 13:30, Break1 End Time: 14:30, are suitable time period

    How would you put the Break2 in above example?

     

    Condition 5: Break3 start time should be less than of break 2 end time. Without break2 break3 timing will be considered as invali

    You should also specify the condition for break3 with considering the comparison with Shift time period, Break1 time period and Break2 time period.

    One point here is that the time will be invalid if any of previous time is invalid or missing, correct?

     

    By the way, you could refactor the js code since you repeat too many time for comparison. Another improvement would be removing the 'true' or 'false' in if condition since the returned value of the function is of type 'bool'.

    For example, 

     //you could modify '== false' with a simple symbol '!'
    if (shift.isValidate24HourTime(startTimeEl.val()) == false) {
        startTimeEl.addClass("validate-time-invalid");
    }
    
    // as below
    if (!shift.isValidate24HourTime(startTimeEl.val())) {
        startTimeEl.addClass("validate-time-invalid");
    }
    
    // no need to compare true with true
    if (shift.isValidate24HourTime(startTimeEl.val()) == true && shift.isValidate24HourTime(endTimeEl.val()) == true) {
    ......
    }
    
    // Try below codes
    if (shift.isValidate24HourTime(startTimeEl.val()) && shift.isValidate24HourTime(endTimeEl.val())) {
    ......
    }

     

    Looking forward to your reply.

    Best regards,

    Sean

    Monday, January 25, 2021 8:39 AM