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/

4
  • 1
    Check the existing duplicate . stackoverflow.com/questions/658522/… Commented Mar 7, 2012 at 5:15
  • I use pure JS in my case for it Commented Mar 7, 2012 at 5:15
  • if( (new Date(fromDate).getTime() > new Date(toDate).getTime())) Commented Mar 7, 2012 at 5:16
  • What's the problem? Do you get any errors? Commented Mar 7, 2012 at 5:17

1 Answer 1

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" />
Sign up to request clarification or add additional context in comments.

2 Comments

Please check this link jqueryui.com/demos/datepicker/#date-range may be it is helpful for you
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.