Date Picker - Compare Date

Introduction

Ensuring that dates entered into forms are logically consistent is crucial for data accuracy. This guide provides a script that compares two date fields to ensure that the end date is later than the start date, preventing invalid date entries.

How does it work?

The provided script validates the dates when the form is submitted. It retrieves the values from the start date and end date fields and compares them. If the end date is earlier than the start date, an alert is shown, and the form submission is prevented. This ensures that users enter dates in a logical sequence.

Code:

<script type="text/javascript">
$('form').submit(function(){
    startDate = $("input[name='start_date']").val();
    endDate = $("input[name='end_date']").val();
    if( endDate < startDate ){
        alert("End Date must be larger than the Start Date. Please try again.");
        $.unblockUI();
        return false;
    }else{
        return true;
    }
});
 
</script>
Created by Julieth Last modified by Aadrian on Dec 13, 2024