代码之家  ›  专栏  ›  技术社区  ›  ChaoticNadirs

输入时,angular ui引导日期选择器mindate未验证

  •  6
  • ChaoticNadirs  · 技术社区  · 9 年前

    我有一个带有ui引导日期选择器的表单。我想防止约会成为过去。

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

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

    柱塞: 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>
    

    简体中文:

    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;
      };
    });
    
    1 回复  |  直到 9 年前
        1
  •  7
  •   Ruben Karapetyan    9 年前

    这应该可以正常工作,我在控制器中添加了changeHandler函数,并在输入发生ng变化时调用它。

    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>
    

    简体中文:

    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;
      };
    
     });