I am trying to get the result from the function called ‘validateScheduleDate()’ and only if the result comes from the function, then next line of code should be executed. I am trying to validate two dates to find these dates are between the given time frame.
Right now, it does not wait for the result from the function called ‘validateScheduleDate’
Here is my code in Jvascript
$wizard.wizard({
validateTab: function (currentTab) {
validateScheduleDate().then(function (data) {
if (data != 'noDifference') {
// Not passed to the next line
}
});
switch (currentTab.index()) {
case 0: // tabPaneNames.general
return validateSales1()
case 1: // tabPaneNames.problems
return validateSales2()
case 2: // tabPaneNames.serviceProviders
return validateSales3()
default:
return false;
}
}
});
$('[submit-sales').click(function (event) {
event.preventDefault();
validateScheduleDate().then(function (data) {
if (data == 'noDifference') {
validateTabs(function () { submitForm(); }, true);
}
else{
// Not passed to the next line
}
});
});
function validateScheduleDate() {
var startDate = document.getElementById('dispatch-start-date').value;
var startTime = document.getElementById('ddl-dispatch-times').value;
var arrivalTimeFrame = parseFloat(document.getElementById("hdn-WOArrivalTimeFrame-id").value).toFixed(2);
var scheduledDate = document.getElementById("ScheduledDate").value;
var scheduledTime = document.getElementById("ddl-dispatch-scheduled-times").value;
var dateErrorSpan = document.getElementById("ScheduledDate-Error");
var timeErrorSpan = document.getElementById("ScheduledTime-Error");;
var resultdata = $.get('/WorkOrderDispatch/GetScheduledEndDate', {
dispStartDate: startDate, dispStartTime: startTime, dispTimeFrame: arrivalTimeFrame,
dispScheduledDate: scheduledDate, dispScheduledTime: scheduledTime
}).done(function (result) {
if (result == 'hasDaysDifference') {
timeErrorSpan.textContent = '';
dateErrorSpan.textContent = 'Enter the Scheduled Date within the arrival time frame ' + arrivalTimeFrame + ' Hrs'
$wizard.wizard('changeTo', '#' + tabPaneNames.general);
return false;
}
else if (result == 'hasTimeDifference')
{
dateErrorSpan.textContent = '';
timeErrorSpan.textContent = 'Enter the Scheduled Date within the arrival time frame ' + arrivalTimeFrame + ' Hrs'
$wizard.wizard('changeTo', '#' + tabPaneNames.general);
return false;
}
else {
dateErrorSpan.textContent = '';
timeErrorSpan.textContent = '';
return true;
}
});
return resultdata;
}
// Code in MVC controller
public async Task<JsonResult> GetScheduledEndDate(string dispStartDate, string dispStartTime, string dispTimeFrame, string dispScheduledDate, string dispScheduledTime) {
var UserID = User.Identity.GetUserId();
var loginUser = await DbContext.Employees.Where(x => x.ApplicationUserID == UserID).FirstOrDefaultAsync();
if (loginUser == null)
{
throw new OfficetraxException(ErrorCode.NotFound);
}
TimeZoneInfo employeeTimeZone = TimeZoneTools.FindTimeZone(loginUser.TimeZone);
DateTime now = DateTime.UtcNow;
string DispatchNowString = "[Dispatch Now]";
DateTime startDate = Convert.ToDateTime(dispStartDate);
DateTime scheduledDate;
DateTime? dispatchScheduledDate = null;
if (!string.IsNullOrEmpty(dispScheduledDate))
{
scheduledDate = Convert.ToDateTime(dispScheduledDate);
dispatchScheduledDate = scheduledDate.ApplyTimeAndTimeZone(dispScheduledTime, employeeTimeZone);
}
double? arrivalTimeFrame = Convert.ToDouble(dispTimeFrame);
string combinedscheduleDate = dispScheduledDate + " " + dispScheduledTime;
DateTime dispatchStartDate = dispStartTime == DispatchNowString ? now : startDate.ApplyTimeAndTimeZone(dispStartTime, employeeTimeZone);
DateTime? scheduledEndDate = arrivalTimeFrame.HasValue ?
dispatchStartDate.AddHours(arrivalTimeFrame.Value) : (DateTime?)null;
//DateTime? dispatchScheduledDate = DateTime.Parse(combinedscheduleDate);
if (scheduledEndDate.HasValue && dispatchScheduledDate.HasValue)
{
if(dispatchScheduledDate < dispatchStartDate)
{
if (dispatchScheduledDate.Value.Date == dispatchStartDate.Date)
{
return Json("hasTimeDifference", JsonRequestBehavior.AllowGet);
}
else
{
return Json("hasDaysDifference", JsonRequestBehavior.AllowGet);
}
}
if(dispatchScheduledDate > scheduledEndDate)
{
if(dispatchScheduledDate.Value.Date == scheduledEndDate.Value.Date)
{
return Json("hasTimeDifference", JsonRequestBehavior.AllowGet);
}
else
{
return Json("hasDaysDifference", JsonRequestBehavior.AllowGet);
}
}
else
{
return Json("noDifference", JsonRequestBehavior.AllowGet);
}
}
return Json("noDifference", JsonRequestBehavior.AllowGet);
}