Can someone help me write a function for showing a line of text (preferably by showing a css class) for a jquery datepicker after someone has clicked a date?
The not
just use onSelect
property when you init datepicker
$( "#delivery_date" ).datepicker({
dateFormat: "dd/mm/yy",
minDate : new Date(),
onSelect: function(str) {
var lastDate = new Date(),
nums = str.split("/"),
userDate = new Date(nums[2], nums[1]-1, nums[0]), // reverse date
period = 21;
lastDate.setDate(lastDate.getDate()+period);
if (userDate<lastDate) {
// notify
alert("inside 21 days period");
}
}
});
try FIDDLE
You might be able to validate it using date.js, it has a fluent API which makes it very easy to express your conditions.
Example code might be:
var enteredDate = $("#delivery_date").prop("value");
var parsed = Date.parse(enteredDate);
var start = Date.today();
var end = Date.today().add(21).days();
if (!parsed.between(start, end)) {
alert("Date is valid");
} else {
alert("Date is not valid");
}
Test plunker
U can use onClose
in datepicker
$(function() {
$( "#date" ).datepicker({
dateFormat: "dd/mm/yy",
onClose: function() {
valid();
}
});
});
jsfiddle - example