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
Js
$(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(), &qu