Jquery datepicker- Prevent to submit form before date is selected
I got Jquery UI Datep开发者_开发问答icker that work well, but one important issue make me serious problems.Problem is when user dont select date from datepicker and submit query, external service throw error, otherwise everything is ok. How to prevent that, disable submit button if date is not selected?Any ideas on how to do that. I put code on JSFIDDLE One more question.How to prevent users to select previous date than tommorow date and set tommorow as default date thx
Take a look at this code snippet. I stripped down unimportant code and commented each line I added. Also updated jsfiddle example.
//variable to indicate was date selected or not
var dateSelected = false;
$('#date').datepicker({
onSelect: function(dateText, inst) {
...
//set to true on date selection
dateSelected = true;
},
//minimal date to select (today)
minDate: new Date( (new Date()).getTime() + 3600*24*1000 )
})
...
//return false when date wasn't selected to prevent submission
$('form').submit(function(){
if (!dateSelected) {
alert('Choose date please');
}
return dateSelected;
});
精彩评论