0
$('#datepicker_start').datepicker({
         dateFormat: 'd-M-y' ,
         onSelect: function(selected) {
              $("#datepicker_end").datepicker("option","10-03-2015", selected)
            }
    });


$('#datepicker_end').datepicker({
         dateFormat: 'd-M-y' ,
         onSelect: function(selected) {
              $("#datepicker_start").datepicker("option","09-04-2015", selected)
            }
    });


<button class="btn" id="btn_submit" type="button" disabled="disabled">Submit</button> 


        //enable 'Submit' button only if start-end dates both are not empty
        if( $('#datepicker_start').val().length !=0 && $('#datepicker_end').val().length !=0 ){
            $('#btn_submit').attr('disabled', false);            
        }else{
            $('#btn_submit').attr('disabled',true);
        }

The above does not checks the start-end dates input when date is selected from calendar but on document load. How to check input value is empty or not when date is selected from datepicker

4
  • What is the problem in above code? Error or not working? Commented Apr 22, 2015 at 11:53
  • Your "enable submit button only if..." should be handled by an event. I mean, how do you know when datepickers have a value? Commented Apr 22, 2015 at 11:55
  • @kmsdev, that could be handled using onSelect event for datepicker Commented Apr 22, 2015 at 12:00
  • Yes, that's it. Other workaround would be to manage your submit button, something like $('#btn_submit').on('click', function(){ // if datepickers have values return true, else return false }); Commented Apr 22, 2015 at 12:02

4 Answers 4

0

Try this : You can create a function to enable or disable submit button and call it after selecting a date value.

$(function(){

   var enableDisableSubmitBtn = function(){
     var startVal = $('#datepicker_start').val().trim();
     var endVal = $('#datepicker_end').val().trim();
     var disableBtn =  startVal.length == 0 ||  endVal.length == 0;
     $('#btn_submit').attr('disabled',disableBtn);
   }

   $('#datepicker_start').datepicker({
         dateFormat: 'd-M-y' ,
         onSelect: function(selected) {
              $("#datepicker_end").datepicker("option","10-03-2015", selected);
              enableDisableSubmitBtn();
            }
    });


    $('#datepicker_end').datepicker({
         dateFormat: 'd-M-y' ,
         onSelect: function(selected) {
              $("#datepicker_start").datepicker("option","09-04-2015", selected);
             enableDisableSubmitBtn();
            }
    });
});


<button class="btn" id="btn_submit" type="button" disabled="disabled">Submit</button>

JSFiddle Demo

Sign up to request clarification or add additional context in comments.

Comments

0

You can make a check function and call it when you pick a date some thing like that :

function check(){
        if( $('#datepicker_start').val().length !=0 && $('#datepicker_end').val().length !=0 ){
            $('#btn_submit').attr('disabled', false);            
        }else{
            $('#btn_submit').attr('disabled',true);
        }
}
$('#datepicker_start').datepicker({
         dateFormat: 'd-M-y' ,
         onSelect: function(selected) {
              $("#datepicker_end").datepicker("option","10-03-2015", selected)
check();
            }
    });


$('#datepicker_end').datepicker({
         dateFormat: 'd-M-y' ,
         onSelect: function(selected) {
              $("#datepicker_start").datepicker("option","09-04-2015", selected)
            }
    });


<button class="btn" id="btn_submit" type="button" disabled="disabled">Submit</button> 

Comments

0

Well i suggest you to work within the onSelect datepicker's event:

$('#datepicker_start').datepicker({
     dateFormat: 'd-M-y' ,
     onSelect: function(selected) {
        $("#datepicker_end").datepicker("option","10-03-2015", selected);
        enablebtn($("#btn_submit"), $('#datepicker_start'), $('#datepicker_end')); // use it here and pass the elements
     }
});

$('#datepicker_end').datepicker({
     dateFormat: 'd-M-y' ,
     onSelect: function(selected) {
        $("#datepicker_start").datepicker("option","09-04-2015", selected);
        enablebtn($("#btn_submit"), $('#datepicker_start'), $('#datepicker_end')); // use it here and pass the elements
     }
});

function enablebtn(btn, startDate, endDate){
    var bool = startDate.val().length && endDate.val().length; // returns true/false;
    btn.prop('disabled', bool); // now it enables when true and disables if false.
}

Comments

-1

Try something like this

if(isNaN(fromDate) || isNaN(toDate))
{
alert("Please select To/From Date");
}

1 Comment

This makes no sense and has no relation with real issue. Also you will get always a NaN from the datepickers values.

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.