Skip to content Skip to sidebar Skip to footer

Check If The Value In A Datepicker Html Element Is Empty And Execute Jquery Function

I'm using a jquery datepicker in my form. I'm checking if the date is empty using jquery and checking if the date selected is a Sunday. I'm using the Blur function as I couldn't fi

Solution 1:

Use the .onSelect() event:

$("#date_o").datepicker({
    onSelect: function(dateText, inst) {
        if($("#date_o").val() != '' ){
            var date_val = $("#date_o").val();
            $.ajax({
                data: 'date='+date_val,
                url:'common/ajax/date_check.php',
                success:function(data){
                    if(data=="sunday"){
                      alert('Sorry You cant book on a Sunday.!!!');
                      $("#date_o").val('');
                    }else{

                    }
                }
            });
        }
    }
});

You also don't need to use server side to check if the day is sunday:

$("#date_o").datepicker({
    onSelect: function(dateText, inst) {
        var d = newDate(dateText);
        if(d.getDay() == 0){
            alert('You cannot choose sunday!');
            $('#date_o').val('');
        }
    }
});

Here's a working jsFiddle

Post a Comment for "Check If The Value In A Datepicker Html Element Is Empty And Execute Jquery Function"