Comparing two dates with jQuery

I am using the Calender plugin to select two dates from and to respectively. I need to compare the value always with the jQuery value as soon as I select the dates.

I am using the following code

var fromDate = $("#from").val();
var toDate = $("#to").val();
if (Date.parse(fromDate) > Date.parse(toDate)) {
    alert("Invalid Date Range!\nStart Date cannot be after End Date!")
    return false;
} 

      

And the HTML code:

<input type="text" name="from" id="from" value="" class="datepicker validate[custom[date]]"  tabindex="4" />
<input type="text" name="from" id="from" value="" class="datepicker validate[custom[date]]"  tabindex="4" />

      

If I use jquery validation plugin for comparison:

<input value="" class="validate[required,custom[date],future[2009-01-01]" type="text" id="d1" name="d1" />

      

As in http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/

+3


source to share


1 answer


You have to change your HTML, you used the same ID and name for both fields. Change it as shown below



<input type="text" name="from" id="from" value="" class="datepicker validate[custom[date]]"  tabindex="4" />
<input type="text" name="to" id="to" value="" class="datepicker validate[custom[date]]"  tabindex="4" />

      

+4


source







All Articles