I am using jQuery datepicker to allow the user to select the dates.
var currentTime = new Date();
var startDateFrom = new Date(currentTime.getFullYear(),currentTime.getMonth()-2);
var datePickerOptions = {
dateFormat : 'dd-MM-yy',
monthNames: ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
changeYear : true,
changeMonth : true,
maxDate : '0',
minDate : startDateFrom
}
$(document).ready(function() {
$('#myHeader').on( 'draw.dt', function () {
$(".datepickerColumn").datepicker(datePickerOptions);
});
})
I wanted to allow the user that in addition to select the date from datepicker, it can be entered manually also. Right now user is able to just enter the numbers, and no text and special characters like hyphen or slashes. Can anyone please help me with this. How can we allow to enter the date in a valid format. Thanks in advance
2
Answers
I think you are looking for Autocorrection of date after entering digit.
below is some idea. you also need to add more logic towards if day is exceeded greater than 31 or 30 and month is grater than 12.
In the OP you have:
What is a
"draw.dt"
event? Also, this:should be this according to
option(options)
:The
.datepicker()
doesn’t accept any input that isn’t part of it’s currentdateFormat
option. One way around it is to intercept any dashes the user types and change them into forward slashes.Wrap the
<input>
in a content element (any element with an end tag (ex.</div>
)).Have that element listen for the
"keydown"
or"keyup"
event.Make the callback function append a
"/"
to the<input>
value whenever a"-"
is keyed.