简体   繁体   English

如何验证jQuery UI日期选择器“从日期”到“到日期”

[英]How to validate jQuery UI Date Picker “From Date” to “To Date”

I have 2 fields "From date" and "To date". 我有2个字段“起始日期”和“截止日期”。 I am new in jQuery and I am learning it. 我是jQuery的新手,正在学习中。

  1. When selecting from date on jQuery UI Date Picker, all previous dates (before current date) to be un-selectable (greyed out). 在jQuery UI Date Picker上从日期中进行选择时,所有以前的日期(当前日期之前)都是不可选择的(显示为灰色)。
  2. When selecting to date, only dates starting from current date 选择日期时,仅从当前日期开始的日期
    • 1 day to be selectable (cannot select same from date or date passed as to date) 可以选择1天(不能从日期或经过的日期中选择相同的日期)

I am currently using this code: 我目前正在使用此代码:

$( function() {
var dateFormat = "dd-mm-yy",
  from = $( "#from" )
    .datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 3
    })
    .on( "change", function() {
      to.datepicker( "option", "minDate", getDate( this ) );
    }),
  to = $( "#to" ).datepicker({
    defaultDate: "+1w",
    changeMonth: true,
    numberOfMonths: 3
  })
  .on( "change", function() {
    from.datepicker( "option", "maxDate", getDate( this ) );
  });

function getDate( element ) {
  var date;
  try {
    date = $.datepicker.parseDate( dateFormat, element.value );
  } catch( error ) {
    date = null;
  }

  return date;
}
});

Note: Date format should be " dd-mm-yy " 注意:日期格式应为“ dd-mm-yy

To grey out dates before current date, simply set minDate to current date in the datepicker configuration. 要使当前日期之前的日期灰显,只需在datepicker配置中将minDate设置为当前日期即可。

minDate: new Date()

You also should explicitly set the dateFormat option to be the same as the one you are using in $.datepicker.parseDate call. 您还应该显式地将dateFormat选项设置为与$ .datepicker.parseDate调用中使用的选项相同。

If I understood your problem correctly, this fiddle should be about right (modified your code a bit) 如果我正确理解了您的问题,那么这种提琴应该是正确的(稍微修改了您的代码)

http://jsfiddle.net/8w8v9/1846/ http://jsfiddle.net/8w8v9/1846/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM