0

I am using the following code to validate date form fields to ensure the user does not enter a start date that is after the end date.

the code works fine in Chrome and catches the conditions when the dates are entered incorrectly.

In firefox however the form submits even when the start date entered is after the end date entered which clearly is not correct.

Assistance with a solution would be appreciated. I have been on this for hrs without any success.

var startdate = new Date(Date.parse(document.form_subscription.start_date.value));
var enddate = new Date(Date.parse(document.form_subscription.end_date.value));

if (enddate < startdate) {
                alert("Availability 'To' Date cannot be on or earlier than the Availability 'Start' Date.");
                return false;
                }

The following is a solution found that works perfrectly

var enddate = document.form_subscription.end_date.value;
        var startdate = document.form_subscription.start_date.value;
        enddate = enddate.replace(/-/g,'/');
        enddate = new Date(enddate);
        startdate = startdate.replace(/-/g,'/');
        startdate = new Date(startdate);

        if (enddate < startdate) {
            alert("Availability 'To' Date cannot be on or earlier than the Availability 'Start' Date.");
            return false;
  • SADLY NOT !!!!! when i use the suggested code it does not validate in google or firefiox. So i would appreciate some assistance PLEASE – Claude Raiola Aug 07 '18 at 03:57
  • Great news for those who seek a working solution this works perfectly: var enddate = document.form_subscription.end_date.value; var startdate = document.form_subscription.start_date.value; enddate = enddate.replace(/-/g,'/'); enddate = new Date(enddate); startdate = startdate.replace(/-/g,'/'); startdate = new Date(startdate); if (enddate < startdate) { alert("Availability 'To' Date cannot be on or earlier than the Availability 'Start' Date."); return false; – Claude Raiola Aug 07 '18 at 04:49
  • Just make sure to use a [valid date time string](https://stackoverflow.com/questions/51715259/what-are-valid-date-time-strings-in-javascript) and read the section regarding browser compatibility. – str Aug 07 '18 at 09:29

0 Answers0