PeterS
PeterS

Reputation: 724

How to filter dates in Javascript

I have this code is JS

$(".datepicker").datepicker();
    $(".datepicker-to").datepicker({
        changeMonth: true,
          changeYear: true,
        maxDate: "0D"
    });

that accepts date not beyond the current system date. Now for example I have two textbox that accepts date from and date to...Now I want the system to validate. Example I have entered in date from: 1-15-13(MMDDYY) and date to: 1-13-13...abviously it is not acceptable. Now how to filter this one?

Upvotes: 1

Views: 240

Answers (2)

Praveen
Praveen

Reputation: 56509

jQuery Datepicker provides the ability to select the date range.

Try using the below code.

HTML:

<input id="fromDate" type="text"/>
<input id="toDate" type="text"/>

jQuery:

$('#fromDate').datepicker({
    changeMonth: true,
    changeYear: true,
    onClose: function( selectedDate ) {
        $( "#toDate" ).datepicker( "option", "minDate", selectedDate );
        }
    });
    $('#toDate').datepicker({
        changeMonth: true,
        changeYear: true,
        onClose: function( selectedDate ) {
            $( "#fromDate" ).datepicker( "option", "maxDate", selectedDate );
        }
    });

Check this JSFiddle.

Upvotes: 2

Vishal Suthar
Vishal Suthar

Reputation: 17194

Here you go:

HTML:

<input id="from" type="text"/>
<input id="to" type="text"/>

JQUERY:

 $(function () {
     $("#from").datepicker({
         defaultDate: "+1w",
         changeMonth: true,
         onClose: function (selectedDate) {
             $("#to").datepicker("option", "minDate", selectedDate);
         }
     });
     $("#to").datepicker({
         defaultDate: "+1w",
         changeMonth: true,
         onClose: function (selectedDate) {
             $("#from").datepicker("option", "maxDate", selectedDate);
         }
     });
 });

Live Demo: JSFiddle

Upvotes: 0

Related Questions