I have two Kendo Time Pickers on my page. Both are optional but I want to prompt the user to input the value in the format hh:mm am/pm. I want to use the KendoValidator to validate that the values are either empty or contain a valid time. Also, for the second input (end time), a valid time must exist in start time and it cannot come before the start time.
The first issue I am running into is that when using the dateInput: true option to enforce the format it is sending the string "hh:mm am/pm" to the validator as the date when the user enters nothing. Since this is not null it tries to validate it which it of course fails since "hh:mm am/pm" is not a date. This is also causing an issue with validating the required attribute on the startDate.
Another issue would be that this gets passed to the server as the invalid date string which would potentially cause issues on the back end.
Finally, when I do not enter a start date at all it should trigger the required message but it is not.
How can I solve this issue? Here is what my code looks like at the moment:
Initialize Time Pickers:
$.each($(".js-time-picker"), function (index, picker) {
let id = $(picker).attr("id");
timePickers[id] = $(`#${id}`).kendoTimePicker({
dateInput: true,
format: "h:mm tt",
parseFormats: "HH:mm",
}).data("kendoTimePicker");
timePickers[id]._dateInput.setOptions({
messages: {
hour: "hh",
minute: "mm",
dayperiod: "am/pm"
}
});
});
Validator:
$(document).ready(function () {
myValidator = $("#myForm").kendoValidator({
rules: {
timeValidation: function (input) {
if ($(input).hasClass("js-time-picker")) {
var validDate = kendo.parseDate($(input).val());
if (!validDate) {
return false;
}
}
return true;
}
},
messages: {
required: "Required",
timeValidation: "Invalid Time"
},
validateOnBlur: false
}).data("kendoValidator");
});
HTML:
<div class="form-element">
<label id="startTimeLabel" for="startTime">Start Time</label>
<input id="startTime" name="startTime" class="js-time-picker" required validationMessage="Invalid time entered" aria-labelledby="startTimeLabel" />
<span class="k-invalid-msg" data-for="startTime"></span>
</div>
<div class="form-element">
<label id="endTimeLabel" for="endTime">End Time</label>
<input id="endTime" name="endTime" class="js-time-picker" validationMessage="Invalid time entered" aria-labelledby="endTimeLabel" />
<span class="k-invalid-msg" data-for="endTime"></span>
</div>