Skip to content

Commit b78ff94

Browse files
committed
Partial master merge
2 parents f3376fe + 2edb8c5 commit b78ff94

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ String. Default: 'en'
164164

165165
The two-letter code of the language to use for month and day names. These will also be used as the input's value (and subsequently sent to the server in the case of form submissions). Currently ships with English ('en'), German ('de'), Brazilian ('br'), and Spanish ('es') translations, but others can be added (see I18N below). If an unknown language code is given, English will be used.
166166

167+
### forceParse
168+
169+
Boolean. Default: true
170+
171+
Whether or not to force parsing of the input value when the picker is closed. That is, when an invalid date is left in the input field by the user, the picker will forcibly parse that value, and set the input's value to the new, valid date, conforming to the given `format`.
172+
167173
## Markup
168174

169175
Format a component.

js/bootstrap-datepicker.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444
if(this.component && this.component.length === 0)
4545
this.component = false;
4646

47+
this.forceParse = true;
48+
if ('forceParse' in options) {
49+
this.forceParse = options.forceParse;
50+
} else if ('dateForceParse' in this.element.data()) {
51+
this.forceParse = this.element.data('date-force-parse');
52+
}
53+
4754
if (this.isInput) { //single input
4855
this.element.on({
4956
focus: $.proxy(this.show, this),
@@ -162,7 +169,14 @@
162169
if (!this.isInput) {
163170
$(document).off('mousedown', this.hide);
164171
}
165-
if (e && e.currentTarget.value)
172+
173+
if (
174+
this.forceParse &&
175+
(
176+
this.isInput && this.element.val() ||
177+
this.hasInput && this.element.find('input').val()
178+
)
179+
)
166180
this.setValue();
167181
this.element.trigger({
168182
type: 'hide',
@@ -809,7 +823,7 @@
809823
}
810824
for (var i=0, s; i<setters_order.length; i++){
811825
s = setters_order[i];
812-
if (s in parsed)
826+
if (s in parsed && !isNaN(parsed[s]))
813827
setters_map[s](date, parsed[s])
814828
}
815829
}

js/locales/bootstrap-datepicker.ms.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
daysShort: ["Aha", "Isn", "Sel", "Rab", "Kha", "Jum", "Sab", "Aha"],
99
daysMin: ["Ah", "Is", "Se", "Ra", "Kh", "Ju", "Sa", "Ah"],
1010
months: ["Januari", "Februari", "Mac", "April", "Mei", "Jun", "Julai", "Ogos", "September", "Oktober", "November", "Disember"],
11-
monthsShort: ["Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Ogo", "Sep", "Okt", "Nov", "Dis"]
11+
monthsShort: ["Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Ogo", "Sep", "Okt", "Nov", "Dis"],
1212
today: "Hari Ini"
1313
};
1414
}(jQuery));

tests/suites/formats.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,18 @@ test('Regression: End-of-month bug', patch_date(function(Date){
193193
.datepicker('setValue');
194194
equal(this.input.val(), '29-02-2012');
195195
}));
196+
197+
test('Invalid formats are force-parsed into a valid date on tab', patch_date(function(Date){
198+
Date.now = UTCDate(2012, 4, 31);
199+
this.input
200+
.val('44-44-4444')
201+
.datepicker({format: 'yyyy-MM-dd'})
202+
.focus();
203+
204+
this.input.trigger({
205+
type: 'keydown',
206+
keyCode: 9
207+
});
208+
209+
equal(this.input.val(), '56-September-30');
210+
}));

0 commit comments

Comments
 (0)