How can I limit two dates in an ExtJS DatePicker?
-
26-10-2019 - |
Question
I have a DatePicker in ExtJS4. I only want to allow TWO dates for each month. The 15th and last day (30/31/28/29 depending on month/year)
How can I disable every day in the picker but allow those two dates?
Solution
See disabledDates
config option for Ext.form.field.Date
From API docs:
disabledDates : String[] An array of "dates" to disable, as strings. These strings will be used to build a dynamic regular expression so they are very powerful. Some examples:
// disable these exact dates: disabledDates: ["03/08/2003", "09/16/2003"] // disable these days for every year: disabledDates: ["03/08", "09/16"] // only match the beginning (useful if you are using short years): disabledDates: ["^03/08"] // disable every day in March 2006: disabledDates: ["03/../2006"] // disable every day in every March: disabledDates: ["^03"]
Note that the format of the dates included in the array should exactly match the format config. In order to support regular expressions, if you are using a date format that has "." in it, you will have to escape the dot when restricting dates. For example: ["03\.08\.03"].
OTHER TIPS
//Get the last date of the month. If in Feb 2012, lastDate is 29.
var lastDate = Ext.Date.getDaysInMonth(new Date());
//15th
var middleDate = 15;
//Construct regular expression
var disabledArray=[];
var today = Ext.Date.format(new Date(), 'm/d/Y');
var dateReg = /(\d{2}\/)\d{2}(\/\d{4})/;
disabledArray.push(today.replace(dateReg, '$1' + middleDate + '$2'));
disabledArray.push(today.replace(dateReg, '$1' + lastDate + '$2'));
//Something like "^(?!02/15/2012|02/29/2012).*$" including the two days allowed.
var disabledReg = '^(?!' + disabledArray.join('|') + ').*$';
//Apply the regular expression to date field
var dateField = new Ext.form.field.Date({
format: 'm/d/Y',
disabledDates: [disabledReg]
});