I have created a JavaScript function to validate that the start date is earlier than the end date. If not, an alert should be triggered.
The JavaScript code looks like this:
function DateValidation(startDate, EndDate) {
debugger;
var stdate = startDate;
var enddate = EndDate;
if (stdate!= '' && enddate!='') {
if (stdate > enddate) {
alert('Start date cannot be greater than end date');
return false;
}
else {
return true;
}
}
}
This JavaScript function is executed when a button labeled "Show Report" is clicked.
Challenges I am encountering include:
The JavaScript does not accurately validate the dates. What could be the issue? I am passing dates from text inputs
The JavaScript function only runs on the second click of the button, not the first. How can I fix this?
Furthermore, I have applied the function to the button like so:
btnShowReport.Attributes.Add("onclick", "return DateValidation('" + txtStartDate.Text + "', '" + txtEndDate.Text + "');");
Is the above implementation correct? And where should I properly register the JavaScript function?
Your guidance is appreciated. Thank you!