【问题标题】:datetimepicker startDate can't greater than endDatedatetimepicker startDate 不能大于 endDate
【发布时间】:2016-11-24 11:29:36
【问题描述】:

我遇到了引导验证 datetimepicker 的问题。 endDate 必须小于 startDate 并显示消息或强制 endDate 输入大于 startDate。

我有这样的 datetimepicker 格式:

$(document).ready(function() {
$(".datetimepicker").datetimepicker({
     format: "YYYY-MM-DD",
});

这是我的表单代码:

<div class="col-xs-4 col-sm-4 col-md-4">
   <div class="form-group">
      <label>Date In:</label>
      {!! Form::text('date_in', null, array( 'id' => 'date_in', 'class' => 'form-control datetimepicker' )) !!}
   </div>
</div>

<div class="col-xs-4 col-sm-4 col-md-4">
   <div class="form-group">
      <label>Date Out:</label>
      {!! Form::text('date_out', null, array('id' => 'date_out', 'class' => 'form-control datetimepicker' )) !!}
   </div>
</div>

到目前为止,这是我的 jquery:

$.each(inputFormEle, function(index, value) {
var getElByName = $(value).attr('name')

switch(getElByName) {
case 'date_in':

    $('#accidentForm').bootstrapValidator('addField', getElByName, {
        validators: {
            date: {
                format: 'YYYY-MM-DD'
            },
            callback: {
                callback: function(value, validator, $field) {
                    var dateIn = moment(value);
                    var dateOut = moment($('input[name="date_in"]').val());
                    console.log(dateIn, dateOut);
                    if ( dateIn.isBefore(dateOut) ) { 
                        return {
                            valid: true,
                        }
                    }
                    else {
                        return {
                            valid: false,
                            message: 'The Date in must be less than Date out'
                        }
                    }
                }
            }
        },
    });

break;

case 'date_out':

    $('#accidentForm').bootstrapValidator('addField', getElByName, {
        validators: {
            date: {
                format: 'YYYY-MM-DD'
            },
            callback: {
                callback: function(value, validator, $field) {
                    var dateOut = moment(value);
                    var dateIn = moment($('input[name="date_out"]').val());

                    if ( dateOut.isAfter(dateIn) ) { 
                        return {
                            valid: true,
                        }
                    }
                    else {
                        return {
                            valid: false,
                            message: 'The Date out must be greater than Date in'
                        }
                    }
                }
            }
        }
    });
break;
  }
});

它根本不起作用,有什么想法吗? 谢谢。

【问题讨论】:

标签: jquery datetimepicker


【解决方案1】:
$("#endDate").change(function(){

    var startDate = new Date($("#startDate").val());

    var endtDate = new Date($("#endDate").val());

    if(startDate < endtDate ){

        /* your validation and error message */

    }else{

        /* your piece of code*/

    }
});


/* similarly you can write for start date also */

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    相关资源
    最近更新 更多