I am facing an issue with the departure date validation on my website. I have a textbox called txtDepartureDate
where users can choose the date of departure. The requirement is that if the selected date is before today's date, an error message should be displayed. To achieve this, I attempted to utilize the `moment` library in Javascript and also employed the `oninput()` event handler. My logic involves calculating the number of days between today's date and the chosen departure date, and if this number is less than or equal to zero, then the error message displayed by the element lblError
. However, I am struggling with getting the validation part to work properly.
Textbox:
<asp:TextBox ID="txtDepartureDate" runat="server" ForeColor="Gray" onfocus="txtOnFocusDeparture(this)" onblur="txtOnBlurDeparture(this)" oninput="oninputDeparture()" AutoPostBack="True">DEPARTURE DATE</asp:TextBox>
Script:
<script type="text/javascript>
function oninputDeparture() {
var inputDate = moment(document.getElementById('txtDepartureDate').value, 'DD/MM/YYYY');
var todayDate = moment().format('DD/MM/YYYY');
var lblError = document.getElementById('lblError');
var daysDiff = todayDate.diff(inputDate, 'days');
if (daysDiff <= 0) {
lblError.innerText = "Departure Day should be after today";
}
else {
lblError.innerText = "";
}
}
</script>