3

I am using Calender plugin to pick two dates respectively from & to. I need to compare value of to always greater than value of from using jQuery as soon as I pick 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 is:

<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 comparision:

<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/

Phil
  • 157,677
  • 23
  • 242
  • 245
Ankit
  • 607
  • 2
  • 9
  • 16

1 Answers1

4

You have to change your HTML code, you have used same id and name for both the fields. Change it as per 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" />
pkachhia
  • 1,876
  • 1
  • 19
  • 30
  • 2
    Please check this link http://jqueryui.com/demos/datepicker/#date-range may be it is helpful for you – pkachhia Mar 07 '12 at 05:22
  • Please use the link I have given above, you have no need to write the javascript to compare dates, it will automatically handle everything, check the link – pkachhia Mar 07 '12 at 05:25