【问题标题】:angular ui-bootstrap datepicker mindate not validated when typed in输入时未验证角度 ui-bootstrap datepicker mindate
【发布时间】:2016-01-12 13:28:52
【问题描述】:

我有一个带有 ui-bootstrap 日期选择器的表单。我想防止日期成为过去。

我将指令的最小日期设置为new Date(),如下所示。这正确地防止了在使用弹出窗口时选择过去的日期,但是我仍然可以输入过去的日期并且这验证 OK。

如何对输入的日期强制执行最小日期验证?

Plunkr:https://plnkr.co/edit/EHO1BG8BspNMvsoKT30l?p=preview

HTML:

<div class="input-group">
    <input type="text" 
           class="form-control" 
           uib-datepicker-popup="{{format}}" 
           ng-model="dt" 
           is-open="popup1.opened" 
           min-date="minDate" 
           datepicker-options="dateOptions" 
           ng-required="true" 
           close-text="Close"
           alt-input-formats="altInputFormats"
           name="dt"/>
    <span class="input-group-btn">
        <button type="button" class="btn btn-default" ng-click="open1()">
            <i class="glyphicon glyphicon-calendar"></i>
        </button>
    </span>
</div>

JS:

app.controller('MainCtrl', function($scope) {
  $scope.dt = new Date();
  $scope.minDate = new Date();
  $scope.format = "dd/MM/yyyy";
  $scope.altInputFormats = ['d!/M!/yyyy'];
  $scope.dateOptions = {
    formatYear: 'yy',
    startingDay: 1
  };

  $scope.popup1 = {
    opened: false
  };

  $scope.open1 = function() {
    $scope.popup1.opened = true;
  };
});

【问题讨论】:

    标签: angularjs datepicker angular-ui-bootstrap


    【解决方案1】:

    这应该可以正常工作,我在你的控制器中添加了changeHandler函数并在输入的ng-change上调用它。

    HTML:

    <div class="input-group">
          <input type="text" 
                 class="form-control" 
                 uib-datepicker-popup="{{format}}" 
                 ng-model="dt" 
                 is-open="popup1.opened" 
                 min-date="minDate" 
                 datepicker-options="dateOptions" 
                 ng-required="true" 
                 close-text="Close"
                 alt-input-formats="altInputFormats"
                 ng-change="changeHandler()"
                 name="dt"/>
          <span class="input-group-btn">
            <button type="button" class="btn btn-default" ng-click="open1()">
              <i class="glyphicon glyphicon-calendar"></i>
            </button>
          </span>
    </div>
    

    JS:

    app.controller('MainCtrl', function($scope) {
      $scope.dt = new Date();
      $scope.minDate = new Date();
      $scope.format = "dd/MM/yyyy";
      $scope.altInputFormats = ['d!/M!/yyyy'];
      $scope.dateOptions = {
         formatYear: 'yy',
         startingDay: 1
      };
    
      $scope.popup1 = {
         opened: false
      };
    
      $scope.changeHandler = function () {
        $scope.dateForm.dt.$setValidity('$valid', $scope.dt.getTime() >= $scope.minDate.getTime());
      }
    
      $scope.open1 = function() {
         $scope.popup1.opened = true;
      };
    
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-27
      相关资源
      最近更新 更多