jQuery Datepicker:验证日期 [英] jquery datepicker: validate date
问题描述
我已经设置了一个jQuery类:
$(function() {
$( ".datepickerIT" ).datepicker({
showOtherMonths: true,
selectOtherMonths: true,
showAnim: "clip",
dateFormat: "dd/mm/yy",
minDate: "01/01/1925",
maxDate: "31/12/2050",
changeMonth: true,
changeYear: true,
yearRange: "1925:2050",
regional: "it"
});
});
我想添加一个日期检查控件,如果用户输入的日期无效,该控件将发出警报
如何将这样的检查添加到".datepickerIT"类中?
onClose: function(dateText, inst) {
try {
$.datepicker.parseDate('dd/mm/yy', dateText);
} catch (e) {
alert(e);
};
我必须在头部添加哪些插件?
不建议使用 Date.parse
,因为在不同的主机解析日期字符串的方式上仍然存在许多差异. [1] [2]
我会用瞬间进行日期验证.
moment(newDate, 'DD/MM/YYYY', true).isValid()
jsfiddle: http://jsfiddle.net/dw8xyzd4/
[1] https://developer .mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date/parse [2] 为什么Date.parse给出不正确的结果? >
i have set a jquery class:
$(function() {
$( ".datepickerIT" ).datepicker({
showOtherMonths: true,
selectOtherMonths: true,
showAnim: "clip",
dateFormat: "dd/mm/yy",
minDate: "01/01/1925",
maxDate: "31/12/2050",
changeMonth: true,
changeYear: true,
yearRange: "1925:2050",
regional: "it"
});
});
I want to add a date check control that alert if user input not is a valid date
How can add to the class ".datepickerIT" a check like this?
onClose: function(dateText, inst) {
try {
$.datepicker.parseDate('dd/mm/yy', dateText);
} catch (e) {
alert(e);
};
And what plugin i must include into head section?
Date.parse
is not recommend to use as there are still many differences in how different hosts parse date strings. [1][2]
I would use moment for date validation.
moment(newDate, 'DD/MM/YYYY', true).isValid()
jsfiddle: http://jsfiddle.net/dw8xyzd4/
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse [2] Why does Date.parse give incorrect results?
这篇关于jQuery Datepicker:验证日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!