Restricting Datepicker input and WYSIWYG
With this setup it is possible to have the user enter an invalid value "1899/5/11". The text box show this value.
1) If it is an invalid value, I don't want to allow it.
The user can enter a "short" year like "51/8/21" which is actually "1951/8/11".
2) Since Datepicker is making assumptions about the "cutoff year", I would want to explicitly show in the input control the assumed year.
The user can input an incomplete entry like "5/2".
3) Since this is also invalid input, I don't want to allow it.
The user can input a bogus day value as in "1956/08/44". Datepicker assumes/transforms the day to 4. This is not WYSIWYG.
4) In this case, this is also bad data entry and I don't want to allow it.
Is there a better way than what I have done in onClose event handler?
My goal here is to not allow bad data entry and to also make assumptions made by Datepicker consistent with WYSIWYG.
$(function(){
// Datepicker
$('#date').datepicker
(
{ //BEGIN: anonymous object creation
dateFormat: 'yy/mm/dd'
,constrainInput: true
,changeMonth: true
,changeYear: true
,yearRange: "-100:+0"
,minDate: '-100y'
,maxDate: '-1d'
,autoSize: true
,onClose: function(sDateText, inst)
{
debugger;
var aOf_YMD = sDateText.split('/');
if(aOf_YMD.length != 3)
{
inst.input.val(""); // Wipe out incorrect input.
return;
}
if(inst.currentYear != inst.selectedYear)
{
//
// Year actually input by the user is out of range of minDate option value
//
inst.input.val(""); // Wipe out incorrect input.
return;
}
if( parseInt(aOf_YMD[ 2 ],10) != inst.selectedDay)
{
//
// The day value entered by the user was not valid; Such as is "1963/08/44"
inst.input.val(""); // Wipe out incorrect input.
return;
}
if(aOf_YMD[ 0 ] != (inst.selectedYear + ""))
{
inst.input.val( inst.selectedYear + "/" + aOf_YMD[ 1 ] + "/" + aOf_YMD[ 2 ] );
}
}//onClose
} //END: anonymous object creation
);
});